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.

300 lines
6.5 KiB

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