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.

205 lines
5.7 KiB

  1. //#pragma title( "Common.cpp - Common class implementations" )
  2. /*
  3. Copyright (c) 1995-1998, Mission Critical Software, Inc. All rights reserved.
  4. ===============================================================================
  5. Module - Common.cpp
  6. System - Common
  7. Author - Tom Bernhardt, Rich Denham
  8. Created - 1994-08-22
  9. Description - Common class implementations.
  10. Updates - 1997-09-09 RED ErrorCodeToText moved to Err.cpp
  11. - 1997-09-12 RED replace TTime class
  12. ===============================================================================
  13. */
  14. #ifdef USE_STDAFX
  15. # include "stdafx.h"
  16. # include "rpc.h"
  17. #else
  18. # include <windows.h>
  19. #endif
  20. #include "Common.hpp"
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // TTime class member functions
  23. ///////////////////////////////////////////////////////////////////////////////
  24. TTime gTTime; // global instance of TTime
  25. time_t // ret-current time
  26. TTime::Now(
  27. time_t * pTime // out-optional current time
  28. ) const
  29. {
  30. time_t tTime; // work copy of current time
  31. union
  32. {
  33. __int64 intTime;
  34. FILETIME fileTime;
  35. } wTime;
  36. GetSystemTimeAsFileTime( &wTime.fileTime );
  37. tTime = ConvertFiletimeToTimet( wTime.intTime );
  38. if ( pTime ) *pTime = tTime;
  39. return tTime;
  40. }
  41. __int64 // ret-current time
  42. TTime::NowAsFiletime(
  43. __int64 * pTime // out-optional current time
  44. ) const
  45. {
  46. union
  47. {
  48. __int64 intTime;
  49. FILETIME fileTime;
  50. } wTime;
  51. GetSystemTimeAsFileTime( &wTime.fileTime );
  52. if ( pTime ) *pTime = wTime.intTime;
  53. return wTime.intTime;
  54. }
  55. time_t // ret-time_t representation
  56. TTime::ConvertFiletimeToTimet(
  57. __int64 fileTime // in -filetime representation
  58. ) const
  59. {
  60. __int64 wTime; // intermediate work area
  61. time_t retTime; // returned time
  62. // If the source date/time is less than the minimum date/time supported
  63. // by time_t, then zero is returned.
  64. // If the source date/time is more that the maximum date/time supported
  65. // by time_t, then ULONG_MAX is returned.
  66. wTime = fileTime / 10000000;
  67. if ( wTime < 11644473600 )
  68. {
  69. retTime = 0;
  70. }
  71. else
  72. {
  73. wTime -= 11644473600;
  74. if ( wTime > ULONG_MAX )
  75. {
  76. retTime = ULONG_MAX;
  77. }
  78. else
  79. {
  80. retTime = (time_t) wTime;
  81. }
  82. }
  83. return retTime;
  84. }
  85. WCHAR * // ret-YYYY-MM-DD HH:MM:SS string
  86. TTime::FormatIsoUtc(
  87. time_t tTime ,// in -time_t representation
  88. WCHAR * sTime // out-YYYY-MM-DD HH:MM:SS string
  89. ) const
  90. {
  91. struct tm * tmTime;
  92. tmTime = gmtime( &tTime );
  93. tmTime->tm_year += tmTime->tm_year >= 70 ? 1900 : 2000;
  94. swprintf(
  95. sTime,
  96. L"%04d-%02d-%02d %02d:%02d:%02d",
  97. tmTime->tm_year,
  98. tmTime->tm_mon+1,
  99. tmTime->tm_mday,
  100. tmTime->tm_hour,
  101. tmTime->tm_min,
  102. tmTime->tm_sec );
  103. return sTime;
  104. }
  105. WCHAR * // ret-YYYY-MM-DD HH:MM:SS string
  106. TTime::FormatIsoLcl(
  107. time_t tTime ,// in -time_t representation
  108. WCHAR * sTime // out-YYYY-MM-DD HH:MM:SS string
  109. ) const
  110. {
  111. struct tm * tmTime;
  112. TIME_ZONE_INFORMATION infoTime; // WIN32 time zone info
  113. time_t wTime; // workarea
  114. switch ( GetTimeZoneInformation( &infoTime ) )
  115. {
  116. case TIME_ZONE_ID_STANDARD:
  117. wTime = infoTime.StandardBias;
  118. break;
  119. case TIME_ZONE_ID_DAYLIGHT:
  120. wTime = infoTime.DaylightBias;
  121. break;
  122. default:
  123. wTime = 0;
  124. break;
  125. }
  126. wTime = (infoTime.Bias + wTime) * 60;
  127. wTime = tTime - wTime;
  128. if ( wTime < 0 )
  129. {
  130. wTime = 0;
  131. }
  132. tmTime = gmtime( &wTime );
  133. tmTime->tm_year += tmTime->tm_year >= 70 ? 1900 : 2000;
  134. swprintf(
  135. sTime,
  136. L"%04d-%02d-%02d %02d:%02d:%02d",
  137. tmTime->tm_year,
  138. tmTime->tm_mon+1,
  139. tmTime->tm_mday,
  140. tmTime->tm_hour,
  141. tmTime->tm_min,
  142. tmTime->tm_sec );
  143. return sTime;
  144. }
  145. // Return time zone information
  146. // If the returned value is TRUE, the EaTimeZoneInfo structure is filled in
  147. // If the returned value is FALSE, the EaTimeZoneInfo structure is all zeroes
  148. // Note: UTC (gTTime.Now( NULL )) plus pTimeZoneInfo->biasdst is the local date/time
  149. BOOL
  150. EaGetTimeZoneInfo(
  151. EaTimeZoneInfo * pTimeZoneInfo // in -time zone information
  152. )
  153. {
  154. memset( pTimeZoneInfo, 0, sizeof *pTimeZoneInfo );
  155. BOOL retval=TRUE; // returned value
  156. DWORD OsRc; // OS return code
  157. TIME_ZONE_INFORMATION TimeZoneInfo; // WIN32 time zone info
  158. OsRc = GetTimeZoneInformation( &TimeZoneInfo );
  159. switch ( OsRc )
  160. {
  161. case TIME_ZONE_ID_STANDARD:
  162. pTimeZoneInfo->dst = TimeZoneInfo.StandardBias;
  163. break;
  164. case TIME_ZONE_ID_DAYLIGHT:
  165. pTimeZoneInfo->dst = TimeZoneInfo.DaylightBias;
  166. break;
  167. case TIME_ZONE_ID_UNKNOWN:
  168. retval = TimeZoneInfo.Bias;
  169. break;
  170. default:
  171. retval = FALSE;
  172. break;
  173. }
  174. if ( retval )
  175. {
  176. pTimeZoneInfo->bias = TimeZoneInfo.Bias * 60;
  177. pTimeZoneInfo->dst *= 60;
  178. pTimeZoneInfo->biasdst = pTimeZoneInfo->bias + pTimeZoneInfo->dst;
  179. }
  180. return retval;
  181. }
  182. // Common.cpp - end of file