cdate.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifndef _CDATE_H
  2. #define _CDATE_H
  3. //-----------------------------------------------------------------------------
  4. // @doc
  5. // @module cdate.h : definitions for the CDate class |
  6. //
  7. // This class encapsulates date and time and many useful date/time manipulation
  8. // routines.
  9. //
  10. // Copyright <cp> 1995 Microsoft Corporation, All Rights Reserved
  11. //
  12. // Mark Snyder, Software Design Engineer
  13. #ifndef _INC_TIME
  14. #include "time.h"
  15. #endif
  16. //#ifndef _STDTIME_H
  17. //#include "stdtime.h"
  18. //#endif
  19. // data structure used during parsing of date strings
  20. struct rcParse { int nBump; int nVal; };
  21. class TValue
  22. {
  23. protected:
  24. int value;
  25. public:
  26. TValue(int i=0) : value(i) {};
  27. operator int() const { return value; }
  28. operator + ( const int increment ) const { return value + increment; }
  29. operator - ( const int increment ) const { return value - increment; }
  30. // operator +=( const int increment ) { value += increment; return value; }
  31. // operator -=( const int increment ) { value -= increment; return value; }
  32. operator ++( const int increment ) { value++; return value; }
  33. operator --( const int increment ) { value--; return value; }
  34. };
  35. class CYear : public TValue { public: CYear(int i=0) : TValue(i) {} };
  36. class CMon : public TValue { public: CMon (int i=0) : TValue(i) {} };
  37. class CDay : public TValue { public: CDay (int i=0) : TValue(i) {} };
  38. class CHour : public TValue { public: CHour(int i=0) : TValue(i) {} };
  39. class CMin : public TValue { public: CMin (int i=0) : TValue(i) {} };
  40. class CSec : public TValue { public: CSec (int i=0) : TValue(i) {} };
  41. //------------------------------------------------------------------------------
  42. //
  43. // @class This class is used to contain a date and time object which can
  44. // manipulated and used to keep track of a date/time value. Other
  45. // services provided allow for querying the day of week, day of year,
  46. // etc.
  47. //
  48. class CDate
  49. {
  50. public:
  51. // This enumeration is used to parse the Windows int date format
  52. enum DateToken
  53. {
  54. DateToken_m,
  55. DateToken_mm,
  56. DateToken_d,
  57. DateToken_dd,
  58. DateToken_yy,
  59. DateToken_yyyy,
  60. DateTokenUnrecognized,
  61. };
  62. private:
  63. CYear year; // Year (calendar year; i.e., 1989)
  64. CMon mon; // Month (1--12)
  65. CDay mday; // Day of month (1--31)
  66. CHour hour; // Hour (0--23)
  67. CMin minute; // Minutes (0--59)
  68. CSec sec; // Seconds (0--59)
  69. //ATime m_atm;
  70. // state transition functions for parsing a string containing a date/time
  71. int StateInit(const char*& lpChar, CDate& fDate );
  72. int State1(const char*& lpChar, CDate& fDate );
  73. int State2(const char*& lpChar, CDate& fDate );
  74. int State3(const char*& lpChar, CDate& fDate );
  75. int State4(const char*& lpChar, CDate& fDate );
  76. int State10(const char*& lpChar, CDate& fDate );
  77. int State11(const char*& lpChar, CDate& fDate );
  78. int State12(const char*& lpChar, CDate& fDate );
  79. protected:
  80. int DivisibleUnits( int x, int y, int z ) const;
  81. int LeapYearsBetween( int x, int y ) const;
  82. int abs( int x ) const { return (x>0) ? x : -x; }
  83. void IncDay ( int incday = 1 );
  84. void IncMonth( int incmon = 1 );
  85. void IncYear ( int incyear = 1 );
  86. void IncHour ( int inchour = 1 );
  87. void IncMin ( int incmin = 1 );
  88. void IncSec ( int incsec = 1 );
  89. void DecDay ( int incday = 1 );
  90. void DecMonth( int incmon = 1 );
  91. void DecYear ( int incyear = 1 );
  92. void DecHour ( int inchour = 1 );
  93. void DecMin ( int incmin = 1 );
  94. void DecSec ( int incsec = 1 );
  95. // Method used for parsing the Windows int date format
  96. DateToken GetNextDateToken(LPCSTR & lpFormat);
  97. public:
  98. CDate() { SetDateCurrent(); }
  99. CDate(CYear newyear, CMon newmonth, CDay newmday, CHour newhour, CMin newminute, CSec newsecond )
  100. : sec(newsecond), minute(newminute), hour(newhour), mday(newmday), mon(newmonth), year(newyear) {}
  101. //CDate(const ATime& a)
  102. // : year(a.Year()), mon(a.Month()), mday(a.Day()), hour(a.Hour()), minute(a.Minute()), sec(0) {}
  103. //CDate(const RTime&);
  104. CDate(const CDate& d)
  105. : year(d.year), mon(d.mon), mday(d.mday), hour(d.hour), minute(d.minute), sec(d.sec) {}
  106. CDate(const FILETIME ft);
  107. int GetDay() const { return mday; }
  108. int GetMonth() const { return mon; }
  109. int GetYear() const { return year; }
  110. int GetHour() const { return hour; }
  111. int GetMin() const { return minute; }
  112. int GetSec() const { return sec; }
  113. int GetDate( LPSTR szBuf, int nBufSize, LPCSTR szFmt );
  114. void GetWinTime(CString& str);
  115. void GetWinShortDate(CString& str);
  116. void GetParseTime(CString& str);
  117. void GetParseDate(CString& str) const;
  118. void GetFiletime(FILETIME* ft) const;
  119. CDate GetDate() const { CDate t(*this); t.SetTime(0,0,0); return t; }
  120. CDate GetTime() const { CDate t(*this); t.SetDate(1,1,1900); return t; }
  121. int GetDayOfWeek() const;
  122. int GetDayOfYear() const;
  123. int GetDaysInMonth() const;
  124. int GetDaysInMonth( int i ) const;
  125. int GetWeeksInMonth() const;
  126. void SetDay ( int mday );
  127. void SetMonth( int mon );
  128. void SetYear ( int year );
  129. void SetHour ( int hour );
  130. void SetMin ( int minute );
  131. void SetSec ( int sec );
  132. void SetTime ( int hour, int minute, int sec );
  133. void SetDate ( int mon, int mday, int year );
  134. int SetDate ( const char* lpszDate );
  135. void SetDateCurrent();
  136. int DayDiff( const CDate& ADate ) const;
  137. int isLeapYear() const;
  138. BOOL isLessThan(const CDate& testDate) const;
  139. BOOL isEqualTo(const CDate& testDate) const;
  140. BOOL operator <( const CDate& testDate ) const { return isLessThan(testDate); }
  141. BOOL operator ==( const CDate& testDate ) const { return isEqualTo(testDate); }
  142. BOOL operator !=( const CDate& testDate ) const { return !isEqualTo(testDate); }
  143. BOOL operator <=( const CDate& testDate ) const { return isLessThan(testDate) || isEqualTo(testDate); }
  144. BOOL operator >( const CDate& testDate ) const { return !(isLessThan(testDate) || isEqualTo(testDate)); }
  145. BOOL operator >=( const CDate& testDate ) const { return !isLessThan(testDate); }
  146. CDate operator +( const CSec sec_inc ) const;
  147. CDate operator +( const CMin min_inc ) const;
  148. CDate operator +( const CHour hour_inc ) const;
  149. CDate operator +( const CDay mday_inc ) const;
  150. CDate operator +( const CMon mon_inc ) const;
  151. CDate operator +( const CYear year_inc ) const;
  152. CDate operator -( const CSec sec_inc ) const;
  153. CDate operator -( const CMin min_inc ) const;
  154. CDate operator -( const CHour hour_inc ) const;
  155. CDate operator -( const CDay mday_inc ) const;
  156. CDate operator -( const CMon mon_inc ) const;
  157. CDate operator -( const CYear year_inc ) const;
  158. void operator =( const CDate& a ) { SetDate(a.GetMonth(), a.GetDay(), a.GetYear()); SetTime(a.GetHour(), a.GetMin(), a.GetSec()); }
  159. //void operator =( const ATime& a ) { SetDate(a.Month(), a.Day(), a.Year()); SetTime(a.Hour(), a.Minute(), 0); }
  160. //void operator =( const RTime& r ) { operator=((ATime)r); }
  161. void operator =( const time_t time );
  162. // operator ATime () const;
  163. //operator RTime (void) const;
  164. virtual void Serialize( CArchive& ar );
  165. };
  166. void DDX_CDate(CDataExchange* pDX, int nIDC, CDate& value);
  167. CArchive& operator>> (CArchive& ar, CDate& d);
  168. CArchive& operator<< (CArchive& ar, CDate& d);
  169. // These templates are needed to allow us to add/sub any class to a CDate for which
  170. // CDate supplies the + and - that these use for valid math types, namely the
  171. // classes CMin, CSec, CHour, CMonth, CDay, and CYear.
  172. #define protoPlusEquals(type) \
  173. CDate operator+=(CDate& d,const type& t)
  174. #define protoMinusEquals(type) \
  175. CDate operator-=(CDate& d,const type& t)
  176. protoPlusEquals(CYear);
  177. protoMinusEquals(CYear);
  178. protoPlusEquals(CMon);
  179. protoMinusEquals(CMon);
  180. protoPlusEquals(CDay);
  181. protoMinusEquals(CDay);
  182. protoPlusEquals(CHour);
  183. protoMinusEquals(CHour);
  184. protoPlusEquals(CMin);
  185. protoMinusEquals(CMin);
  186. protoPlusEquals(CSec);
  187. protoMinusEquals(CSec);
  188. class CDateRange
  189. {
  190. private:
  191. // State transition functions
  192. int StateInit(const char*& lpChar, CDate& fDate, CDate& lDate );
  193. int State1(const char*& lpChar, CDate& fDate, CDate& lDate );
  194. int State2(const char*& lpChar, CDate& fDate, CDate& lDate );
  195. int State3(const char*& lpChar, CDate& fDate, CDate& lDate );
  196. int State4(const char*& lpChar, CDate& fDate, CDate& lDate );
  197. int State5(const char*& lpChar, CDate& fDate, CDate& lDate );
  198. int State6(const char*& lpChar, CDate& fDate, CDate& lDate );
  199. int State7(const char*& lpChar, CDate& fDate, CDate& lDate );
  200. int State8(const char*& lpChar, CDate& fDate, CDate& lDate );
  201. int State9(const char*& lpChar, CDate& fDate, CDate& lDate );
  202. public:
  203. CDate m_fDate, m_lDate;
  204. CDateRange() {}
  205. CDateRange(LPCSTR lpszDate) { SetRange(lpszDate); }
  206. CDateRange(CDate fDate, CDate lDate) { m_fDate = fDate; m_lDate = lDate; }
  207. int SetRange(LPCSTR lpszDate);
  208. void GetParseDate(CString& str) const;
  209. };
  210. #endif