Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
7.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: time.hxx
  7. //
  8. // Contents: Time/date common routines.
  9. //
  10. // History: 08-Sep-95 EricB Created.
  11. // 01-Dec-95 MarkBl Split from util.hxx.
  12. //
  13. //----------------------------------------------------------------------------
  14. #ifndef __TIME_HXX__
  15. #define __TIME_HXX__
  16. //#include "runobj.hxx"
  17. //
  18. // Time/date constants
  19. //
  20. const WORD JOB_MIN_MONTH = 1;
  21. const WORD JOB_MAX_MONTH = 12;
  22. const WORD JOB_MONTHS_PER_YEAR = JOB_MAX_MONTH ;
  23. const WORD JOB_MIN_DAY = 1;
  24. const WORD JOB_MAX_DAY = 31;
  25. const WORD JOB_DAYS_PER_MONTHMAX = JOB_MAX_DAY;
  26. const WORD JOB_MIN_HOUR = 0;
  27. const WORD JOB_MAX_HOUR = 23;
  28. const WORD JOB_MIN_MINUTE = 0;
  29. const WORD JOB_MAX_MINUTE = 59;
  30. const WORD JOB_MILLISECONDS_PER_SECOND = 1000;
  31. const WORD JOB_MILLISECONDS_PER_MINUTE = 60 * JOB_MILLISECONDS_PER_SECOND;
  32. const WORD JOB_MINS_PER_HOUR = 60;
  33. const WORD JOB_HOURS_PER_DAY = 24;
  34. const WORD JOB_MINS_PER_DAY = JOB_HOURS_PER_DAY * JOB_MINS_PER_HOUR;
  35. const WORD JOB_DAYS_PER_WEEK = 7;
  36. const DWORD JOB_RGFDAYS_MAX = 0x7fffffff;
  37. const WORD JOB_RGFDOW_MAX = 0x7f;
  38. const WORD JOB_RGFMONTHS_MAX = 0xfff;
  39. const WORD JOB_MONTH_FEBRUARY = 2;
  40. const int JOB_TIMEBUF_LEN = 40;
  41. const short MAX_INTERVAL_MINUTES = 720;
  42. const short MAX_INTERVAL_HOURS = 12;
  43. const unsigned long FILETIMES_PER_MILLISECOND = 10000;
  44. const DWORD FILETIMES_PER_SECOND = JOB_MILLISECONDS_PER_SECOND *
  45. FILETIMES_PER_MILLISECOND;
  46. const DWORD FILETIMES_PER_MINUTE = FILETIMES_PER_MILLISECOND *
  47. JOB_MILLISECONDS_PER_MINUTE;
  48. //
  49. // BUGBUG: neither of these are currently working without CRT initialization.
  50. // BUG # 37752.
  51. //
  52. //const __int64 FILETIMES_PER_HOUR = (__int64)FILETIMES_PER_MINUTE
  53. // * JOB_MINS_PER_HOUR;
  54. //const __int64 FILETIMES_PER_DAY = FILETIMES_PER_HOUR * JOB_HOURS_PER_DAY;
  55. //
  56. // Logically, this should be MAX_FILETIME = { MAXDWORD, MAXDWORD };
  57. // but CompareFileTime on NT 4 is broken for FILETIMEs larger than the
  58. // one below (NT bug 88901).
  59. // Also, the FILETIME below is the largest FILETIME accepted by
  60. // FileTimeToSystemTime().
  61. //
  62. const FILETIME MAX_FILETIME = { MAXULONG, 0x7fffffff };
  63. const BYTE g_rgMonthDays[] =
  64. {
  65. 0, // make January's index 1
  66. 31, // January
  67. 28, // February
  68. 31, // March
  69. 30, // April
  70. 31, // May
  71. 30, // June
  72. 31, // July
  73. 31, // August
  74. 30, // September
  75. 31, // October
  76. 30, // November
  77. 31 // December
  78. };
  79. //
  80. // Time/date common routines.
  81. //
  82. HRESULT MonthDays(WORD wMonth, WORD wYear, WORD *pwDays);
  83. BOOL IsLeapYear(WORD wYear);
  84. BOOL IsValidDate(WORD wMonth, WORD wDay, WORD wYear);
  85. //+----------------------------------------------------------------------------
  86. //
  87. // Function: IsFirstDateEarlier
  88. //
  89. // Synopsis: Compares the date portion of the two params
  90. //
  91. // Arguments: [pstFirst, pstSecond] - the dates to compare
  92. //
  93. // Returns: TRUE if the first date is earlier than the second
  94. //
  95. //-----------------------------------------------------------------------------
  96. inline BOOL
  97. IsFirstDateEarlier(const SYSTEMTIME * pstFirst, const SYSTEMTIME * pstSecond)
  98. {
  99. if (pstFirst->wYear > pstSecond->wYear ||
  100. (pstFirst->wYear == pstSecond->wYear &&
  101. (pstFirst->wMonth > pstSecond->wMonth ||
  102. (pstFirst->wMonth == pstSecond->wMonth &&
  103. pstFirst->wDay >= pstSecond->wDay))))
  104. {
  105. return FALSE;
  106. }
  107. return TRUE;
  108. }
  109. //+----------------------------------------------------------------------------
  110. //
  111. // Function: IsFirstTimeEarlier
  112. //
  113. // Synopsis: Compares two SYSTEMTIMES
  114. //
  115. // Arguments: [pstFirst, pstSecond] - the dates to compare
  116. //
  117. // Returns: TRUE if the first date-time is earlier than the second
  118. //
  119. //-----------------------------------------------------------------------------
  120. inline BOOL
  121. IsFirstTimeEarlier(const SYSTEMTIME *pstFirst, const SYSTEMTIME *pstSecond)
  122. {
  123. FILETIME ftFirst, ftSecond;
  124. SystemTimeToFileTime(pstFirst, &ftFirst);
  125. SystemTimeToFileTime(pstSecond, &ftSecond);
  126. return (CompareFileTime(&ftFirst, &ftSecond) < 0);
  127. }
  128. //+----------------------------------------------------------------------------
  129. //
  130. // Function: IncrementMonth
  131. //
  132. // Synopsis: increases the SYSTEMTIME month value by one and corrects for
  133. // overflow
  134. //
  135. // Arguments: [pst] - the date to increment
  136. //
  137. //-----------------------------------------------------------------------------
  138. inline void
  139. IncrementMonth(LPSYSTEMTIME pst)
  140. {
  141. if (pst->wMonth < JOB_MONTHS_PER_YEAR)
  142. {
  143. pst->wMonth++;
  144. }
  145. else
  146. {
  147. pst->wMonth = 1;
  148. pst->wYear++;
  149. }
  150. }
  151. //+----------------------------------------------------------------------------
  152. //
  153. // Function: IncrementDau
  154. //
  155. // Synopsis: increases the SYSTEMTIME day value by one and corrects for
  156. // overflow
  157. //
  158. // Arguments: [pst] - the date to increment
  159. //
  160. //-----------------------------------------------------------------------------
  161. void
  162. IncrementDay(LPSYSTEMTIME pst);
  163. #ifdef YANK
  164. //+----------------------------------------------------------------------------
  165. //
  166. // Gregorian calendar functions
  167. //
  168. //-----------------------------------------------------------------------------
  169. const WORD GREG_MAXMONTH = 12;
  170. const WORD GREG_MINMONTH = 1;
  171. const WORD JUL_DOW_MONDAY = 0;
  172. const WORD JUL_DOW_TUESDAY = 1;
  173. const WORD JUL_DOW_WEDNESDAY = 2;
  174. const WORD JUL_DOW_THURSDAY = 3;
  175. const WORD JUL_DOW_FRIDAY = 4;
  176. const WORD JUL_DOW_SATURDAY = 5;
  177. const WORD JUL_DOW_SUNDAY = 6;
  178. WORD DayOfWeek(long lDayNum);
  179. void MakeDate(long lDayNum, WORD *pwMonth, WORD *pwDay, WORD *pwYear);
  180. HRESULT MakeJulianDayNumber(WORD wMonth, WORD wDay, WORD wYear, long *plDayNum);
  181. //
  182. // Forward declaration.
  183. //
  184. class CTimeList;
  185. //+----------------------------------------------------------------------------
  186. //
  187. // Time list node class
  188. //
  189. //-----------------------------------------------------------------------------
  190. class CTimeNode : public CDLink
  191. {
  192. friend CTimeList;
  193. public:
  194. CTimeNode(LPSYSTEMTIME pst) :
  195. m_dwMaxRunTime(MAX_RUN_TIME_DEFAULT)
  196. {SystemTimeToFileTime(pst, &m_ft);}
  197. CTimeNode(LPFILETIME pft) :
  198. m_ft(*pft),
  199. m_dwMaxRunTime(MAX_RUN_TIME_DEFAULT) {}
  200. CTimeNode(LPFILETIME pft, DWORD MaxRunTime) :
  201. m_ft(*pft),
  202. m_dwMaxRunTime(MaxRunTime) {}
  203. CTimeNode(void) {m_ft.dwLowDateTime = 0;
  204. m_ft.dwHighDateTime = 0; m_dwMaxRunTime = RUN_TIME_NO_END;}
  205. ~CTimeNode() {}
  206. CTimeNode * Next(void) {return((CTimeNode *)CDLink::Next());}
  207. CTimeNode * Prev(void) {return((CTimeNode *)CDLink::Prev());}
  208. private:
  209. };
  210. //+----------------------------------------------------------------------------
  211. //
  212. // Time/job list class
  213. //
  214. //-----------------------------------------------------------------------------
  215. class CTimeList
  216. {
  217. public:
  218. CTimeList() : m_ptnHead(NULL) {};
  219. ~CTimeList() {};
  220. HRESULT SetList(SYSTEMTIME rgst[], WORD cList);
  221. void Clear(void);
  222. void Add(CTimeNode * pte);
  223. HRESULT Add(LPSYSTEMTIME pst);
  224. HRESULT Add(LPFILETIME pft);
  225. HRESULT PeekHeadTime(LPFILETIME pft);
  226. CTimeNode * Pop(void);
  227. void Merge(CTimeList * ptl);
  228. private:
  229. CTimeNode * m_ptnHead;
  230. };
  231. #endif // YANK
  232. #endif // __TIME_HXX__