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.

102 lines
2.4 KiB

  1. /********************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1990-1991 **/
  4. /********************************************************************/
  5. /* :ts=4 */
  6. /*** convtime.cpp - map between SYSTEM & NET time formats
  7. */
  8. #include "npcommon.h"
  9. #include <convtime.h>
  10. void
  11. NetToSystemDate(
  12. DWORD time,
  13. LPSYSTEMTIME pinfo) // ptr for return data:
  14. {
  15. UINT secs, days;
  16. WORD r;
  17. // Base the time on 1980, not 1970, to make leap year calculation
  18. // easier -- 1980 is a leap year, but 1970 isn't. This code is being
  19. // written in 1996, so we aren't going to be dealing with dates before
  20. // 1980 anyway.
  21. time -= _70_to_80_bias; // # of seconds since 1980
  22. secs = time % SECS_IN_DAY; // seconds into day
  23. days = time / SECS_IN_DAY; // days since Jan 1 1980
  24. pinfo->wDayOfWeek = (days + 2) % 7; // Jan 1 1980 was a Tuesday, hence "+2"
  25. pinfo->wMilliseconds = 0;
  26. pinfo->wSecond = secs % 60; // # of seconds
  27. secs /= 60;
  28. pinfo->wMinute = secs % 60; // # of minutes
  29. pinfo->wHour = secs / 60; // # of hours
  30. r = days / FOURYEARS; // (r) = four year period past 1980
  31. days %= FOURYEARS; // (days) = days into four year period
  32. r *= 4; // (r) = years since 1980 (within 3)
  33. if (days == 31+28) { // this many days into a 4-year period is feb 29
  34. //* Special case for FEB 29th
  35. pinfo->wDay = 29;
  36. pinfo->wMonth = 2;
  37. } else {
  38. if (days > 31+28)
  39. --days; // compensate for leap year
  40. while (days >= 365) {
  41. ++r;
  42. days -= 365;
  43. }
  44. for (secs = 1; days >= MonTotal[secs+1] ; ++secs)
  45. ;
  46. days -= MonTotal[secs];
  47. pinfo->wDay = days + 1;
  48. pinfo->wMonth = (unsigned short) secs;
  49. }
  50. pinfo->wYear = r + 1980;
  51. }
  52. DWORD
  53. SystemToNetDate(LPSYSTEMTIME pinfo)
  54. {
  55. UINT days, secs;
  56. days = pinfo->wYear - 1980;
  57. days = days*365 + days/4; // # of years in days
  58. days += pinfo->wDay + MonTotal[pinfo->wMonth];
  59. if (!(pinfo->wYear % 4)
  60. && pinfo->wMonth <= 2)
  61. --days; // adjust days for early in leap year
  62. secs = (((pinfo->wHour * 60) + pinfo->wMinute) * 60) + pinfo->wSecond;
  63. return days*SECS_IN_DAY + _70_to_80_bias + secs;
  64. }
  65. DWORD
  66. GetCurrentNetDate(void)
  67. {
  68. SYSTEMTIME st;
  69. GetSystemTime(&st);
  70. return SystemToNetDate(&st);
  71. }
  72. DWORD
  73. GetLocalNetDate(void)
  74. {
  75. SYSTEMTIME st;
  76. GetLocalTime(&st);
  77. return SystemToNetDate(&st);
  78. }