Public paste
myClass.h
By: jazy | Date: Sep 21 2007 01:01 | Format: C++ | Expires: never | Size: 2.83 KB | Hits: 1248

  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class dateType {
  9.  
  10. private:
  11.  
  12. long julianDay;
  13. long toJulianDay(int m, int d, int y);
  14. std::vector<int> monthLength;
  15.  
  16. public:
  17.  
  18. long getJulian() {
  19. return julianDay;
  20. }
  21.  
  22.  void incDate() {
  23.    julianDay += 1;
  24.  }
  25.  int daysToDate(dateType otherDate) {
  26.    return julianDay - otherDate.julianDay;
  27.  }
  28.  
  29.  void setDate(int m, int d, int y) {
  30.    cout<<"nEnter the following.... n";
  31.    cout<<"Month: ";
  32.    cin>>m;
  33.    cout<<"Day: ";
  34.    cin>>d;
  35.    cout<<"Year: ";
  36.    cin>>y;
  37.   if (validDate(m,d,y)) {
  38. julianDay = toJulianDay(m,d,y);
  39.   }
  40.   else
  41.     julianDay = 0;
  42. }
  43.  
  44.  void printDate() {
  45.    cout<<"JulianDate: "<<julianDay<<endl;
  46.  }
  47.  
  48. void setVector(int isLeap,int y) {
  49. if (isLeap == 0) {
  50. monthLength.push_back(0);
  51. monthLength.push_back(31);
  52. monthLength.push_back(28);
  53. monthLength.push_back(31);
  54. monthLength.push_back(30);
  55. monthLength.push_back(31);
  56. monthLength.push_back(30);
  57. monthLength.push_back(31);
  58. monthLength.push_back(31);
  59. monthLength.push_back(30);
  60. monthLength.push_back(31);
  61. monthLength.push_back(30);
  62. monthLength.push_back(31);
  63. }
  64. else {
  65. monthLength.push_back(0);
  66. monthLength.push_back(31);
  67. monthLength.push_back(29);
  68. monthLength.push_back(31);
  69. monthLength.push_back(30);
  70. monthLength.push_back(31);
  71. monthLength.push_back(30);
  72. monthLength.push_back(31);
  73. monthLength.push_back(31);
  74. monthLength.push_back(30);
  75. monthLength.push_back(31);
  76. monthLength.push_back(30);
  77. monthLength.push_back(31);
  78. }
  79.  
  80. }
  81.  
  82. int isLeapYear(int y) {
  83. long tmp, tmp1;
  84. tmp = toJulianDay(2,28,y);
  85. tmp1 = toJulianDay(3,1,y);
  86. return ((tmp1-tmp) > 1);
  87. }
  88.  
  89. bool validDate (int m,int d, int y) {
  90. setVector(isLeapYear(y),y);
  91.  
  92. //have to validate the month first//
  93. if (m > 0 && m < 13)
  94. std::cout<<"month looks good.....n";
  95. else
  96. return false;
  97.  
  98. vector<int>::iterator iter;
  99.  iter = monthLength.begin();
  100.  for (int i = 0 ; i < m; i++) {
  101.    iter++;
  102.  }
  103.  
  104.  int test = *iter;
  105.  if (d < test && d > 0)
  106.    cout<<"month looks goodn";
  107.  
  108. else
  109. return false;
  110.  
  111. if (y <= 9999 && y >= -4713 )
  112. std::cout<<"year looks good.....n";
  113. else
  114. return false;
  115.  
  116. return true;
  117. }
  118.  
  119.  
  120. dateType() {
  121. julianDay = 0;
  122. }
  123.  
  124. dateType(int m,int d,int y) {
  125.   if (validDate(m,d,y)) {
  126. julianDay = toJulianDay(m,d,y);
  127.   }
  128.   else
  129.     julianDay = 0;
  130. }
  131.  
  132. };
  133.  
  134.  
  135. long dateType::toJulianDay(int m, int d, int y) {
  136. long toReturn;
  137.  
  138. int a, b , yy, mm, dd;
  139. float yorr;
  140.  
  141. yy = y;
  142. mm = m;
  143. dd = d;
  144.  
  145. if (yy < 0)
  146. yy++;
  147.  
  148. yorr = ( yy > 0 ? 0.0 : 0.75 );
  149. if (mm <= 2) {
  150. yy--;
  151. mm += 12;
  152. }
  153.  
  154. b = 0;
  155. if (yy *10000.0 + mm * 100.0 + dd >= 15821015.0 ) {
  156. a = yy / 100;
  157. b = 2 - a + a /4;
  158. }
  159.  
  160. toReturn = dd - 32075 + 1461 * (yy + 4800 + ( mm - 14) / 12) / 4 +
  161. 367L * ( mm - 2 - ( mm - 14 ) /12 * 12) / 12 -
  162. 3 * ( (yy+4900 +(mm -14)/12)/100)/4;
  163.  
  164. return toReturn;
  165. }
  166.  
  167.