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.

171 lines
3.6 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. Implementation of various time APIs
  6. Revision History:
  7. Author:
  8. Arnold Miller (ArnoldM) 18-Oct-1997
  9. --*/
  10. #include "fltapis.hxx"
  11. #include <winsock2.h>
  12. #include <time.h>
  13. //
  14. // The system time for 0h 1900
  15. //
  16. LARGE_INTEGER li1900 = {0xfde04000,
  17. 0x14f373b};
  18. EXTERNCDECL
  19. DWORD
  20. WINAPI
  21. NTTimeToNTPTime(PULONG pTime,
  22. PFILETIME pft OPTIONAL)
  23. /*++
  24. Routine Description:
  25. Convert NT filetime to NTP time. If no time provided use
  26. the current time.
  27. --*/
  28. {
  29. #ifdef CHICAGO
  30. return ERROR_NOT_SUPPORTED;
  31. #else
  32. LARGE_INTEGER liTime;
  33. DWORD dwMs;
  34. //
  35. // put the current time into the quadword pointed to by the arg
  36. // The argument points to two consecutive ULONGs in which the
  37. // time in seconds goes into the first work and the fractional
  38. // time in seconds in the second word.
  39. if(!pft)
  40. {
  41. GetSystemTimeAsFileTime((LPFILETIME)&liTime);
  42. }
  43. else
  44. {
  45. *(LPFILETIME)&liTime = *pft;
  46. }
  47. //
  48. // Seconds is simply the time difference
  49. //
  50. *pTime = htonl((ULONG)((liTime.QuadPart - li1900.QuadPart) / 10000000));
  51. //
  52. // Ms is the residue from the seconds calculation.
  53. //
  54. dwMs = (DWORD)(((liTime.QuadPart - li1900.QuadPart) % 10000000) / 10000);
  55. //
  56. // time base in the beginning of the year 1900
  57. //
  58. *(1 + pTime) = htonl((unsigned long)
  59. (.5+0xFFFFFFFF*(double)(dwMs/1000.0)));
  60. return(ERROR_SUCCESS);
  61. #endif
  62. }
  63. EXTERNCDECL
  64. DWORD
  65. WINAPI
  66. NTPTimeToNTFileTime(PLONG pTime, PFILETIME pft, BOOL bHostOrder)
  67. /*++
  68. Routine Description:
  69. Convert NTP timestamp to NT filetime.
  70. --*/
  71. {
  72. #ifdef CHICAGO
  73. return ERROR_NOT_SUPPORTED;
  74. #else
  75. time_t lmaj;
  76. LONG lmin;
  77. struct tm *newtime;
  78. SYSTEMTIME nt;
  79. if(bHostOrder)
  80. {
  81. lmaj = *pTime;
  82. lmin = *(pTime + 1);
  83. }
  84. else
  85. {
  86. lmaj = ntohl(*pTime);
  87. lmin = ntohl(*(pTime + 1));
  88. }
  89. //
  90. // convert time since 1970 to time since 1900.
  91. //
  92. lmaj -= (time_t)(365.2422*70*24*60*60+3974.4);
  93. //
  94. // get a time structure based on the seconds part of the NTP time.
  95. //
  96. newtime = gmtime(&lmaj);
  97. if(!newtime)
  98. {
  99. return(GetLastError());
  100. }
  101. //
  102. // marshall this into a SYSTEMTIME
  103. //
  104. nt.wYear=(WORD)(newtime->tm_year+1900);
  105. nt.wMonth=(WORD)(newtime->tm_mon+1);
  106. nt.wDay=(WORD)newtime->tm_mday;
  107. nt.wHour=(WORD)newtime->tm_hour;
  108. nt.wMinute=(WORD)newtime->tm_min;
  109. nt.wSecond=(WORD)newtime->tm_sec;
  110. //
  111. // compute ms part from the fractional value of the NTP time
  112. //
  113. nt.wMilliseconds=(WORD)(.5+1000*(double)lmin /
  114. (unsigned int)0xFFFFFFFF);
  115. if (nt.wMilliseconds > 999)
  116. {
  117. #if 0
  118. //
  119. // this is more accurate but it can also produce a failure
  120. // and since this isn't supposed to happen anway, it is
  121. // likely failure will ensue. So just truncate the ms field
  122. //
  123. nt.wMilliseconds -= 1000;
  124. if (++nt.wSecond > 59)
  125. {
  126. nt.wSecond = 0;
  127. if (++nt.wMinute > 59)
  128. {
  129. nt.wMinute = 0;
  130. if (++nt.wHour > 23)
  131. {
  132. return(ERROR_INVALID_TIME);
  133. }
  134. }
  135. }
  136. #else
  137. nt.wMilliseconds = 999;
  138. #endif
  139. }
  140. return(SystemTimeToFileTime(&nt, pft));
  141. #endif
  142. }