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.

185 lines
4.1 KiB

  1. /****************************** Module Header *******************************
  2. * Module Name: GDATE.C
  3. *
  4. * Contains date conversion functions.
  5. *
  6. * Functions:
  7. *
  8. * gdi_isleap()
  9. * gdate_daytodmy()
  10. * gdate_dmytoday()
  11. * gdate_monthdays()
  12. * gdate_weeklyday()
  13. *
  14. * Comments: This code stolen from windiff.exe
  15. *
  16. ****************************************************************************/
  17. #include <windows.h>
  18. #include <string.h>
  19. //#include "gutils.h"
  20. BOOL gdi_isleap(LONG year);
  21. /*---static data--------------------------------------------*/
  22. int monthdays[] = {
  23. 31,
  24. 28,
  25. 31,
  26. 30,
  27. 31,
  28. 30,
  29. 31,
  30. 31,
  31. 30,
  32. 31,
  33. 30,
  34. 31
  35. };
  36. /***************************************************************************
  37. * Function: gdate_daytomy
  38. *
  39. * Purpose:
  40. *
  41. * converts day to d/m/y
  42. */
  43. void APIENTRY
  44. gdate_daytodmy(LONG days, int FAR* yrp, int FAR* monthp, int FAR* dayp)
  45. {
  46. int years;
  47. int nleaps;
  48. int month;
  49. int mdays;
  50. /* get number of completed years and calc leap days */
  51. years = (int) (days / 365);
  52. days = days % 365;
  53. nleaps = (years / 4) - (years / 100) + (years / 400);
  54. while (nleaps > days) {
  55. days += 365;
  56. years--;
  57. nleaps = (years / 4) - (years / 100) + (years / 400);
  58. }
  59. days -= nleaps;
  60. /* add one year for current (non-complete) year */
  61. years++;
  62. /* current month */
  63. for (month = 0; month < 12; month++) {
  64. mdays = monthdays[month];
  65. if (gdi_isleap(years) && (month == 1)) {
  66. mdays++;
  67. }
  68. if (days == mdays) {
  69. days = 0;
  70. month++;
  71. break;
  72. } else if (days < mdays) {
  73. break;
  74. } else {
  75. days -= mdays;
  76. }
  77. }
  78. /* conv month from 0-11 to 1-12 */
  79. if (monthp != NULL) {
  80. *monthp = month+1;
  81. }
  82. if (dayp != NULL) {
  83. *dayp = (int) days + 1;
  84. }
  85. if (yrp != NULL) {
  86. *yrp = years;
  87. }
  88. }
  89. /***************************************************************************
  90. * Function: gdate_dmytoday
  91. *
  92. * Purpose:
  93. *
  94. * converts d/m/y to a day
  95. */
  96. LONG APIENTRY
  97. gdate_dmytoday(int yr, int month, int day)
  98. {
  99. int nleaps;
  100. int i;
  101. long ndays;
  102. /* exclude the current year */
  103. yr--;
  104. nleaps = (yr / 4) - (yr / 100) + (yr / 400);
  105. /* in any given year, day 0 is jan1 */
  106. month--;
  107. day--;
  108. ndays = 0;
  109. for (i = 0; i < month ; i++) {
  110. ndays += monthdays[i];
  111. if (gdi_isleap(yr+1) && (i == 1)) {
  112. ndays++;
  113. }
  114. }
  115. ndays = ndays + day + nleaps + (yr * 365L);
  116. return(ndays);
  117. }
  118. /***************************************************************************
  119. * Function: gdate_monthdays
  120. *
  121. * Purpose:
  122. *
  123. * Gets number of days in month
  124. */
  125. int APIENTRY
  126. gdate_monthdays(int month, int year)
  127. {
  128. int ndays;
  129. ndays = monthdays[month - 1];
  130. if (gdi_isleap(year) && (month == 2)) {
  131. ndays++;
  132. }
  133. return(ndays);
  134. }
  135. /***************************************************************************
  136. * Function: gdate_weekday
  137. *
  138. * Purpose:
  139. *
  140. * Gets the day of the week
  141. */
  142. int APIENTRY
  143. gdate_weekday(long daynr)
  144. {
  145. return((int) ((daynr + 1) % 7));
  146. }
  147. /***************************************************************************
  148. * Function: gdi_isleap
  149. *
  150. * Purpose:
  151. *
  152. * Determines whether the year is a leap year
  153. */
  154. BOOL
  155. gdi_isleap(LONG year)
  156. {
  157. if ( ((year % 4) == 0) &&
  158. (((year % 100) != 0) ||
  159. ((year % 400) == 0))) {
  160. return TRUE;
  161. } else {
  162. return FALSE;
  163. }
  164. }