home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 February / PCWorld_2000-02_cd.bin / Software / TemaCD / tcvpa / data1.cab / MyFileGroup / INCLUDE / DateTime.hpp < prev    next >
C/C++ Source or Header  |  1999-06-03  |  11KB  |  251 lines

  1. #ifndef _INC_DATETIME_HPP
  2. #define _INC_DATETIME_HPP
  3. class TC_CDate;
  4. class TC_CTime;
  5. TC_COREEX_EXPORT TC_CString tcStr (TC_CDate & dt, char * fmt = 0)  ;
  6. TC_COREEX_EXPORT TC_CString tcStr (TC_CTime tm, char * fmt = 0)  ;
  7.  extern  TC_COREEX_EXPORT TC_CDate tcNullDate ;
  8.  extern  TC_COREEX_EXPORT TC_CTime tcNullTime ;
  9. /* 
  10.  *  %c    Country dependent date 
  11.  */ 
  12.  
  13. #define    DATEFMT_DATE_SEP        0x00000001L        // %-    Date separator
  14. #define    DATEFMT_ABBR_WEEKDAY    0x00000002L        // %a    Abbreviated weekday name 
  15. #define    DATEFMT_FULL_WEEKDAY    0x00000004L        // %A    Full weekday name 
  16. #define    DATEFMT_WEEKDAY_NUM        0x00000008L        // %w    Weekday as a decimal number (0-6; Sunday is 0) 
  17. #define    DATEFMT_ABBR_MONTH        0x00000010L        // %b   Abbreviated month name 
  18. #define    DATEFMT_FULL_MONTH        0x00000020L        // %B    Full month name 
  19. #define    DATEFMT_DAY_OF_MONTH    0x00000040L        // %d    Day of the month as a decimal number (01-31)
  20. #define    DATEFMT_DAY_OF_YEAR        0x00000080L        // %j    Day of the year as a decimal number (000-365) 
  21. #define    DATEFMT_MONTH_NUM        0x00000100L        // %m    Month as a decimal number (01-12)  
  22. #define    DATEFMT_YEAR4            0x00000200L        // %Y    Year with the century as a decimal number 
  23. #define    DATEFMT_YEAR2            0x00000400L        // %y    Year without the century as a decimal number (00-99)
  24. #define    DATEFMT_CURR_YEAR        0x00000800L        // %1    Current year if no year specified, otherwise 1900
  25. #define    DATEFMT_CURR_MONTH        0x00001000L        // %2   Current month if no month specified, otherwise 1st month
  26. #define    DATEFMT_CURR_DAY        0x00002000L        // %3    Current day if no day specified, otherwise 1st day
  27. #define    DATEFMT_CURRENT            0x00004000L        // %T    Current date/time component if one of them is not specified
  28. #define TC_INVALID_JULNUM    0xFFFFFFFFL
  29. #define TC_INVALID_SECONDS    0xFFFFFFFFL
  30.  
  31. // **********************************************************************
  32. class TC_COREEX_EXPORT TC_CDate 
  33. {
  34. friend TC_CTime;
  35. friend inline TC_CDate operator + (TC_CDate &dt, int dd)    { return TC_CDate(dt.m_Julnum + dd); }
  36. friend inline TC_CDate operator + (int dd, TC_CDate &dt)    { return TC_CDate(dt.m_Julnum + dd); }
  37. friend inline TC_CDate operator - (TC_CDate &dt, int dd)    { return TC_CDate(dt.m_Julnum - dd); }
  38. private:  ULONG m_Julnum ;
  39. public:   TC_CDate ()  ;
  40. public:   TC_CDate (const TC_CDate & dt) 
  41. {
  42. Set(((TC_CDate*)(&dt))->Get());
  43. } // end of func
  44. // **********************************************************************
  45.  
  46. public:   TC_CDate (int day, int year)  ;
  47. public:   TC_CDate ( int day, char * monthName, int year )  ;
  48. public:   TC_CDate ( int year, int month, int day )  ;
  49. public:   TC_CDate (const char * str, char * fmt = 0)  ;
  50. public:   TC_CDate (TC_CTime & t)  ;
  51. public:   TC_CDate ( ULONG j )  ;
  52. public: static int AssertIndexOfMonth (int m) 
  53. {
  54. return m>=1 && m<=12;
  55. } // end of func
  56. // **********************************************************************
  57.  
  58. public: static int AssertWeekDayNumber (int d) 
  59. {
  60. return d>=1 && d<=7;
  61. } // end of func
  62. // **********************************************************************
  63.  
  64. public: static char * DayName ( int weekDayNumber, BOOL IsShort = FALSE)  ;
  65. public: static int DayOfWeek ( char * nameOfDay, BOOL IsShort = FALSE)  ;
  66. public: static int FindMatch ( char * str, char ** candidates, int icand )  ;
  67. public: static int DayWithinMonth ( int year, int month, int day )  ;
  68. public: static int DaysInYear ( int year ) 
  69. {
  70. // How many days are in the given int year?
  71. return IsLeapYear(year) ? 366 : 365;
  72. } // end of func
  73. // **********************************************************************
  74.  
  75. public: static int DaysPerMonth (int year, int month)  ;
  76. public: static ULONG Jday ( int y, int m, int d )  ;
  77. public: static int IndexOfMonth ( char * nameOfMonth )  ;
  78. public: static BOOL IsLeapYear ( int year )  ;
  79. public: static char * MonthName ( int monthNumber )  ;
  80. public: static int HashValue (TC_CDate & d)  ;
  81. public:  TC_CDate & operator = (const TC_CDate & dt)  ;
  82. public:
  83.  
  84. int operator <     ( TC_CDate & date )    { return m_Julnum < date.m_Julnum; }
  85. int operator <=     ( TC_CDate & date )    { return m_Julnum <= date.m_Julnum; }
  86. int operator >     ( TC_CDate & date )    { return m_Julnum > date.m_Julnum; }
  87. int operator >=     ( TC_CDate & date )    { return m_Julnum >= date.m_Julnum; }
  88. int operator ==     ( TC_CDate & date )    { return m_Julnum == date.m_Julnum; }
  89. int operator !=     ( TC_CDate & date )    { return m_Julnum != date.m_Julnum; }
  90. ULONG operator - ( TC_CDate & dt )        { return m_Julnum - dt.m_Julnum; }
  91.  
  92. void operator ++ ()                    { m_Julnum += 1; }
  93. void operator ++ ( int )            { m_Julnum += 1; }
  94. void operator -- ()                    { m_Julnum -= 1; }
  95. void operator -- ( int )            { m_Julnum -= 1; }
  96. void operator += ( int dd )            { m_Julnum += dd; }
  97. void operator -= ( int dd )            { m_Julnum -= dd; }
  98. public:  int Between (TC_CDate & d1, TC_CDate & d2)  ;
  99. public:  int Day ()  ;
  100. public:  int DayOfMonth ()  ;
  101. public:  int DaysPerMonth ()  ;
  102. public:  int FirstDayOfMonth ( int month )  ;
  103. public:  int FirstDayOfMonth ()  ;
  104. public:  int FirstWeekDay ()  ;
  105. public:  int LastWeekDay ()  ;
  106. public:  BOOL IsNull ()  ;
  107. public:  void SetNull ()  ;
  108. public:  void SetInvalid () 
  109. {
  110. m_Julnum = TC_INVALID_JULNUM;
  111. } // end of func
  112. // **********************************************************************
  113.  
  114. public:  int IsValid () 
  115. {
  116. return m_Julnum != TC_INVALID_JULNUM;
  117. } // end of func
  118. // **********************************************************************
  119.  
  120. public:  BOOL IsLeapYear ()  ;
  121. public:  TC_CDate Max (TC_CDate & dt)  ;
  122. public:  TC_CDate Min (TC_CDate & dt)  ;
  123. public:  int Month ()  ;
  124. public:  char * NameOfDay (BOOL IsShort = FALSE)  ;
  125. public:  char * NameOfMonth ()  ;
  126. public:  void ChangeMonth (int delta, int day=0)  ;
  127. public:  int Compare (TC_CDate & d)  ;
  128. public:  int Hash ()  ;
  129. public:  TC_CDate Previous (char * dayName)  ;
  130. public:  TC_CDate Previous ( int desiredDayOfWeek )  ;
  131. public:  void Set (ULONG Jnum) 
  132. {
  133. m_Julnum = Jnum;
  134. } // end of func
  135. // **********************************************************************
  136.  
  137. public:  ULONG Get ()  ;
  138. public:  void SetDate ( int y, int m, int d )  ;
  139. public:  void SetDateTr ( int y, int m, int d )  ;
  140. public:  void SetDay (int d)  ;
  141. public:  void SetDayTr (int d)  ;
  142. public:  int WeekDay ()  ;
  143. public:  void YMD ( int & y, int & m, int & D )  ;
  144. public:  int Year ()  ;
  145. public:  void SetCurrent ()  ;
  146. public:  BOOL SetDate (const char * str = 0, const char * fmt = 0)  ;
  147. public:  TC_CString Format (const char * fmt = 0)  ;
  148. public:  BOOL Format (TC_CString &s, const char * fmt = 0)  ;
  149. public:  BOOL IsFmtValid (const char * fmt)  ;
  150.  
  151. }; // end of class TC_CDate
  152.  
  153. // **********************************************************************
  154. /* 
  155.  * %C    Country dependent format 
  156.  * %h    Hundredths of seconds 
  157.  * %I    Hour in 12-hour format (01-12) 
  158.  * %p    Current locale's AM/PM indicator for a 12-hour clock(followed by default PM hours) 
  159.  */ 
  160.  
  161. #define    TIMEFMT_TIME_SEP    0x00010000L        // %:    Time separator is next charater 
  162. #define    TIMEFMT_HOUR24        0x00020000L        // %H    Hour in 24-hour format (00-23) 
  163. #define    TIMEFMT_MINUTE        0x00040000L        // %M    Minute as a decimal number (00-59) 
  164. #define    TIMEFMT_SECOND        0x00080000L        // %S   Seconds as a decimal number (00-59) 
  165. #define    TIMEFMT_CURR_HOUR    0x00100000L        // %4    Current hour if no specified, otherwise 0
  166. #define    TIMEFMT_CURR_MINUTE    0x00200000L        // %5    Current minute if no specified, otherwise 0
  167. #define    TIMEFMT_CURR_SECOND    0x00400000L        // %6    Current second if no specified, otherwise 0
  168.  
  169. // **********************************************************************
  170. class TC_COREEX_EXPORT TC_CTime 
  171. {
  172. friend TC_CDate;
  173. public:
  174.  
  175. // Boolean operators.
  176. int TC_CTime::operator <  ( TC_CTime & t )    { return m_Sec < t.m_Sec; }
  177. int TC_CTime::operator <= ( TC_CTime & t )    { return m_Sec <= t.m_Sec; }
  178. int TC_CTime::operator >  ( TC_CTime & t )    { return m_Sec > t.m_Sec; }
  179. int TC_CTime::operator >= ( TC_CTime & t )    { return m_Sec >= t.m_Sec; }
  180. int TC_CTime::operator == ( TC_CTime & t )    { return m_Sec == t.m_Sec; }
  181. int TC_CTime::operator != ( TC_CTime & t )    { return m_Sec != t.m_Sec; }
  182.  
  183. // Add or subtract seconds.
  184. friend TC_CTime operator + ( TC_CTime & t, long s )    { return TC_CTime(t.m_Sec+s); }
  185. friend TC_CTime operator + ( long s, TC_CTime & t )    { return TC_CTime(t.m_Sec+s); }
  186. friend TC_CTime operator - ( TC_CTime & t, long s )    { return TC_CTime(t.m_Sec-s); }
  187. friend TC_CTime operator - ( long s, TC_CTime & t )    { return TC_CTime(t.m_Sec-s); }
  188.  
  189. void TC_CTime::operator ++ ()        { m_Sec += 1; }
  190. void TC_CTime::operator ++ ( int )    { m_Sec += 1; }
  191. void TC_CTime::operator -- ()        { m_Sec -= 1; }
  192. void TC_CTime::operator -- ( int )    { m_Sec -= 1; }
  193. void TC_CTime::operator += (long s)    { m_Sec += s; }
  194. void TC_CTime::operator -= (long s)    { m_Sec -= s; }
  195.  
  196. TC_CTime & TC_CTime::operator = (const TC_CTime & tm )     { m_Sec = ((TC_CTime*)(&tm))->Get(); return (*this); }
  197. private:  ULONG m_Sec ;
  198. protected:  static  TC_CDate m_RefDate ;
  199. protected:  static  TC_CDate m_MaxDate ;
  200. public:   TC_CTime ()  ;
  201. public:   TC_CTime ( int h, int m, int s = 0)  ;
  202. public:   TC_CTime (TC_CDate & date, int h=0, int m=0, int s=0)  ;
  203. public:   TC_CTime (TC_CTime & tm)  ;
  204. public:   TC_CTime (const char * str, char * fmt = 0)  ;
  205. public:   TC_CTime ( ULONG s )  ;
  206. public: static int AssertDate (TC_CDate & date)  ;
  207. public: static TC_CTime BeginDST (int year)  ;
  208. public: static TC_CTime BuildLocal (TC_CDate & date, int h)  ;
  209. public: static TC_CTime EndDST (int year)  ;
  210. public: static unsigned HashValue (TC_CTime & t)  ;
  211. public:  int Between (TC_CTime & a, TC_CTime & b)  ;
  212. public:  int Compare (TC_CTime & t)  ;
  213. public:  unsigned Hash ()  ;
  214. public:  int Hour ()  ;
  215. public:  int HourGMT ()  ;
  216. public:  ULONG Get ()  ;
  217. public:  void Set (ULONG secs)  ;
  218. public:  int IsDST ()  ;
  219. public:  void SetInvalid () 
  220. {
  221. m_Sec = TC_INVALID_SECONDS;
  222. } // end of func
  223. // **********************************************************************
  224.  
  225. public:  int IsValid () 
  226. {
  227. return m_Sec != TC_INVALID_SECONDS;
  228. } // end of func
  229. // **********************************************************************
  230.  
  231. public:  BOOL IsNull ()  ;
  232. public:  void SetNull ()  ;
  233. private:  ULONG LocalSecs ()  ;
  234. public:  TC_CTime Max (TC_CTime & t)  ;
  235. public:  TC_CTime Min (TC_CTime & t)  ;
  236. public:  int Minute ()  ;
  237. public:  int MinuteGMT ()  ;
  238. public:  ULONG Seconds ()  ;
  239. public:  ULONG Second ()  ;
  240. public:  void SetCurrent ()  ;
  241. public:  BOOL SetTime (const char * str = 0, const char * fmt = 0)  ;
  242. public:  TC_CString Format (const char * fmt = 0)  ;
  243. public:  BOOL Format (TC_CString &s, const char * fmt = 0)  ;
  244. public:  BOOL IsFmtValid (const char * fmt)  ;
  245.  
  246. }; // end of class TC_CTime
  247.  
  248. // **********************************************************************
  249.  
  250. #endif // _INC_DATETIME_HPP
  251.