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.

404 lines
12 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-2001 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLTIME_H__
  11. #define __ATLTIME_H__
  12. #pragma once
  13. #include <atldef.h>
  14. #ifndef _ATL_MIN_CRT
  15. #include <time.h>
  16. #ifdef _AFX
  17. #include <afxstr.h>
  18. #else
  19. #include <atlstr.h>
  20. #endif
  21. #endif
  22. namespace ATL
  23. {
  24. class CTimeSpan
  25. {
  26. public:
  27. CTimeSpan() throw();
  28. CTimeSpan( __time64_t time ) throw();
  29. CTimeSpan( LONG lDays, int nHours, int nMins, int nSecs ) throw();
  30. LONGLONG GetDays() const throw();
  31. LONGLONG GetTotalHours() const throw();
  32. LONG GetHours() const throw();
  33. LONGLONG GetTotalMinutes() const throw();
  34. LONG GetMinutes() const throw();
  35. LONGLONG GetTotalSeconds() const throw();
  36. LONG GetSeconds() const throw();
  37. __time64_t GetTimeSpan() const throw();
  38. CTimeSpan operator+( CTimeSpan span ) const throw();
  39. CTimeSpan operator-( CTimeSpan span ) const throw();
  40. CTimeSpan& operator+=( CTimeSpan span ) throw();
  41. CTimeSpan& operator-=( CTimeSpan span ) throw();
  42. bool operator==( CTimeSpan span ) const throw();
  43. bool operator!=( CTimeSpan span ) const throw();
  44. bool operator<( CTimeSpan span ) const throw();
  45. bool operator>( CTimeSpan span ) const throw();
  46. bool operator<=( CTimeSpan span ) const throw();
  47. bool operator>=( CTimeSpan span ) const throw();
  48. #ifndef _ATL_MIN_CRT
  49. public:
  50. CString Format( LPCTSTR pszFormat ) const;
  51. CString Format( UINT nID ) const;
  52. #endif
  53. #if defined(_AFX) && defined(_UNICODE)
  54. // REVIEW
  55. // for compatibility with MFC 3.x
  56. CString Format(LPCSTR pFormat) const;
  57. #endif
  58. #ifdef _AFX
  59. CArchive& Serialize64(CArchive& ar);
  60. #endif
  61. private:
  62. __time64_t m_timeSpan;
  63. };
  64. class CTime
  65. {
  66. public:
  67. static CTime GetCurrentTime() throw();
  68. CTime() throw();
  69. CTime( __time64_t time ) throw();
  70. CTime( int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec,
  71. int nDST = -1 ) throw();
  72. CTime( WORD wDosDate, WORD wDosTime, int nDST = -1 ) throw();
  73. CTime( const SYSTEMTIME& st, int nDST = -1 ) throw();
  74. CTime( const FILETIME& ft, int nDST = -1 ) throw();
  75. #ifdef __oledb_h__
  76. CTime( const DBTIMESTAMP& dbts, int nDST = -1 ) throw();
  77. #endif
  78. CTime& operator=( __time64_t time ) throw();
  79. CTime& operator+=( CTimeSpan span ) throw();
  80. CTime& operator-=( CTimeSpan span ) throw();
  81. CTimeSpan operator-( CTime time ) const throw();
  82. CTime operator-( CTimeSpan span ) const throw();
  83. CTime operator+( CTimeSpan span ) const throw();
  84. bool operator==( CTime time ) const throw();
  85. bool operator!=( CTime time ) const throw();
  86. bool operator<( CTime time ) const throw();
  87. bool operator>( CTime time ) const throw();
  88. bool operator<=( CTime time ) const throw();
  89. bool operator>=( CTime time ) const throw();
  90. struct tm* GetGmtTm( struct tm* ptm = NULL ) const throw();
  91. struct tm* GetLocalTm( struct tm* ptm = NULL ) const throw();
  92. bool GetAsSystemTime( SYSTEMTIME& st ) const throw();
  93. #ifdef __oledb_h__
  94. bool GetAsDBTIMESTAMP( DBTIMESTAMP& dbts ) const throw();
  95. #endif
  96. __time64_t GetTime() const throw();
  97. int GetYear() const throw();
  98. int GetMonth() const throw();
  99. int GetDay() const throw();
  100. int GetHour() const throw();
  101. int GetMinute() const throw();
  102. int GetSecond() const throw();
  103. int GetDayOfWeek() const throw();
  104. #ifndef _ATL_MIN_CRT
  105. // formatting using "C" strftime
  106. CString Format( LPCTSTR pszFormat ) const;
  107. CString FormatGmt( LPCTSTR pszFormat ) const;
  108. CString Format( UINT nFormatID ) const;
  109. CString FormatGmt( UINT nFormatID ) const;
  110. #endif
  111. #if defined(_AFX) && defined(_UNICODE)
  112. // REVIEW
  113. // for compatibility with MFC 3.x
  114. CString Format(LPCSTR pFormat) const;
  115. CString FormatGmt(LPCSTR pFormat) const;
  116. #endif
  117. #ifdef _AFX
  118. CArchive& Serialize64(CArchive& ar);
  119. #endif
  120. private:
  121. __time64_t m_time;
  122. };
  123. class COleDateTimeSpan
  124. {
  125. // Constructors
  126. public:
  127. COleDateTimeSpan() throw();
  128. COleDateTimeSpan(double dblSpanSrc) throw();
  129. COleDateTimeSpan(LONG lDays, int nHours, int nMins, int nSecs) throw();
  130. // Attributes
  131. enum DateTimeSpanStatus
  132. {
  133. valid = 0,
  134. invalid = 1, // Invalid span (out of range, etc.)
  135. null = 2, // Literally has no value
  136. };
  137. double m_span;
  138. DateTimeSpanStatus m_status;
  139. void SetStatus(DateTimeSpanStatus status) throw();
  140. DateTimeSpanStatus GetStatus() const throw();
  141. double GetTotalDays() const throw(); // span in days (about -3.65e6 to 3.65e6)
  142. double GetTotalHours() const throw(); // span in hours (about -8.77e7 to 8.77e6)
  143. double GetTotalMinutes() const throw(); // span in minutes (about -5.26e9 to 5.26e9)
  144. double GetTotalSeconds() const throw(); // span in seconds (about -3.16e11 to 3.16e11)
  145. LONG GetDays() const throw(); // component days in span
  146. LONG GetHours() const throw(); // component hours in span (-23 to 23)
  147. LONG GetMinutes() const throw(); // component minutes in span (-59 to 59)
  148. LONG GetSeconds() const throw(); // component seconds in span (-59 to 59)
  149. // Operations
  150. COleDateTimeSpan& operator=(double dblSpanSrc) throw();
  151. bool operator==(const COleDateTimeSpan& dateSpan) const throw();
  152. bool operator!=(const COleDateTimeSpan& dateSpan) const throw();
  153. bool operator<(const COleDateTimeSpan& dateSpan) const throw();
  154. bool operator>(const COleDateTimeSpan& dateSpan) const throw();
  155. bool operator<=(const COleDateTimeSpan& dateSpan) const throw();
  156. bool operator>=(const COleDateTimeSpan& dateSpan) const throw();
  157. // DateTimeSpan math
  158. COleDateTimeSpan operator+(const COleDateTimeSpan& dateSpan) const throw();
  159. COleDateTimeSpan operator-(const COleDateTimeSpan& dateSpan) const throw();
  160. COleDateTimeSpan& operator+=(const COleDateTimeSpan dateSpan) throw();
  161. COleDateTimeSpan& operator-=(const COleDateTimeSpan dateSpan) throw();
  162. COleDateTimeSpan operator-() const throw();
  163. operator double() const throw();
  164. void SetDateTimeSpan(LONG lDays, int nHours, int nMins, int nSecs) throw();
  165. #ifndef _ATL_MIN_CRT
  166. // formatting
  167. CString Format(LPCTSTR pFormat) const;
  168. CString Format(UINT nID) const;
  169. #endif
  170. // Implementation
  171. void CheckRange();
  172. private:
  173. static const double OLE_DATETIME_HALFSECOND;
  174. };
  175. class COleDateTime
  176. {
  177. // Constructors
  178. public:
  179. static COleDateTime GetCurrentTime() throw();
  180. COleDateTime() throw();
  181. COleDateTime(const VARIANT& varSrc) throw();
  182. COleDateTime(DATE dtSrc) throw();
  183. #ifndef _WIN64
  184. COleDateTime(time_t timeSrc) throw();
  185. #endif // _WIN64
  186. COleDateTime(__time64_t timeSrc) throw();
  187. COleDateTime(const SYSTEMTIME& systimeSrc) throw();
  188. COleDateTime(const FILETIME& filetimeSrc) throw();
  189. COleDateTime(int nYear, int nMonth, int nDay,
  190. int nHour, int nMin, int nSec) throw();
  191. COleDateTime(WORD wDosDate, WORD wDosTime) throw();
  192. // Attributes
  193. enum DateTimeStatus
  194. {
  195. error = -1,
  196. valid = 0,
  197. invalid = 1, // Invalid date (out of range, etc.)
  198. null = 2, // Literally has no value
  199. };
  200. DATE m_dt;
  201. DateTimeStatus m_status;
  202. void SetStatus(DateTimeStatus status) throw();
  203. DateTimeStatus GetStatus() const throw();
  204. bool GetAsSystemTime(SYSTEMTIME& sysTime) const throw();
  205. bool GetAsUDATE( UDATE& udate ) const throw();
  206. int GetYear() const throw();
  207. // Month of year (1 = January)
  208. int GetMonth() const throw();
  209. // Day of month (1-31)
  210. int GetDay() const throw();
  211. // Hour in day (0-23)
  212. int GetHour() const throw();
  213. // Minute in hour (0-59)
  214. int GetMinute() const throw();
  215. // Second in minute (0-59)
  216. int GetSecond() const throw();
  217. // Day of week (1 = Sunday, 2 = Monday, ..., 7 = Saturday)
  218. int GetDayOfWeek() const throw();
  219. // Days since start of year (1 = January 1)
  220. int GetDayOfYear() const throw();
  221. // Operations
  222. COleDateTime& operator=(const VARIANT& varSrc) throw();
  223. COleDateTime& operator=(DATE dtSrc) throw();
  224. #ifndef _WIN64
  225. COleDateTime& operator=(const time_t& timeSrc) throw();
  226. #endif // _WIN64
  227. COleDateTime& operator=(const __time64_t& timeSrc) throw();
  228. COleDateTime& operator=(const SYSTEMTIME& systimeSrc) throw();
  229. COleDateTime& operator=(const FILETIME& filetimeSrc) throw();
  230. COleDateTime& operator=(const UDATE& udate) throw();
  231. bool operator==(const COleDateTime& date) const throw();
  232. bool operator!=(const COleDateTime& date) const throw();
  233. bool operator<(const COleDateTime& date) const throw();
  234. bool operator>(const COleDateTime& date) const throw();
  235. bool operator<=(const COleDateTime& date) const throw();
  236. bool operator>=(const COleDateTime& date) const throw();
  237. // DateTime math
  238. COleDateTime operator+(COleDateTimeSpan dateSpan) const throw();
  239. COleDateTime operator-(COleDateTimeSpan dateSpan) const throw();
  240. COleDateTime& operator+=(COleDateTimeSpan dateSpan) throw();
  241. COleDateTime& operator-=(COleDateTimeSpan dateSpan) throw();
  242. // DateTimeSpan math
  243. COleDateTimeSpan operator-(const COleDateTime& date) const throw();
  244. operator DATE() const throw();
  245. int SetDateTime(int nYear, int nMonth, int nDay,
  246. int nHour, int nMin, int nSec) throw();
  247. int SetDate(int nYear, int nMonth, int nDay) throw();
  248. int SetTime(int nHour, int nMin, int nSec) throw();
  249. bool ParseDateTime(LPCTSTR lpszDate, DWORD dwFlags = 0,
  250. LCID lcid = LANG_USER_DEFAULT) throw();
  251. #ifndef _ATL_MIN_CRT
  252. // formatting
  253. CString Format(DWORD dwFlags = 0, LCID lcid = LANG_USER_DEFAULT) const;
  254. CString Format(LPCTSTR lpszFormat) const;
  255. CString Format(UINT nFormatID) const;
  256. #endif
  257. protected:
  258. static double DoubleFromDate( DATE date ) throw();
  259. static DATE DateFromDouble( double f ) throw();
  260. void CheckRange();
  261. };
  262. class CFileTimeSpan
  263. {
  264. public:
  265. CFileTimeSpan() throw();
  266. CFileTimeSpan( const CFileTimeSpan& span ) throw();
  267. CFileTimeSpan( LONGLONG nSpan ) throw();
  268. CFileTimeSpan& operator=( const CFileTimeSpan& span ) throw();
  269. CFileTimeSpan& operator+=( CFileTimeSpan span ) throw();
  270. CFileTimeSpan& operator-=( CFileTimeSpan span ) throw();
  271. CFileTimeSpan operator+( CFileTimeSpan span ) const throw();
  272. CFileTimeSpan operator-( CFileTimeSpan span ) const throw();
  273. bool operator==( CFileTimeSpan span ) const throw();
  274. bool operator!=( CFileTimeSpan span ) const throw();
  275. bool operator<( CFileTimeSpan span ) const throw();
  276. bool operator>( CFileTimeSpan span ) const throw();
  277. bool operator<=( CFileTimeSpan span ) const throw();
  278. bool operator>=( CFileTimeSpan span ) const throw();
  279. LONGLONG GetTimeSpan() const throw();
  280. void SetTimeSpan( LONGLONG nSpan ) throw();
  281. protected:
  282. LONGLONG m_nSpan;
  283. };
  284. class CFileTime :
  285. public FILETIME
  286. {
  287. public:
  288. CFileTime() throw();
  289. CFileTime( const FILETIME& ft ) throw();
  290. CFileTime( ULONGLONG nTime ) throw();
  291. static CFileTime GetCurrentTime() throw();
  292. CFileTime& operator=( const FILETIME& ft ) throw();
  293. CFileTime& operator+=( CFileTimeSpan span ) throw();
  294. CFileTime& operator-=( CFileTimeSpan span ) throw();
  295. CFileTime operator+( CFileTimeSpan span ) const throw();
  296. CFileTime operator-( CFileTimeSpan span ) const throw();
  297. CFileTimeSpan operator-( CFileTime ft ) const throw();
  298. bool operator==( CFileTime ft ) const throw();
  299. bool operator!=( CFileTime ft ) const throw();
  300. bool operator<( CFileTime ft ) const throw();
  301. bool operator>( CFileTime ft ) const throw();
  302. bool operator<=( CFileTime ft ) const throw();
  303. bool operator>=( CFileTime ft ) const throw();
  304. ULONGLONG GetTime() const throw();
  305. void SetTime( ULONGLONG nTime ) throw();
  306. CFileTime UTCToLocal() const throw();
  307. CFileTime LocalToUTC() const throw();
  308. static const ULONGLONG Millisecond = 10000;
  309. static const ULONGLONG Second = Millisecond * 1000;
  310. static const ULONGLONG Minute = Second * 60;
  311. static const ULONGLONG Hour = Minute * 60;
  312. static const ULONGLONG Day = Hour * 24;
  313. static const ULONGLONG Week = Day * 7;
  314. };
  315. }
  316. #include <atltime.inl>
  317. #endif // __ATLTIME_H__