00001 #ifndef H__DATE_H
00002 #define H__DATE_H
00011 #include <stdlib.h>
00012 #include <time.h>
00013
00014 class Date
00015 {
00016
00017 private:
00018
00019 long lJulianDay;
00020
00022 static long YmdToJd( const int iYear, const int iMonth, const int iDay );
00023
00026 static void JdToYmd( const long lJD, int *piYear, int *piMonth,
00027 int *piDay );
00036 Date& Add( const long iDays )
00037 {
00038 lJulianDay += iDays;
00039 return *this;
00040 }
00041
00050 Date& Subtract( const long iDays )
00051 {
00052 lJulianDay -= iDays;
00053 return *this;
00054 }
00055
00064 long Subtract( const Date Other ) const
00065 {
00066 return lJulianDay - Other.lJulianDay;
00067 }
00068
00069 public:
00070
00080 static int IsLeapYear( const int iYear )
00081 {
00082 long jd1, jd2;
00083 jd1 = YmdToJd( iYear, 2, 29 );
00084 jd2 = YmdToJd( iYear, 3, 1 );
00085 return (int) ( jd2 - jd1 );
00086 }
00087
00095 Date() { lJulianDay = 0L; }
00096
00104 Date( const Date& Orig ) { lJulianDay = Orig.lJulianDay; }
00105
00115 Date( const time_t tSysTime )
00116 {
00117 struct tm *ptm;
00118 int y, m, d;
00119
00120 ptm = localtime( &tSysTime );
00121 y = ptm->tm_year + 1900;
00122 m = ptm->tm_mon + 1;
00123 d = ptm->tm_mday;
00124 lJulianDay = YmdToJd( y, m, d );
00125 }
00126
00134 Date( const char *String )
00135 {
00136 lJulianDay = atol( String );
00137 }
00138
00150 Date( const int iDay, const int iMonth, const int iYear )
00151 {
00152 lJulianDay = YmdToJd( iYear, iMonth, iDay );
00153 }
00154
00161 int Year( void ) const
00162 {
00163 int y, m, d;
00164
00165 JdToYmd( lJulianDay, &y, &m, &d );
00166 return y;
00167 }
00168
00175 int Month( void ) const
00176 {
00177 int y, m, d;
00178
00179 JdToYmd( lJulianDay, &y, &m, &d );
00180 return m;
00181 }
00182
00189 int Day( void ) const
00190 {
00191 int y, m, d;
00192
00193 JdToYmd( lJulianDay, &y, &m, &d );
00194 return d;
00195 }
00196
00198 int DayOfYear( void ) const;
00199
00209 int DayOfWeek( void ) const
00210 {
00211 return ( ( ( (int) ( lJulianDay % 7L ) ) + 1 ) % 7 );
00212 }
00213
00222 int DayOfWorkWeek( void ) const
00223 {
00224 return ( (int) ( lJulianDay % 7L ) );
00225 }
00226
00234 int IsLeapYear( void ) const
00235 {
00236 int y, m, d;
00237 JdToYmd( lJulianDay, &y, &m, &d );
00238 return IsLeapYear( y );
00239 }
00240
00252 void YMD( int *pY, int *pM, int *pD )
00253 {
00254 JdToYmd( lJulianDay, pY, pM, pD );
00255 return;
00256 }
00257
00268 friend Date operator+( const Date& Left, const long Right )
00269 {
00270 Date Temp = Left;
00271 Temp.Add( Right );
00272 return Temp;
00273 }
00274
00285 friend Date operator+( const long Left, const Date& Right )
00286 {
00287 Date Temp = Right;
00288 Temp.Add( Left );
00289 return Temp;
00290 }
00291
00302 friend Date operator-( const Date& Left, const long Right )
00303 {
00304 Date Temp = Left;
00305 Temp.Subtract( Right );
00306 return Temp;
00307 }
00319 friend Date operator-( const long Left, const Date& Right )
00320 {
00321 Date Temp = Right;
00322 Temp.Subtract( Left );
00323 return Temp;
00324 }
00325
00334 Date& operator+=( const long Right )
00335 {
00336 Add( Right );
00337 return *this;
00338 }
00347 Date& operator-=( const long Right )
00348 {
00349 Subtract( Right );
00350 return *this;
00351 }
00352
00361 long operator-( const Date& Right )
00362 {
00363 return lJulianDay - Right.lJulianDay;
00364 }
00365
00374 Date& operator++()
00375 {
00376 lJulianDay++;
00377 return *this;
00378 }
00379 Date& operator--()
00380 {
00381 lJulianDay++;
00382 return *this;
00383 }
00385
00394 Date operator++( int )
00395 {
00396 Date Temp = *this;
00397 lJulianDay++;
00398 return Temp;
00399 }
00400 Date operator--( int )
00401 {
00402 Date Temp = *this;
00403 lJulianDay++;
00404 return Temp;
00405 }
00407
00409 void ToString( char *szBuffer ) const;
00410
00418 Date& operator=( const Date& Orig )
00419 {
00420 lJulianDay = Orig.lJulianDay;
00421 return *this;
00422 }
00423
00435 int operator==( const Date& Right ) const
00436 {
00437 return lJulianDay == Right.lJulianDay;
00438 }
00439 int operator!=( const Date& Right ) const
00440 {
00441 return lJulianDay != Right.lJulianDay;
00442 }
00443 int operator<( const Date& Right ) const
00444 {
00445 return lJulianDay < Right.lJulianDay;
00446 }
00447 int operator<=( const Date& Right ) const
00448 {
00449 return lJulianDay <= Right.lJulianDay;
00450 }
00451 int operator>( const Date& Right ) const
00452 {
00453 return lJulianDay > Right.lJulianDay;
00454 }
00455 int operator>=( const Date& Right ) const
00456 {
00457 return lJulianDay >= Right.lJulianDay;
00458 }
00460
00463 time_t ToSysTime( void ) const;
00464
00465 };
00466
00467 #endif