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.

172 lines
3.4 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. LONGLONG Increment;
  34. DWORD dwMs;
  35. //
  36. // put the current time into the quadword pointed to by the arg
  37. // The argument points to two consecutive ULONGs in which the
  38. // time in seconds goes into the first work and the fractional
  39. // time in seconds in the second word.
  40. if(!pft)
  41. {
  42. GetSystemTimeAsFileTime((LPFILETIME)&liTime);
  43. }
  44. else
  45. {
  46. *(LPFILETIME)&liTime = *pft;
  47. }
  48. //
  49. // Seconds is simply the time difference
  50. //
  51. *pTime = htonl((ULONG)((liTime.QuadPart - li1900.QuadPart) / 10000000));
  52. //
  53. // Ms is the residue from the seconds calculation.
  54. //
  55. dwMs = (DWORD)(((liTime.QuadPart - li1900.QuadPart) % 10000000) / 10000);
  56. //
  57. // time base in the beginning of the year 1900
  58. //
  59. *(1 + pTime) = htonl((unsigned long)
  60. (.5+0xFFFFFFFF*(double)(dwMs/1000.0)));
  61. return(ERROR_SUCCESS);
  62. #endif
  63. }
  64. EXTERNCDECL
  65. DWORD
  66. WINAPI
  67. NTPTimeToNTFileTime(PLONG pTime, PFILETIME pft, BOOL bHostOrder)
  68. /*++
  69. Routine Description:
  70. Convert NTP timestamp to NT filetime.
  71. --*/
  72. {
  73. #ifdef CHICAGO
  74. return ERROR_NOT_SUPPORTED;
  75. #else
  76. time_t lmaj;
  77. LONG lmin;
  78. struct tm *newtime;
  79. SYSTEMTIME nt;
  80. if(bHostOrder)
  81. {
  82. lmaj = *pTime;
  83. lmin = *(pTime + 1);
  84. }
  85. else
  86. {
  87. lmaj = ntohl(*pTime);
  88. lmin = ntohl(*(pTime + 1));
  89. }
  90. //
  91. // convert time since 1970 to time since 1900.
  92. //
  93. lmaj -= (time_t)(365.2422*70*24*60*60+3974.4);
  94. //
  95. // get a time structure based on the seconds part of the NTP time.
  96. //
  97. newtime = gmtime(&lmaj);
  98. if(!newtime)
  99. {
  100. return(GetLastError());
  101. }
  102. //
  103. // marshall this into a SYSTEMTIME
  104. //
  105. nt.wYear=(WORD)(newtime->tm_year+1900);
  106. nt.wMonth=(WORD)(newtime->tm_mon+1);
  107. nt.wDay=(WORD)newtime->tm_mday;
  108. nt.wHour=(WORD)newtime->tm_hour;
  109. nt.wMinute=(WORD)newtime->tm_min;
  110. nt.wSecond=(WORD)newtime->tm_sec;
  111. //
  112. // compute ms part from the fractional value of the NTP time
  113. //
  114. nt.wMilliseconds=(WORD)(.5+1000*(double)lmin /
  115. (unsigned int)0xFFFFFFFF);
  116. if (nt.wMilliseconds > 999)
  117. {
  118. #if 0
  119. //
  120. // this is more accurate but it can also produce a failure
  121. // and since this isn't supposed to happen anway, it is
  122. // likely failure will ensue. So just truncate the ms field
  123. //
  124. nt.wMilliseconds -= 1000;
  125. if (++nt.wSecond > 59)
  126. {
  127. nt.wSecond = 0;
  128. if (++nt.wMinute > 59)
  129. {
  130. nt.wMinute = 0;
  131. if (++nt.wHour > 23)
  132. {
  133. return(ERROR_INVALID_TIME);
  134. }
  135. }
  136. }
  137. #else
  138. nt.wMilliseconds = 999;
  139. #endif
  140. }
  141. return(SystemTimeToFileTime(&nt, pft));
  142. #endif
  143. }