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.

309 lines
6.6 KiB

  1. /**************************************************************************\
  2. *
  3. * Copyright (c) 1999 Microsoft Corporation
  4. *
  5. * Module Name:
  6. *
  7. * FileTime.hpp
  8. *
  9. * Abstract:
  10. *
  11. * Declare the CFILETIME class. This class wraps the FILETIME
  12. * structure, and gives it operators and conversion functions
  13. * (type-cast operators) to make it usable in equations.
  14. * This class adds no state, so a CFILETIME object is bitwise
  15. * compatible with a FILETIME object.
  16. *
  17. * This class is completely inline. There is no implementation
  18. * file.
  19. *
  20. * Created:
  21. *
  22. * 4/26/1999 Mike Hillberg
  23. *
  24. \**************************************************************************/
  25. #ifndef _FILETIME_HPP
  26. #define _FILETIME_HPP
  27. #include <tchar.h> // _tcsftime
  28. class CFILETIME // cft
  29. {
  30. // ------------
  31. // Construction
  32. // ------------
  33. public:
  34. CFILETIME()
  35. {
  36. SetToUTC();
  37. }
  38. CFILETIME( const CFILETIME &cft )
  39. {
  40. _ll = cft._ll;
  41. }
  42. CFILETIME( const SYSTEMTIME &st )
  43. {
  44. SystemTimeToFileTime( &st, &_filetime );
  45. }
  46. CFILETIME( const FILETIME &ft )
  47. {
  48. _ll = *(LONGLONG*) &ft;
  49. }
  50. CFILETIME( const LONGLONG ll )
  51. {
  52. _ll = ll;
  53. }
  54. CFILETIME( const LARGE_INTEGER &li )
  55. {
  56. _li = li;
  57. }
  58. CFILETIME( const ULARGE_INTEGER &uli )
  59. {
  60. _uli = uli;
  61. }
  62. // --------------------
  63. // Conversion Functions
  64. // --------------------
  65. public:
  66. // Convert to a struct _FILETIME
  67. operator FILETIME () const
  68. {
  69. return( _filetime );
  70. }
  71. // Convert to a SYSTEMTIME (fails if the current _filetime)
  72. // is greater than 0x80000000
  73. operator SYSTEMTIME () const
  74. {
  75. SYSTEMTIME st;
  76. if( !FileTimeToSystemTime( &_filetime, &st ))
  77. {
  78. // The current _filetime is negative.
  79. memset( &st, 0, sizeof(st) );
  80. }
  81. return( st );
  82. }
  83. operator LARGE_INTEGER () const
  84. {
  85. return( _li );
  86. }
  87. operator LONGLONG () const
  88. {
  89. return( _ll );
  90. }
  91. operator ULARGE_INTEGER () const
  92. {
  93. return( _uli );
  94. }
  95. // ---------
  96. // Operators
  97. // ---------
  98. public:
  99. // Assignment
  100. CFILETIME &operator= (const CFILETIME &cft)
  101. {
  102. _filetime = cft._filetime;
  103. return (*this);
  104. }
  105. // Addition of an offset
  106. CFILETIME operator+ ( CFILETIME &cft ) const
  107. {
  108. return (CFILETIME) ( _ll + cft._ll );
  109. }
  110. CFILETIME &operator+= ( CFILETIME &cft )
  111. {
  112. _ll += cft._ll;
  113. return( *this );
  114. }
  115. // Subtraction of an offset (note that this can result in a negative
  116. // number).
  117. CFILETIME operator- ( CFILETIME &cft ) const
  118. {
  119. return (CFILETIME) ( _ll - cft._ll );
  120. }
  121. CFILETIME &operator-= ( CFILETIME &cft )
  122. {
  123. _ll -= cft._ll;
  124. return( *this );
  125. }
  126. // Comparisson
  127. BOOL operator== ( const CFILETIME &cft ) const
  128. {
  129. return( cft._ll == _ll );
  130. }
  131. BOOL operator> ( const CFILETIME &cft ) const
  132. {
  133. return( _ll > cft._ll );
  134. }
  135. BOOL operator< ( const CFILETIME &cft ) const
  136. {
  137. return( _ll < cft._ll );
  138. }
  139. BOOL operator>= ( const CFILETIME &cft ) const
  140. {
  141. return( *this == cft || *this > cft );
  142. }
  143. BOOL operator<= ( const CFILETIME &cft ) const
  144. {
  145. return( *this == cft || *this < cft );
  146. }
  147. // -------
  148. // Methods
  149. // -------
  150. public:
  151. void IncrementTickCount( DWORD dwTickCount )
  152. {
  153. // Add the tick count offset, converted from milliseconds
  154. // to 100 nanosecond units
  155. _ll += (LONGLONG) dwTickCount * 10*1000;
  156. }
  157. void DecrementTickCount( DWORD dwTickCount )
  158. {
  159. _ll -= (LONGLONG) dwTickCount * 10*1000;
  160. }
  161. void IncrementSeconds( DWORD dwSeconds )
  162. {
  163. _ll += (LONGLONG) dwSeconds * 10*1000*1000;
  164. }
  165. void DecrementSeconds( DWORD dwSeconds )
  166. {
  167. _ll -= (LONGLONG) dwSeconds * 10*1000*1000;
  168. }
  169. void IncrementMilliseconds( DWORD dwMilliseconds )
  170. {
  171. _ll += (LONGLONG) dwMilliseconds * 10*1000;
  172. }
  173. void DecrementMilliseconds( DWORD dwMilliseconds )
  174. {
  175. _ll -= (LONGLONG) dwMilliseconds * 10*1000;
  176. }
  177. void SetToUTC()
  178. {
  179. SYSTEMTIME st;
  180. GetSystemTime( &st );
  181. SystemTimeToFileTime( &st, (FILETIME*) this );
  182. }
  183. void SetToLocal()
  184. {
  185. SYSTEMTIME st;
  186. GetLocalTime( &st );
  187. SystemTimeToFileTime( &st, (FILETIME*) this );
  188. }
  189. // Convert from UTC to Local. A time of zero, though, converts to zero.
  190. CFILETIME ConvertUtcToLocal() const
  191. {
  192. CFILETIME cftLocal(0);
  193. if( cftLocal == *this
  194. ||
  195. !FileTimeToLocalFileTime( &_filetime, reinterpret_cast<FILETIME*>(&cftLocal) ))
  196. {
  197. cftLocal = 0;
  198. }
  199. return( cftLocal );
  200. }
  201. DWORD HighDateTime() const
  202. {
  203. return( _filetime.dwHighDateTime );
  204. }
  205. DWORD LowDateTime() const
  206. {
  207. return( _filetime.dwLowDateTime );
  208. }
  209. // Format the time to a string using a strftime format string.
  210. // The time is not converted to local or wrt daylight savings
  211. // Cannot handle times before 1/1/1900.
  212. void Format( ULONG cch, TCHAR *ptszResult, const TCHAR *ptszFormat ) const
  213. {
  214. struct tm tm;
  215. SYSTEMTIME st = static_cast<SYSTEMTIME>(*this);
  216. memset( &tm, 0, sizeof(tm) );
  217. tm.tm_sec = st.wSecond;
  218. tm.tm_min = st.wMinute;
  219. tm.tm_hour = st.wHour;
  220. tm.tm_mday = st.wDay;
  221. tm.tm_mon = st.wMonth - 1; // tm_mon is 0 based
  222. tm.tm_year = st.wYear - 1900; // tm_year is 1900 based
  223. tm.tm_isdst = 0; // This method does no time zone conversion
  224. tm.tm_wday = st.wDayOfWeek;
  225. if( 0 == _tcsftime( ptszResult, cch, ptszFormat, &tm ))
  226. ptszResult[0] = TEXT('\0');
  227. }
  228. // Default string-ization
  229. void Stringize( ULONG cch, TCHAR *ptsz ) const
  230. {
  231. // E.g. "Sunday, 3/22/98, 13:30:22"
  232. Format( cch, ptsz, TEXT("%a, %m/%d/%y, %H:%M:%S") );
  233. }
  234. // ------------
  235. // Data Members
  236. // ------------
  237. private:
  238. union
  239. {
  240. FILETIME _filetime;
  241. LONGLONG _ll;
  242. LARGE_INTEGER _li;
  243. ULARGE_INTEGER _uli;
  244. };
  245. };
  246. #endif // #ifndef _FILETIME_HPP