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.

171 lines
4.3 KiB

  1. /* --------------------------------------------------------------------------
  2. timeconv.cpp
  3. Functions to perform various time conversion operations.
  4. Copyright (C) 1994, Microsoft Corporation.
  5. All rights reserved.
  6. Author:
  7. Lindsay Harris - lindsayh
  8. -------------------------------------------------------------------------- */
  9. #include "smtpinc.h"
  10. #include <time.h>
  11. #include <string.h>
  12. #include <dbgtrace.h>
  13. #include "timeconv.h"
  14. /*
  15. * A handcrafted time zone string to GMT offsets table. This is not
  16. * a very good way to handle this.
  17. */
  18. static struct
  19. {
  20. int iTZOffset; // Arithmetic offset from GMT, in seconds.
  21. char rgchTZName[ 4 ]; // String representation of time zone.
  22. } _TZ_NAME[] =
  23. {
  24. { 0, { 'G', 'M', 'T', '\0' } },
  25. { 0, { 'U', 'T', 'C', '\0' } },
  26. { 0, { 'U', 'T', '\0', '\0' } },
  27. { -14400, { 'E', 'D', 'T', '\0' } },
  28. { -18000, { 'E', 'S', 'T', '\0' } },
  29. { -18000, { 'C', 'D', 'T', '\0' } },
  30. { -21600, { 'C', 'S', 'T', '\0' } },
  31. { -21600, { 'M', 'D', 'T', '\0' } },
  32. { -25200, { 'M', 'S', 'T', '\0' } },
  33. { -25200, { 'P', 'D', 'T', '\0' } },
  34. { -28800, { 'P', 'S', 'T', '\0' } },
  35. { 43200, { 'N', 'Z', 'S', '\0' } }, // NZ standard time.
  36. { 46800, { 'N', 'Z', 'D', '\0' } },
  37. };
  38. #define NUM_TZ (sizeof( _TZ_NAME ) / sizeof( _TZ_NAME[ 0 ] ))
  39. // The date Jan 1, 1970 00:00:00 in type FILETIME
  40. #define ft1970high 27111902
  41. #define ft1970low 3577643008
  42. static FILETIME ft1970 = {ft1970low, ft1970high};
  43. // The number of FILETIME units (100's of nanoseconds) in a time_t unit (seconds)
  44. #define dFiletimePerDTime_t 10000000
  45. /*
  46. * English language month table.
  47. */
  48. static char *rgchMonth[ 12 ] =
  49. {
  50. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  51. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  52. };
  53. /* -----------------------------------------------------------------------
  54. GetSysAndFileTimeAsString --s tring should be atleast 64 bytes
  55. ----------------------------------------------------------------------- */
  56. void GetSysAndFileTimeAsString( char *achReturn )
  57. {
  58. SYSTEMTIME stUTC; // The current time in UTC/GMT
  59. GetSystemTime( &stUTC );
  60. FILETIME ft;
  61. // Convert from SYSTEMTIME to FILETIME
  62. SystemTimeToFileTime(&stUTC, &ft);
  63. /*
  64. * No major trickery here. We have all the data, so simply
  65. * format it according to the rules on how to do this.
  66. */
  67. wsprintf( achReturn, "%02d %s %04d %02d:%02d:%02d.%04d (UTC) FILETIME=[%08X:%08X]",
  68. stUTC.wDay,
  69. rgchMonth[ stUTC.wMonth - 1 ],
  70. stUTC.wYear,
  71. stUTC.wHour,
  72. stUTC.wMinute,
  73. stUTC.wSecond,
  74. stUTC.wMilliseconds,
  75. ft.dwLowDateTime,
  76. ft.dwHighDateTime);
  77. }
  78. /* -----------------------------------------------------------------------
  79. GetArpaDate
  80. Returns a pointer to static memory containing the current date in
  81. Internet/ARPA standard format.
  82. Author
  83. Lindsay Harris - lindasyh
  84. History
  85. 13:49 on Wed 20 Apr 1994 -by- Lindsay Harris [lindsayh]
  86. First version.
  87. Imported to Tigris. Added passed-in buffer, changed year to 4-digit format
  88. ----------------------------------------------------------------------- */
  89. char *
  90. GetArpaDate( char achReturn[ cMaxArpaDate ] )
  91. {
  92. char chSign; // Sign to print.
  93. DWORD dwResult;
  94. int iBias; // Offset relative to GMT.
  95. TIME_ZONE_INFORMATION tzi; // Local time zone data.
  96. SYSTEMTIME stUTC; // The current time in UTC/GMT
  97. dwResult = GetTimeZoneInformation( &tzi );
  98. GetLocalTime( &stUTC );
  99. // Calculate the time zone offset.
  100. iBias = tzi.Bias;
  101. if( dwResult == TIME_ZONE_ID_DAYLIGHT )
  102. iBias += tzi.DaylightBias;
  103. /*
  104. * We always want to print the sign for the time zone offset, so
  105. * we decide what it is now and remember that when converting.
  106. * The convention is that west of the 0 degree meridian has a
  107. * negative offset - i.e. add the offset to GMT to get local time.
  108. */
  109. if( iBias > 0 )
  110. {
  111. chSign = '-'; // Yes, I do mean negative.
  112. }
  113. else
  114. {
  115. iBias = -iBias;
  116. chSign = '+';
  117. }
  118. /*
  119. * No major trickery here. We have all the data, so simply
  120. * format it according to the rules on how to do this.
  121. */
  122. _snprintf( achReturn, cMaxArpaDate , "%d %s %04d %02d:%02d:%02d %c%02d%02d",
  123. stUTC.wDay, rgchMonth[ stUTC.wMonth - 1 ],
  124. stUTC.wYear,
  125. stUTC.wHour, stUTC.wMinute, stUTC.wSecond, chSign,
  126. (iBias / 60) % 100, iBias % 60 );
  127. return achReturn;
  128. }