home *** CD-ROM | disk | FTP | other *** search
- #include "datecls3.hpp"
-
- const char *dayname[] = {"Sunday","Monday","Tuesday","Wednesday",
- "Thursday","Friday","Saturday"} ;
-
- const char *mname[] = {"January","February","March","April","May",
- "June","July","August","September","October","November","December"};
-
- const char month_days[2][13] = {
- {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
- {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
-
- ////////////////////////////////////////////////////////////
- // Constructors
- ////////////////////////////////////////////////////////////
-
- Date::Date()
- {
- month = day = year = julian = day_of_week = 0;
- }
-
- Date::Date (const long j) : julian(j)
- {
- julian_to_mdy ();
- }
-
- Date::Date (const int m, const int d, const int y) : month(m), day(d), year(y)
- {
- mdy_to_julian ();
- }
-
- Date::Date (char *dat)
- {
- if (!stricmp(dat, "TODAY"))
- {
- struct date temp_date;
- getdate(&temp_date);
- month = temp_date.da_mon;
- day = temp_date.da_day;
- year = temp_date.da_year;
- }
- else
- {
- month = atoi(strtok(dat,"/-"));
- day = atoi(strtok(NULL,"/-"));
- year = atoi(strtok(NULL," "));
- }
-
- mdy_to_julian ();
- }
-
- Date::Date (const date &ds)
- {
- month = ds.da_mon;
- day = ds.da_day;
- year = ds.da_year;
- mdy_to_julian ();
- }
-
- Date::Date (const Date &dt)
- {
- month = dt.month;
- day = dt.day;
- year = dt.year;
- mdy_to_julian ();
- }
-
- //////////////////////////////////////////////////////////////
- // Conversion operations
- //////////////////////////////////////////////////////////////
-
- Date::operator char *( void )
- {
- static char *buf = new char[11];
- if (day==0 || month==0 || year==0)
- strcpy(buf,"invalid date");
- else
- sprintf(buf,"%1d/%1d/%4d",month,day,year);
- return buf;
- }
-
- //////////////////////////////////////////////////////////////
- // Date Arithmetic
- //////////////////////////////////////////////////////////////
-
- Date &Date::operator + (const long i)
- {
- Date *dp = new Date(julian + i);
- return *dp;
- }
-
- Date &Date::operator - (const long i)
- {
- Date *dp = new Date (julian - i);
- return *dp;
- }
-
- long Date::operator - (const Date &dt)
- {
- return ( julian - dt.julian );
- }
-
- Date &Date::operator += (const long i)
- {
- julian += i;
- julian_to_mdy();
- return *this;
- }
-
- Date &Date::operator -= (const long i)
- {
- julian -= i;
- julian_to_mdy();
- return *this;
- }
-
- Date &Date::operator ++ ( void )
- {
- julian++;
- julian_to_mdy();
- return *this;
- }
-
- Date &Date::operator -- ( void )
- {
- julian--;
- julian_to_mdy();
- return *this;
- }
-
- //////////////////////////////////////////////////////////////
- // Date comparison
- //////////////////////////////////////////////////////////////
-
- int operator < (const Date &dt1, const Date &dt2) const
- {
- return ( dt1.julian < dt2.julian );
- }
-
- int operator <= (const Date &dt1, const Date &dt2) const
- {
- return ( (dt1.julian == dt2.julian) || (dt1.julian < dt2.julian) );
- }
-
- int operator > (const Date &dt1, const Date &dt2) const
- {
- return ( dt1.julian > dt2.julian );
- }
-
- int operator >= (const Date &dt1, const Date &dt2) const
- {
- return ( (dt1.julian == dt2.julian) || (dt1.julian > dt2.julian) );
- }
-
- int operator == (const Date &dt1, const Date &dt2) const
- {
- return ( dt1.julian == dt2.julian );
- }
-
- int operator != (const Date &dt1, const Date &dt2) const
- {
- return ( dt1.julian != dt2.julian );
- }
-
- ////////////////////////////////////////////////////////////////
- // Ostream operations
- ////////////////////////////////////////////////////////////////
-
- ostream &operator << (ostream &os, const Date &dt) const
- {
- return os << dt.month << "/" << dt.day << "/" << dt.year;
- }
-
- ostream &operator << (ostream &os, const date &dt) const
- {
- return os << (int)dt.da_mon << "/" << (int)dt.da_day << "/" << dt.da_year;
- }
-
- //////////////////////////////////////////////////////////////
- // Conversion routines
- //////////////////////////////////////////////////////////////
-
- void Date::julian_to_wday (void)
- {
- day_of_week = (int) ((julian + 2) % 7 + 1);
- }
-
- void Date::julian_to_mdy ()
- {
- long a,b,c,d,e,z,alpha;
- z = julian+1;
- // dealing with Gregorian calendar reform
- if (z < 2299161L)
- a = z;
- else
- {
- alpha = (long) ((z-1867216.25) / 36524.25);
- a = z + 1 + alpha - alpha/4;
- }
- b = a + 1524;
- c = (long) ((b - 122.1) / 365.25);
- d = (long) (365.25 * c);
- e = (long) ((b - d) / 30.6001);
- day = (int) b - d - (long)(30.6001 * e);
- month = (int) (e < 13.5) ? e - 1 : e - 13;
- year = (int) (month > 2.5 ) ? (c - 4716) : c - 4715;
- julian_to_wday ();
- }
-
- void Date::mdy_to_julian (void)
- {
- int a,b=0;
- int work_month=month, work_day=day, work_year=year;
- float year_corr;
- // correct for negative year
- year_corr = (work_year > 0 ? 0.0 : 0.75);
- if (work_month <= 2)
- { work_year--; work_month +=12; }
-
- // deal with Gregorian calendar
- if (work_year*10000. + work_month*100. + work_day >= 15821015.)
- {
- a = work_year/100.;
- b = 2 - a + a/4;
- }
- julian = (long) (365.25*work_year - year_corr) +
- (long) (30.6001 * (work_month+1)) + work_day + 1720994L + b;
- julian_to_wday ();
- }
-
- ////////////////////////////////////////////////////////////////
- // Format routine
- ////////////////////////////////////////////////////////////////
-
- char *Date::formatDate (const int type) const
- {
- static char buf[40];
- switch (type)
- {
- case MDY:
- if (day==0 || month==0 || year==0)
- strcpy(buf,"invalid date");
- else
- sprintf(buf,"%1d/%1d/%4d",month,day,year);
- return buf;
- break;
-
- case DAY:
- if ( (day_of_week < 1) || (day_of_week > 7) )
- strcpy(buf,"invalid day");
- else
- strcpy(buf,dayname[day_of_week-1]);
- return buf;
- break;
-
- case MONTH:
- if ( (month < 1) || (month > 12) )
- strcpy(buf,"invalid month");
- else
- strcpy(buf,mname[month-1]);
- return buf;
- break;
-
- case FULL:
- if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
- (day_of_week > 7) )
- {
- strcpy(buf,"invalid date");
- return buf;
- }
- sprintf(buf,"%s, %s %d, %d",
- dayname[day_of_week-1],mname[month-1],day,abs(year));
- if (year < 0)
- strcat(buf," B.C.E.");
- return buf;
- break;
-
- case EUROPEAN:
- if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
- (day_of_week > 7) )
- {
- strcpy(buf,"invalid date");
- return buf;
- }
- sprintf(buf,"%d %s %d",
- day, mname[month-1], abs(year));
- if (year < 0)
- strcat(buf," B.C.E.");
- return buf;
- break;
-
- default:
- break;
- }
- }
-
- ///////////////////////////////////////////////////////////////
- // Miscellaneous Routines
- ///////////////////////////////////////////////////////////////
-
- long Date::julDate( void ) const
- {
- return julian;
- }
-
- int Date::dayOfYear( void ) const
- {
- int year_day=day;
-
- for (int i = 1; i < month; i++)
- {
- year_day += month_days[isLeapYear()][i];
- }
-
- return year_day;
- }
-
-
- int Date::isLeapYear( void ) const
- {
- return ( year%4 == 0 && year%100 != 0 || year%400 == 0 );
- }
-
- date Date::eom( void ) const
- {
- static date eom_temp;
- eom_temp.da_year = year;
- eom_temp.da_mon = month;
- eom_temp.da_day = month_days[isLeapYear()][month];
- return eom_temp;
- }
-
- date Date::getDate( void ) const
- {
- static date getDate_temp;
- getDate_temp.da_year = year;
- getDate_temp.da_mon = month;
- getDate_temp.da_day = day;
- return getDate_temp;
- }