Leaked source code of windows server 2003
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.

148 lines
3.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: time.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes: None.
  11. //
  12. // Functions: None.
  13. //
  14. // History: 09-Sep-95 EricB Created.
  15. // 01-Dec-95 MarkBl Split from util.cxx.
  16. //
  17. //----------------------------------------------------------------------------
  18. #include "..\pch\headers.hxx"
  19. #pragma hdrstop
  20. #include "..\inc\debug.hxx"
  21. #include "..\inc\time.hxx"
  22. //+-------------------------------------------------------------------------
  23. //
  24. // Function: IsLeapYear
  25. //
  26. // Synopsis: Determines if a given year is a leap year.
  27. //
  28. // Arguments: [wYear] - the year
  29. //
  30. // Returns: boolean value: TRUE == leap year
  31. //
  32. // History: 05-05-93 EricB
  33. //
  34. //--------------------------------------------------------------------------
  35. BOOL
  36. IsLeapYear(WORD wYear)
  37. {
  38. return wYear % 4 == 0 && wYear % 100 != 0 || wYear % 400 == 0;
  39. }
  40. //+----------------------------------------------------------------------------
  41. //
  42. // Function: IsValidDate
  43. //
  44. // Synopsis: Checks for valid values.
  45. //
  46. //-----------------------------------------------------------------------------
  47. BOOL
  48. IsValidDate(WORD wMonth, WORD wDay, WORD wYear)
  49. {
  50. if (wMonth < JOB_MIN_MONTH || wMonth > JOB_MAX_MONTH ||
  51. wDay < JOB_MIN_DAY)
  52. {
  53. return FALSE;
  54. }
  55. if (wMonth == JOB_MONTH_FEBRUARY && IsLeapYear(wYear))
  56. {
  57. if (wDay > (g_rgMonthDays[JOB_MONTH_FEBRUARY] + 1))
  58. {
  59. return FALSE;
  60. }
  61. }
  62. else
  63. {
  64. if (wDay > g_rgMonthDays[wMonth])
  65. {
  66. return FALSE;
  67. }
  68. }
  69. return TRUE;
  70. }
  71. //+----------------------------------------------------------------------------
  72. //
  73. // Function: MonthDays
  74. //
  75. // Synopsis: Returns the number of days in the indicated month.
  76. //
  77. // Arguments: [wMonth] - Index of the month in question where January = 1
  78. // through December equalling 12.
  79. // [yYear] - If non-zero, then leap year adjustment for February
  80. // will be applied.
  81. // [pwDays] - The place to return the number of days in the
  82. // indicated month.
  83. //
  84. // Returns: S_OK or E_INVALIDARG
  85. //
  86. // History: 10-29-93 EricB
  87. //
  88. //-----------------------------------------------------------------------------
  89. HRESULT
  90. MonthDays(WORD wMonth, WORD wYear, WORD *pwDays)
  91. {
  92. if (wMonth < JOB_MIN_MONTH || wMonth > JOB_MAX_MONTH)
  93. {
  94. return E_INVALIDARG;
  95. }
  96. *pwDays = g_rgMonthDays[wMonth];
  97. //
  98. // If February, adjust for leap years
  99. //
  100. if (wMonth == 2 && wYear != 0)
  101. {
  102. if (IsLeapYear(wYear))
  103. {
  104. (*pwDays)++;
  105. }
  106. }
  107. return S_OK;
  108. }
  109. //+----------------------------------------------------------------------------
  110. //
  111. // Function: IncrementDay
  112. //
  113. // Synopsis: increases the SYSTEMTIME day value by one and corrects for
  114. // overflow
  115. //
  116. // Arguments: [pst] - the date to increment
  117. //
  118. //-----------------------------------------------------------------------------
  119. void
  120. IncrementDay(LPSYSTEMTIME pst)
  121. {
  122. pst->wDay++;
  123. WORD wLastDay;
  124. HRESULT hr = MonthDays(pst->wMonth, pst->wYear, &wLastDay);
  125. if (FAILED(hr))
  126. {
  127. schAssert(!"Bad systemtime");
  128. }
  129. else
  130. {
  131. if (pst->wDay > wLastDay)
  132. {
  133. //
  134. // Wrap to the next month.
  135. //
  136. pst->wDay = 1;
  137. IncrementMonth(pst);
  138. }
  139. }
  140. }