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.

142 lines
2.2 KiB

  1. /*
  2. * Gutils -
  3. *
  4. * - date conversion functions
  5. *
  6. * Geraint, 22 May, 28 Oct 91.
  7. */
  8. #include "windows.h"
  9. #include "gutils.h"
  10. #include <string.h>
  11. BOOL gdi_isleap(LONG year);
  12. /*---static data--------------------------------------------*/
  13. int monthdays[] = {
  14. 31,
  15. 28,
  16. 31,
  17. 30,
  18. 31,
  19. 30,
  20. 31,
  21. 31,
  22. 30,
  23. 31,
  24. 30,
  25. 31
  26. };
  27. /*--public functions--------------------------------------*/
  28. void APIENTRY
  29. gdate_daytodmy(LONG days, int FAR* yrp, int FAR* monthp, int FAR* dayp)
  30. {
  31. int years;
  32. int nleaps;
  33. int month;
  34. int mdays;
  35. /* get number of completed years and calc leap days */
  36. years = (int) (days / 365);
  37. days = days % 365;
  38. nleaps = (years / 4) - (years / 100) + (years / 400);
  39. while (nleaps > days) {
  40. days += 365;
  41. years--;
  42. nleaps = (years / 4) - (years / 100) + (years / 400);
  43. }
  44. days -= nleaps;
  45. /* add one year for current (non-complete) year */
  46. years++;
  47. /* current month */
  48. for (month = 0; month < 12; month++) {
  49. mdays = monthdays[month];
  50. if (gdi_isleap(years) && (month == 1)) {
  51. mdays++;
  52. }
  53. if (days == mdays) {
  54. days = 0;
  55. month++;
  56. break;
  57. } else if (days < mdays) {
  58. break;
  59. } else {
  60. days -= mdays;
  61. }
  62. }
  63. /* conv month from 0-11 to 1-12 */
  64. if (monthp != NULL) {
  65. *monthp = month+1;
  66. }
  67. if (dayp != NULL) {
  68. *dayp = (int) days + 1;
  69. }
  70. if (yrp != NULL) {
  71. *yrp = years;
  72. }
  73. }
  74. LONG APIENTRY
  75. gdate_dmytoday(int yr, int month, int day)
  76. {
  77. int nleaps;
  78. int i;
  79. long ndays;
  80. /* exclude the current year */
  81. yr--;
  82. nleaps = (yr / 4) - (yr / 100) + (yr / 400);
  83. /* in any given year, day 0 is jan1 */
  84. month--;
  85. day--;
  86. ndays = 0;
  87. for (i = 0; i < month ; i++) {
  88. ndays += monthdays[i];
  89. if (gdi_isleap(yr+1) && (i == 1)) {
  90. ndays++;
  91. }
  92. }
  93. ndays = ndays + day + nleaps + (yr * 365L);
  94. return(ndays);
  95. }
  96. int APIENTRY
  97. gdate_monthdays(int month, int year)
  98. {
  99. int ndays;
  100. ndays = monthdays[month - 1];
  101. if (gdi_isleap(year) && (month == 2)) {
  102. ndays++;
  103. }
  104. return(ndays);
  105. }
  106. int APIENTRY
  107. gdate_weekday(long daynr)
  108. {
  109. return((int) ((daynr + 1) % 7));
  110. }
  111. /* internal functions-----------------------------------------*/
  112. BOOL
  113. gdi_isleap(LONG year)
  114. {
  115. if ( ((year % 4) == 0) &&
  116. (((year % 100) != 0) ||
  117. ((year % 400) == 0))) {
  118. return TRUE;
  119. } else {
  120. return FALSE;
  121. }
  122. }