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.

305 lines
12 KiB

  1. //typedef long time_t;
  2. #include <time.h>
  3. class COleDateTimeSpan;
  4. class COleDateTime;
  5. /////////////////////////////////////////////////////////////////////////////
  6. // COleDateTime class helpers
  7. #define AFX_OLE_DATETIME_ERROR (-1)
  8. /////////////////////////////////////////////////////////////////////////////
  9. // COleDateTime class
  10. class COleDateTime
  11. {
  12. // Constructors
  13. public:
  14. static COleDateTime PASCAL GetCurrentTime();
  15. COleDateTime();
  16. COleDateTime(const COleDateTime& dateSrc);
  17. COleDateTime(const VARIANT& varSrc);
  18. COleDateTime(DATE dtSrc);
  19. COleDateTime(time_t timeSrc);
  20. COleDateTime(const SYSTEMTIME& systimeSrc);
  21. COleDateTime(const FILETIME& filetimeSrc);
  22. COleDateTime(int nYear, int nMonth, int nDay,
  23. int nHour, int nMin, int nSec);
  24. COleDateTime(WORD wDosDate, WORD wDosTime);
  25. // Attributes
  26. public:
  27. enum DateTimeStatus
  28. {
  29. valid = 0,
  30. invalid = 1, // Invalid date (out of range, etc.)
  31. null = 2 // Literally has no value
  32. };
  33. DATE m_dt;
  34. DateTimeStatus m_status;
  35. void SetStatus(DateTimeStatus status);
  36. DateTimeStatus GetStatus() const;
  37. int GetYear() const;
  38. int GetMonth() const; // month of year (1 = Jan)
  39. int GetDay() const; // day of month (0-31)
  40. int GetHour() const; // hour in day (0-23)
  41. int GetMinute() const; // minute in hour (0-59)
  42. int GetSecond() const; // second in minute (0-59)
  43. int GetDayOfWeek() const; // 1=Sun, 2=Mon, ..., 7=Sat
  44. int GetDayOfYear() const; // days since start of year, Jan 1 = 1
  45. // Operations
  46. public:
  47. const COleDateTime& operator=(const COleDateTime& dateSrc);
  48. const COleDateTime& operator=(const VARIANT& varSrc);
  49. const COleDateTime& operator=(DATE dtSrc);
  50. const COleDateTime& operator=(const time_t& timeSrc);
  51. const COleDateTime& operator=(const SYSTEMTIME& systimeSrc);
  52. const COleDateTime& operator=(const FILETIME& filetimeSrc);
  53. BOOL operator==(const COleDateTime& date) const;
  54. BOOL operator!=(const COleDateTime& date) const;
  55. BOOL operator<(const COleDateTime& date) const;
  56. BOOL operator>(const COleDateTime& date) const;
  57. BOOL operator<=(const COleDateTime& date) const;
  58. BOOL operator>=(const COleDateTime& date) const;
  59. // DateTime math
  60. COleDateTime operator+(const COleDateTimeSpan& dateSpan) const;
  61. COleDateTime operator-(const COleDateTimeSpan& dateSpan) const;
  62. const COleDateTime& operator+=(const COleDateTimeSpan dateSpan);
  63. const COleDateTime& operator-=(const COleDateTimeSpan dateSpan);
  64. // DateTimeSpan math
  65. COleDateTimeSpan operator-(const COleDateTime& date) const;
  66. operator DATE() const;
  67. BOOL SetDateTime(int nYear, int nMonth, int nDay,
  68. int nHour, int nMin, int nSec);
  69. BOOL SetDate(int nYear, int nMonth, int nDay);
  70. BOOL SetTime(int nHour, int nMin, int nSec);
  71. BOOL ParseDateTime(LPCTSTR lpszDate, DWORD dwFlags = 0,
  72. LCID lcid = LANG_USER_DEFAULT);
  73. // formatting
  74. CString Format(DWORD dwFlags = 0, LCID lcid = LANG_USER_DEFAULT) const;
  75. CString Format(LPCTSTR lpszFormat) const;
  76. CString Format(UINT nFormatID) const;
  77. // Implementation
  78. protected:
  79. void CheckRange();
  80. friend COleDateTimeSpan;
  81. };
  82. // COleDateTime diagnostics and serialization
  83. #ifdef _DEBUG
  84. CDumpContext& AFXAPI operator<<(CDumpContext& dc, COleDateTime dateSrc);
  85. #endif
  86. //CArchive& AFXAPI operator<<(CArchive& ar, COleDateTime dateSrc);
  87. //CArchive& AFXAPI operator>>(CArchive& ar, COleDateTime& dateSrc);
  88. /////////////////////////////////////////////////////////////////////////////
  89. // COleDateTimeSpan class
  90. class COleDateTimeSpan
  91. {
  92. // Constructors
  93. public:
  94. COleDateTimeSpan();
  95. COleDateTimeSpan(double dblSpanSrc);
  96. COleDateTimeSpan(const COleDateTimeSpan& dateSpanSrc);
  97. COleDateTimeSpan(long lDays, int nHours, int nMins, int nSecs);
  98. // Attributes
  99. public:
  100. enum DateTimeSpanStatus
  101. {
  102. valid = 0,
  103. invalid = 1, // Invalid span (out of range, etc.)
  104. null = 2 // Literally has no value
  105. };
  106. double m_span;
  107. DateTimeSpanStatus m_status;
  108. void SetStatus(DateTimeSpanStatus status);
  109. DateTimeSpanStatus GetStatus() const;
  110. double GetTotalDays() const; // span in days (about -3.65e6 to 3.65e6)
  111. double GetTotalHours() const; // span in hours (about -8.77e7 to 8.77e6)
  112. double GetTotalMinutes() const; // span in minutes (about -5.26e9 to 5.26e9)
  113. double GetTotalSeconds() const; // span in seconds (about -3.16e11 to 3.16e11)
  114. long GetDays() const; // component days in span
  115. long GetHours() const; // component hours in span (-23 to 23)
  116. long GetMinutes() const; // component minutes in span (-59 to 59)
  117. long GetSeconds() const; // component seconds in span (-59 to 59)
  118. // Operations
  119. public:
  120. const COleDateTimeSpan& operator=(double dblSpanSrc);
  121. const COleDateTimeSpan& operator=(const COleDateTimeSpan& dateSpanSrc);
  122. BOOL operator==(const COleDateTimeSpan& dateSpan) const;
  123. BOOL operator!=(const COleDateTimeSpan& dateSpan) const;
  124. BOOL operator<(const COleDateTimeSpan& dateSpan) const;
  125. BOOL operator>(const COleDateTimeSpan& dateSpan) const;
  126. BOOL operator<=(const COleDateTimeSpan& dateSpan) const;
  127. BOOL operator>=(const COleDateTimeSpan& dateSpan) const;
  128. // DateTimeSpan math
  129. COleDateTimeSpan operator+(const COleDateTimeSpan& dateSpan) const;
  130. COleDateTimeSpan operator-(const COleDateTimeSpan& dateSpan) const;
  131. const COleDateTimeSpan& operator+=(const COleDateTimeSpan dateSpan);
  132. const COleDateTimeSpan& operator-=(const COleDateTimeSpan dateSpan);
  133. COleDateTimeSpan operator-() const;
  134. operator double() const;
  135. void SetDateTimeSpan(long lDays, int nHours, int nMins, int nSecs);
  136. // formatting
  137. CString Format(LPCTSTR pFormat) const;
  138. CString Format(UINT nID) const;
  139. // Implementation
  140. public:
  141. void CheckRange();
  142. friend COleDateTime;
  143. };
  144. // COleDateTimeSpan diagnostics and serialization
  145. #ifdef _DEBUG
  146. CDumpContext& AFXAPI operator<<(CDumpContext& dc,COleDateTimeSpan dateSpanSrc);
  147. #endif
  148. //CArchive& AFXAPI operator<<(CArchive& ar, COleDateTimeSpan dateSpanSrc);
  149. //CArchive& AFXAPI operator>>(CArchive& ar, COleDateTimeSpan& dateSpanSrc);
  150. #define _AFXDISP_INLINE inline
  151. #ifdef _AFX_INLINE
  152. // COleDateTime
  153. _AFXDISP_INLINE COleDateTime::COleDateTime()
  154. { m_dt = 0; SetStatus(valid);}
  155. _AFXDISP_INLINE COleDateTime::COleDateTime(const COleDateTime& dateSrc)
  156. { m_dt = dateSrc.m_dt; m_status = dateSrc.m_status;}
  157. _AFXDISP_INLINE COleDateTime::COleDateTime(const VARIANT& varSrc)
  158. { *this = varSrc;}
  159. _AFXDISP_INLINE COleDateTime::COleDateTime(DATE dtSrc)
  160. { m_dt = dtSrc; SetStatus(valid);}
  161. _AFXDISP_INLINE COleDateTime::COleDateTime(time_t timeSrc)
  162. { *this = timeSrc;}
  163. _AFXDISP_INLINE COleDateTime::COleDateTime(const SYSTEMTIME& systimeSrc)
  164. { *this = systimeSrc;}
  165. _AFXDISP_INLINE COleDateTime::COleDateTime(const FILETIME& filetimeSrc)
  166. { *this = filetimeSrc;}
  167. _AFXDISP_INLINE COleDateTime::COleDateTime(int nYear, int nMonth, int nDay,
  168. int nHour, int nMin, int nSec)
  169. { SetDateTime(nYear, nMonth, nDay, nHour, nMin, nSec);}
  170. _AFXDISP_INLINE COleDateTime::COleDateTime(WORD wDosDate, WORD wDosTime)
  171. { m_status = DosDateTimeToVariantTime(wDosDate, wDosTime, &m_dt) ?
  172. valid : invalid;}
  173. _AFXDISP_INLINE const COleDateTime& COleDateTime::operator=(const COleDateTime& dateSrc)
  174. { m_dt = dateSrc.m_dt; m_status = dateSrc.m_status; return *this;}
  175. _AFXDISP_INLINE COleDateTime::DateTimeStatus COleDateTime::GetStatus() const
  176. { return m_status;}
  177. _AFXDISP_INLINE void COleDateTime::SetStatus(DateTimeStatus status)
  178. { m_status = status;}
  179. _AFXDISP_INLINE BOOL COleDateTime::operator==(const COleDateTime& date) const
  180. { return (m_status == date.m_status && m_dt == date.m_dt);}
  181. _AFXDISP_INLINE BOOL COleDateTime::operator!=(const COleDateTime& date) const
  182. { return (m_status != date.m_status || m_dt != date.m_dt);}
  183. _AFXDISP_INLINE const COleDateTime& COleDateTime::operator+=(
  184. const COleDateTimeSpan dateSpan)
  185. { *this = *this + dateSpan; return *this;}
  186. _AFXDISP_INLINE const COleDateTime& COleDateTime::operator-=(
  187. const COleDateTimeSpan dateSpan)
  188. { *this = *this - dateSpan; return *this;}
  189. _AFXDISP_INLINE COleDateTime::operator DATE() const
  190. { return m_dt;}
  191. _AFXDISP_INLINE COleDateTime::SetDate(int nYear, int nMonth, int nDay)
  192. { return SetDateTime(nYear, nMonth, nDay, 0, 0, 0);}
  193. _AFXDISP_INLINE COleDateTime::SetTime(int nHour, int nMin, int nSec)
  194. // Set date to zero date - 12/30/1899
  195. { return SetDateTime(1899, 12, 30, nHour, nMin, nSec);}
  196. // COleDateTimeSpan
  197. _AFXDISP_INLINE COleDateTimeSpan::COleDateTimeSpan()
  198. { m_span = 0; SetStatus(valid);}
  199. _AFXDISP_INLINE COleDateTimeSpan::COleDateTimeSpan(double dblSpanSrc)
  200. { m_span = dblSpanSrc; SetStatus(valid);}
  201. _AFXDISP_INLINE COleDateTimeSpan::COleDateTimeSpan(
  202. const COleDateTimeSpan& dateSpanSrc)
  203. { m_span = dateSpanSrc.m_span; m_status = dateSpanSrc.m_status;}
  204. _AFXDISP_INLINE COleDateTimeSpan::COleDateTimeSpan(
  205. long lDays, int nHours, int nMins, int nSecs)
  206. { SetDateTimeSpan(lDays, nHours, nMins, nSecs);}
  207. _AFXDISP_INLINE COleDateTimeSpan::DateTimeSpanStatus COleDateTimeSpan::GetStatus() const
  208. { return m_status;}
  209. _AFXDISP_INLINE void COleDateTimeSpan::SetStatus(DateTimeSpanStatus status)
  210. { m_status = status;}
  211. _AFXDISP_INLINE double COleDateTimeSpan::GetTotalDays() const
  212. { ASSERT(GetStatus() == valid); return m_span;}
  213. _AFXDISP_INLINE double COleDateTimeSpan::GetTotalHours() const
  214. { ASSERT(GetStatus() == valid); return m_span * 24;}
  215. _AFXDISP_INLINE double COleDateTimeSpan::GetTotalMinutes() const
  216. { ASSERT(GetStatus() == valid); return m_span * 24 * 60;}
  217. _AFXDISP_INLINE double COleDateTimeSpan::GetTotalSeconds() const
  218. { ASSERT(GetStatus() == valid); return m_span * 24 * 60 * 60;}
  219. _AFXDISP_INLINE long COleDateTimeSpan::GetDays() const
  220. { ASSERT(GetStatus() == valid); return (long)m_span;}
  221. _AFXDISP_INLINE BOOL COleDateTimeSpan::operator==(
  222. const COleDateTimeSpan& dateSpan) const
  223. { return (m_status == dateSpan.m_status &&
  224. m_span == dateSpan.m_span);}
  225. _AFXDISP_INLINE BOOL COleDateTimeSpan::operator!=(
  226. const COleDateTimeSpan& dateSpan) const
  227. { return (m_status != dateSpan.m_status ||
  228. m_span != dateSpan.m_span);}
  229. _AFXDISP_INLINE BOOL COleDateTimeSpan::operator<(
  230. const COleDateTimeSpan& dateSpan) const
  231. { ASSERT(GetStatus() == valid);
  232. ASSERT(dateSpan.GetStatus() == valid);
  233. return m_span < dateSpan.m_span;}
  234. _AFXDISP_INLINE BOOL COleDateTimeSpan::operator>(
  235. const COleDateTimeSpan& dateSpan) const
  236. { ASSERT(GetStatus() == valid);
  237. ASSERT(dateSpan.GetStatus() == valid);
  238. return m_span > dateSpan.m_span;}
  239. _AFXDISP_INLINE BOOL COleDateTimeSpan::operator<=(
  240. const COleDateTimeSpan& dateSpan) const
  241. { ASSERT(GetStatus() == valid);
  242. ASSERT(dateSpan.GetStatus() == valid);
  243. return m_span <= dateSpan.m_span;}
  244. _AFXDISP_INLINE BOOL COleDateTimeSpan::operator>=(
  245. const COleDateTimeSpan& dateSpan) const
  246. { ASSERT(GetStatus() == valid);
  247. ASSERT(dateSpan.GetStatus() == valid);
  248. return m_span >= dateSpan.m_span;}
  249. _AFXDISP_INLINE const COleDateTimeSpan& COleDateTimeSpan::operator+=(
  250. const COleDateTimeSpan dateSpan)
  251. { *this = *this + dateSpan; return *this;}
  252. _AFXDISP_INLINE const COleDateTimeSpan& COleDateTimeSpan::operator-=(
  253. const COleDateTimeSpan dateSpan)
  254. { *this = *this - dateSpan; return *this;}
  255. _AFXDISP_INLINE COleDateTimeSpan COleDateTimeSpan::operator-() const
  256. { return -this->m_span;}
  257. _AFXDISP_INLINE COleDateTimeSpan::operator double() const
  258. { return m_span;}
  259. #endif //_AFX_INLINE