home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c017 / 34.ddi / DATECLS3.ZIP / DATECLS3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-15  |  7.3 KB  |  341 lines

  1. #include "datecls3.hpp"
  2.  
  3. const char *dayname[] = {"Sunday","Monday","Tuesday","Wednesday",
  4.        "Thursday","Friday","Saturday"} ;
  5.  
  6. const char *mname[] = {"January","February","March","April","May",
  7.        "June","July","August","September","October","November","December"};
  8.  
  9. const char month_days[2][13] = {
  10.        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
  11.        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
  12.  
  13. ////////////////////////////////////////////////////////////
  14. // Constructors
  15. ////////////////////////////////////////////////////////////
  16.  
  17. Date::Date()
  18. {
  19.     month = day = year = julian = day_of_week = 0;
  20. }
  21.  
  22. Date::Date (const long j) : julian(j)
  23. {
  24.     julian_to_mdy ();
  25. }
  26.  
  27. Date::Date (const int m, const int d, const int y) : month(m), day(d), year(y)
  28. {
  29.     mdy_to_julian ();
  30. }
  31.  
  32. Date::Date (char *dat)
  33. {
  34.     if (!stricmp(dat, "TODAY"))
  35.     {
  36.         struct date temp_date;
  37.         getdate(&temp_date);
  38.         month = temp_date.da_mon;
  39.         day   = temp_date.da_day;
  40.         year  = temp_date.da_year;
  41.     }
  42.     else
  43.     {
  44.         month = atoi(strtok(dat,"/-"));
  45.         day   = atoi(strtok(NULL,"/-"));
  46.         year  = atoi(strtok(NULL," "));
  47.     }
  48.  
  49.     mdy_to_julian ();
  50. }
  51.  
  52. Date::Date (const date &ds)
  53. {
  54.     month = ds.da_mon;
  55.     day   = ds.da_day;
  56.     year  = ds.da_year;
  57.     mdy_to_julian ();
  58. }
  59.  
  60. Date::Date (const Date &dt)
  61. {
  62.     month = dt.month;
  63.     day   = dt.day;
  64.     year  = dt.year;
  65.     mdy_to_julian ();
  66. }
  67.  
  68. //////////////////////////////////////////////////////////////
  69. // Conversion operations
  70. //////////////////////////////////////////////////////////////
  71.  
  72. Date::operator char *( void )
  73. {
  74.     static char *buf = new char[11];
  75.     if (day==0 || month==0 || year==0)
  76.         strcpy(buf,"invalid date");
  77.     else
  78.         sprintf(buf,"%1d/%1d/%4d",month,day,year);
  79.     return buf;
  80. }
  81.  
  82. //////////////////////////////////////////////////////////////
  83. // Date Arithmetic
  84. //////////////////////////////////////////////////////////////
  85.  
  86. Date &Date::operator + (const long i)
  87. {
  88.     Date *dp = new Date(julian + i);
  89.     return *dp;
  90. }
  91.  
  92. Date &Date::operator - (const long i)
  93. {
  94.     Date *dp = new Date (julian - i);
  95.     return *dp;
  96. }
  97.  
  98. long Date::operator - (const Date &dt)
  99. {
  100.     return ( julian - dt.julian );
  101. }
  102.  
  103. Date &Date::operator += (const long i)
  104. {
  105.      julian += i;
  106.      julian_to_mdy();
  107.      return *this;
  108. }
  109.  
  110. Date &Date::operator -= (const long i)
  111. {
  112.      julian -= i;
  113.      julian_to_mdy();
  114.      return *this;
  115. }
  116.  
  117. Date &Date::operator ++ ( void )
  118. {
  119.     julian++;
  120.     julian_to_mdy();
  121.     return *this;
  122. }
  123.  
  124. Date &Date::operator -- ( void )
  125. {
  126.     julian--;
  127.     julian_to_mdy();
  128.     return *this;
  129. }
  130.  
  131. //////////////////////////////////////////////////////////////
  132. // Date comparison
  133. //////////////////////////////////////////////////////////////
  134.  
  135. int operator <  (const Date &dt1, const Date &dt2) const
  136. {
  137.     return ( dt1.julian < dt2.julian );
  138. }
  139.  
  140. int operator <= (const Date &dt1, const Date &dt2) const
  141. {
  142.     return ( (dt1.julian == dt2.julian) || (dt1.julian < dt2.julian) );
  143. }
  144.  
  145. int operator >  (const Date &dt1, const Date &dt2) const
  146. {
  147.     return ( dt1.julian > dt2.julian );
  148. }
  149.  
  150. int operator >= (const Date &dt1, const Date &dt2) const
  151. {
  152.     return ( (dt1.julian == dt2.julian) || (dt1.julian > dt2.julian) );
  153. }
  154.  
  155. int operator == (const Date &dt1, const Date &dt2) const
  156. {
  157.     return ( dt1.julian == dt2.julian );
  158. }
  159.  
  160. int operator != (const Date &dt1, const Date &dt2) const
  161. {
  162.     return ( dt1.julian != dt2.julian );
  163. }
  164.  
  165. ////////////////////////////////////////////////////////////////
  166. // Ostream operations
  167. ////////////////////////////////////////////////////////////////
  168.  
  169. ostream &operator << (ostream &os, const Date &dt) const
  170. {
  171.     return os << dt.month << "/" << dt.day << "/" << dt.year;
  172. }
  173.  
  174. ostream &operator << (ostream &os, const date &dt) const
  175. {
  176.     return os << (int)dt.da_mon << "/" << (int)dt.da_day << "/" << dt.da_year;
  177. }
  178.  
  179. //////////////////////////////////////////////////////////////
  180. // Conversion routines
  181. //////////////////////////////////////////////////////////////
  182.  
  183. void Date::julian_to_wday (void)
  184. {
  185.     day_of_week = (int) ((julian + 2) % 7 + 1);
  186. }
  187.  
  188. void Date::julian_to_mdy ()
  189. {
  190.     long a,b,c,d,e,z,alpha;
  191.     z = julian+1;
  192.     // dealing with Gregorian calendar reform
  193.     if (z < 2299161L)
  194.          a = z;
  195.     else
  196.         {
  197.         alpha = (long) ((z-1867216.25) / 36524.25);
  198.         a = z + 1 + alpha - alpha/4;
  199.         }
  200.     b = a + 1524;
  201.     c = (long) ((b - 122.1) / 365.25);
  202.     d = (long) (365.25 * c);
  203.     e = (long) ((b - d) / 30.6001);
  204.     day = (int) b - d - (long)(30.6001 * e);
  205.     month = (int) (e < 13.5) ? e - 1 : e - 13;
  206.     year = (int) (month > 2.5 ) ? (c - 4716) : c - 4715;
  207.     julian_to_wday ();
  208. }
  209.  
  210. void Date::mdy_to_julian (void)
  211. {
  212.     int a,b=0;
  213.     int work_month=month, work_day=day, work_year=year;
  214.     float year_corr;
  215.     // correct for negative year
  216.     year_corr = (work_year > 0 ? 0.0 : 0.75);
  217.     if (work_month <= 2)
  218.         { work_year--; work_month +=12; }
  219.  
  220.     // deal with Gregorian calendar
  221.     if (work_year*10000. + work_month*100. + work_day >= 15821015.)
  222.         {
  223.         a = work_year/100.;
  224.         b = 2 - a + a/4;
  225.         }
  226.     julian = (long) (365.25*work_year - year_corr) +
  227.              (long) (30.6001 * (work_month+1))  +  work_day + 1720994L + b;
  228.     julian_to_wday ();
  229. }
  230.  
  231. ////////////////////////////////////////////////////////////////
  232. // Format routine
  233. ////////////////////////////////////////////////////////////////
  234.  
  235. char *Date::formatDate (const int type) const
  236. {
  237.     static char buf[40];
  238.     switch (type)
  239.     {
  240.         case MDY:
  241.             if (day==0 || month==0 || year==0)
  242.                 strcpy(buf,"invalid date");
  243.             else
  244.                 sprintf(buf,"%1d/%1d/%4d",month,day,year);
  245.             return buf;
  246.             break;
  247.  
  248.         case DAY:
  249.             if ( (day_of_week < 1) || (day_of_week > 7) )
  250.                 strcpy(buf,"invalid day");
  251.             else
  252.                 strcpy(buf,dayname[day_of_week-1]);
  253.             return buf;
  254.             break;
  255.  
  256.         case MONTH:
  257.             if ( (month < 1) || (month > 12) )
  258.                 strcpy(buf,"invalid month");
  259.             else
  260.                 strcpy(buf,mname[month-1]);
  261.             return buf;
  262.             break;
  263.  
  264.         case FULL:
  265.             if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
  266.                  (day_of_week > 7) )
  267.             {
  268.                 strcpy(buf,"invalid date");
  269.                 return buf;
  270.             }
  271.             sprintf(buf,"%s, %s %d, %d",
  272.                     dayname[day_of_week-1],mname[month-1],day,abs(year));
  273.             if (year < 0)
  274.                 strcat(buf," B.C.E.");
  275.             return buf;
  276.             break;
  277.  
  278.         case EUROPEAN:
  279.             if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
  280.                  (day_of_week > 7) )
  281.             {
  282.                 strcpy(buf,"invalid date");
  283.                 return buf;
  284.             }
  285.             sprintf(buf,"%d %s %d",
  286.                     day, mname[month-1], abs(year));
  287.             if (year < 0)
  288.                 strcat(buf," B.C.E.");
  289.             return buf;
  290.             break;
  291.  
  292.         default:
  293.             break;
  294.     }
  295. }
  296.  
  297. ///////////////////////////////////////////////////////////////
  298. //  Miscellaneous Routines
  299. ///////////////////////////////////////////////////////////////
  300.  
  301. long Date::julDate( void ) const
  302. {
  303.     return julian;
  304. }
  305.  
  306. int Date::dayOfYear( void ) const
  307. {
  308.     int year_day=day;
  309.  
  310.     for (int i = 1; i < month; i++)
  311.     {
  312.         year_day += month_days[isLeapYear()][i];
  313.     }
  314.  
  315.     return year_day;
  316. }
  317.  
  318.  
  319. int Date::isLeapYear( void ) const
  320. {
  321.     return  ( year%4 == 0  &&  year%100 != 0  ||  year%400 == 0 );
  322. }
  323.  
  324. date Date::eom( void ) const
  325. {
  326.     static date eom_temp;
  327.     eom_temp.da_year  = year;
  328.     eom_temp.da_mon   = month;
  329.     eom_temp.da_day   = month_days[isLeapYear()][month];
  330.     return eom_temp;
  331. }
  332.  
  333. date Date::getDate( void ) const
  334. {
  335.     static date getDate_temp;
  336.     getDate_temp.da_year  = year;
  337.     getDate_temp.da_mon   = month;
  338.     getDate_temp.da_day   = day;
  339.     return getDate_temp;
  340. }
  341.