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.

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