home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snpd9707.zip / DATE.CPP < prev    next >
Text File  |  1997-07-05  |  9KB  |  378 lines

  1. .I 0 1
  2. // +++Date last modified: 05-Jul-1997
  3. .D 1 1
  4. .I 19 8
  5.         zDate::week_day zDate::BeginDSTDay     = zDate::sun;
  6.         zDate::month    zDate::BeginDSTMonth   = zDate::apr;
  7.         zDate::week_day zDate::EndDSTDay       = zDate::sun;
  8.         zDate::month    zDate::EndDSTMonth     = zDate::oct;
  9.  
  10. zDate::zDate()
  11. {
  12.       Set(jan, 1, 1);
  13. .D 20 8
  14. .I 31 1
  15.       Set(aMonth, aDay, aYear);
  16. .D 32 1
  17. .I 36 5
  18.       m_day   = 31;
  19.       m_month = dec;
  20.       m_year  = year - 1;
  21.       m_dayno = MakeDayNumber();
  22.       FromDayNumber(m_dayno + dayOfYear);
  23. .D 37 5
  24. .I 46 6
  25.       m_month = aMonth;
  26.       m_day   = aDay;
  27.       m_year  = aYear;
  28.       m_dayno = MakeDayNumber();
  29.  
  30.       return *this;
  31. .D 47 6
  32. .I 56 4
  33.       m_month = aDate.m_month;
  34.       m_day   = aDate.m_day;
  35.       m_year  = aDate.m_year;
  36.       m_dayno = aDate.m_dayno;
  37. .D 57 4
  38. .I 64 1
  39.       FromDayNumber(nJulian);
  40. .D 65 1
  41. .I 69 4
  42.       m_month = (month)(tmDate->tm_mon + 1);
  43.       m_day   = tmDate->tm_mday;
  44.       m_year  = tmDate->tm_year + 1900;
  45.       m_dayno = MakeDayNumber();
  46. .D 70 4
  47. .I 78 5
  48.       time_t     secs_now = time(0);
  49.       struct tm *time_now = localtime(&secs_now);
  50.       zDate      today(time_now);
  51.  
  52.       return today;
  53. .D 79 5
  54. .I 88 1
  55.       return Boolean(m_dayno != aDate.m_dayno);
  56. .D 89 1
  57. .I 94 1
  58.       return Boolean(m_dayno == aDate.m_dayno);
  59. .D 95 1
  60. .I 100 1
  61.       return Boolean(m_dayno < aDate.m_dayno);
  62. .D 101 1
  63. .I 106 1
  64.       return Boolean(m_dayno <= aDate.m_dayno);
  65. .D 107 1
  66. .I 112 1
  67.       return Boolean(m_dayno > aDate.m_dayno);
  68. .D 113 1
  69. .I 118 1
  70.       return Boolean(m_dayno >= aDate.m_dayno);
  71. .D 119 1
  72. .I 124 6
  73.       m_day   = aDate.m_day;
  74.       m_month = aDate.m_month;
  75.       m_year  = aDate.m_year;
  76.       m_dayno = aDate.m_dayno;
  77.  
  78.       return *this;
  79. .D 125 6
  80. .I 135 4
  81.       static const int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  82.  
  83.       if( aYear == ReformYear && aMonth == ReformMonth ) return 21;
  84.       return days[aMonth] + (feb == aMonth && IsLeapYear(aYear));
  85. .D 136 4
  86. .I 144 3
  87.       zDate first(jan, 1, m_year);
  88.  
  89.       return 1 + (int)(m_dayno - first.m_dayno);
  90. .D 145 3
  91. .I 152 4
  92.       int days = 365 + IsLeapYear(year);
  93.       // 10 days cancelled by the reform of pope Gregor XIII
  94.       if( year == ReformYear ) return days - 10;
  95.       else return days;
  96. .D 153 4
  97. .I 161 5
  98.       if( year % 4 ) return False;  // if not divisible by 4, not leap
  99.       if( year < ReformYear ) return True; // before this year, all were leap
  100.       if( year % 100 ) return True; // by 4, but not by 100 is leap
  101.       if( year % 400 ) return False;      // not by 100 and not by 400 not leap
  102.       return True;
  103. .D 162 5
  104. .I 171 5
  105.       return Boolean(
  106.                aYear  > 0
  107.             && aMonth >= jan && aMonth <= dec
  108.             && aDay   >  0   && aDay   <= DaysInMonth(aMonth, aYear)
  109.       );
  110. .D 172 5
  111. .I 181 6
  112.       int age = m_year - birthday.m_year - 1;
  113.  
  114.       if( m_month > birthday.m_month ) age++;
  115.       else if( m_month == birthday.m_month && m_day >= birthday.m_day ) age++;
  116.  
  117.       return age;
  118. .D 182 6
  119. .I 192 1
  120.       return zDate(m_dayno + (long)nDays);
  121. .D 193 1
  122. .I 198 1
  123.       return zDate(m_dayno + nDays);
  124. .D 199 1
  125. .I 204 1
  126.       return zDate(m_dayno - (long)nDays);
  127. .D 205 1
  128. .I 210 1
  129.       return zDate(m_dayno - nDays);
  130. .D 211 1
  131. .I 216 1
  132.       return (long)(m_dayno - aDate.m_dayno);
  133. .D 217 1
  134. .I 222 2
  135.       FromDayNumber(m_dayno + (long)nDays);
  136.       return *this;
  137. .D 223 2
  138. .I 229 2
  139.       FromDayNumber(m_dayno + nDays);
  140.       return *this;
  141. .D 230 2
  142. .I 236 2
  143.       FromDayNumber(m_dayno + 1L);
  144.       return *this;
  145. .D 237 2
  146. .I 243 4
  147.       zDate date(*this);
  148.  
  149.       FromDayNumber(m_dayno + 1L);
  150.       return date;
  151. .D 244 4
  152. .I 252 2
  153.       FromDayNumber(m_dayno - 1L);
  154.       return *this;
  155. .D 253 2
  156. .I 260 4
  157.       zDate date(*this);
  158.  
  159.       FromDayNumber(m_dayno - 1L);
  160.       return date;
  161. .D 261 4
  162. .I 269 2
  163.       FromDayNumber(m_dayno - (long)nDays);
  164.       return *this;
  165. .D 270 2
  166. .I 276 2
  167.       FromDayNumber(m_dayno - nDays);
  168.       return *this;
  169. .D 277 2
  170. .I 283 3
  171.       zDate first(jan, 1, m_year);
  172.  
  173.       return 1 + int((m_dayno - first.m_dayno + 1) / 7);
  174. .D 284 3
  175. .I 291 3
  176.       int abs_mday = m_day + zDate(m_month, 1, m_year).DayOfWeek() - 1;
  177.  
  178.       return 1 + ((abs_mday - DayOfWeek()) / 7);
  179. .D 292 3
  180. .I 299 1
  181.       return zDate(dec, 31, year).WeekOfYear();
  182. .D 300 1
  183. .I 305 4
  184.       zDate date(*this);
  185.  
  186.       date += (long)nWeeks * 7L;
  187.       return date;
  188. .D 306 4
  189. .I 314 12
  190.       zDate date(*this);
  191.       int   delta = nYears > 0 ? -1 : 1;
  192.       int   year = m_year;
  193.       long  days = 0;
  194.  
  195.       while( nYears ){
  196.             nYears += delta;
  197.             year   -= delta;
  198.             days   += DaysInYear(year);
  199.       }
  200.       date += (-delta * days);
  201.       return date;
  202. .D 315 12
  203. .I 330 1
  204.       return DayNumber();
  205. .D 331 1
  206. .I 340 6
  207.       switch( index ){
  208.             case 0 : return m_day;
  209.             case 1 : return m_month;
  210.             case 2 : return m_year - 1900;
  211.             default: return -1;
  212.       }
  213. .D 341 6
  214. .I 351 1
  215.       return DaysInMonth(m_month, m_year);
  216. .D 352 1
  217. .I 357 1
  218.       return DaysInYear(m_year);
  219. .D 358 1
  220. .I 363 1
  221.       return WeeksInYear(m_year);
  222. .D 364 1
  223. .I 369 1
  224.       return IsValid(m_month, m_day, m_year);
  225. .D 370 1
  226. .I 375 1
  227.       return IsLeapYear(m_year);
  228. .D 376 1
  229. .I 381 20
  230.       long days;
  231.       long year  = (long)m_year - 1L;
  232.  
  233.       // get all days plus all leap years, minus non-leap years
  234.       days = year * 365L + year / 4L - year / 100L + year / 400L;
  235.       // the years before 1582 were all leap if divisible by 4
  236.       if( year > ReformYear ) days += 12;
  237.       else{
  238.             days += year / 100L;
  239.             days -= year / 400L;
  240.       }
  241.       // get the days for the month up to the current one
  242.       for( int i = jan; i < m_month; ++i )
  243.             days += DaysInMonth((month)i, m_year);
  244.       // now add the current days of the month
  245.       days += m_day;
  246.       // now adjust for the 10 missing days (Oct 4 - Oct 15, 1582)
  247.       if( days > ReformDayNumber ) days -= 10L;
  248.       // we have the current day number now
  249.       return days;
  250. .D 382 20
  251. .I 406 3
  252.       const week_day wdays[7] = {sun,mon,tue,wed,thu,fri,sat};
  253.  
  254.       return wdays[(int)(((m_dayno % 7) + 5) % 7)];
  255. .D 407 3
  256. .I 414 30
  257.       m_dayno = dayno;
  258.  
  259.       if( dayno > ReformDayNumber ) dayno += 10L;
  260.  
  261.       m_year = (int)(dayno / 365);
  262.       m_day = (int)(dayno % 365L);
  263.  
  264.       if( m_year < 1700 ) m_day -= (m_year / 4);
  265.       else{
  266.             m_day -= (m_year / 4);
  267.             m_day += (m_year / 100);
  268.             m_day -= (m_year / 400);
  269.             m_day -= 12;
  270.       }
  271.  
  272.       while( m_day <= 0 ){
  273.             m_day += (365 + IsLeapYear(m_year));
  274.             m_year--;
  275.       }
  276.  
  277.       // m_year is the number of elapsed years, add 1 to get current
  278.       m_year += 1;
  279.  
  280.       // figure out the month and current day too
  281.       for( m_month = jan; m_month <= dec; m_month = (month)(m_month + 1) ){
  282.             int days = DaysInMonth(m_month, m_year);
  283.  
  284.             if( m_day <= days ) break;
  285.             else m_day -= days;
  286.       }
  287. .D 415 30
  288. .I 449 1
  289.       return IsDST(*this);
  290. .D 450 1
  291. .I 460 4
  292.       zDate date(BeginDSTMonth, 1, year);
  293.  
  294.       while( BeginDSTDay != date.DayOfWeek() ) date++;
  295.       return date;
  296. .D 461 4
  297. .I 469 4
  298.       zDate date(EndDSTMonth, 31, year);
  299.  
  300.       while( EndDSTDay != date.DayOfWeek() ) date--;
  301.       return date;
  302. .D 470 4
  303. .I 478 1
  304.       return BeginDST(m_year);
  305. .D 479 1
  306. .I 484 1
  307.       return EndDST(m_year);
  308. .D 485 1
  309. .I 490 1
  310.       return Boolean( date >= date.BeginDST() && date <= date.EndDST() );
  311. .D 491 1
  312. .I 496 1
  313.       return MoonPhase(*this);
  314. .D 497 1
  315. .I 502 8
  316.       ulong phase = date.m_dayno;
  317.  
  318.       phase *= 9693L;
  319.       phase /= 35780L;
  320.       phase -= 4L;
  321.       phase %= 8L;
  322.  
  323.       return (moon_phase)phase;
  324. .D 503 8
  325. .I 515 1
  326.       return Easter(m_year);
  327. .D 516 1
  328. .I 525 15
  329.       int c, n, k, i, j, l, m, d;
  330.  
  331.       c = year / 100;
  332.       n = year - 19 * (year / 19);
  333.       k = (c - 17) / 25;
  334.       i = c - c / 4 - (c - k) / 3 + 19 * n + 15;
  335.       i = i - 30 * (i / 30);
  336.       i = i - (i / 28) * (1 - (i / 28) * (29 / (i + 1)) * ((21 - n) / 11));
  337.       j = year + year / 4 + i + 2 - c + c / 4;
  338.       j = j - 7 * (j / 7);
  339.       l = i - j;
  340.       m = 3 + (l + 40) / 44;
  341.       d = l + 28 - 31 * (m / 4);
  342.  
  343.       return zDate((month)m, d, year);
  344. .D 526 15
  345. .I 555 31
  346.       int mon  = m_month + nMonths;
  347.       int year = m_year;
  348.       int day  = m_day;
  349.       int mdays;
  350.  
  351.       while( mon < 1 ){
  352.             mon += 12;
  353.             year--;
  354.       }
  355.  
  356.       while( mon > 12 ){
  357.             mon -= 12;
  358.             year++;
  359.       }
  360.  
  361.       mdays = DaysInMonth((month)mon, year);
  362.  
  363.       if( day > mdays ){
  364.             if( nMonths < 0 ) day = mdays - (day - mdays);
  365.             else{
  366.                   day -= mdays;
  367.                   mon++;
  368.                   if( mon > 12 ){
  369.                         year++;
  370.                         mon = 1;
  371.                   }
  372.             }
  373.       }
  374.  
  375.       return zDate((month)mon, day, year);
  376. }
  377. .D 556 31
  378.