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.

385 lines
9.9 KiB

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1995 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include <urlint.h>
  11. #include <map_kv.h>
  12. #include "coll.hxx"
  13. #include "ctime.hxx"
  14. /*
  15. #include "stdafx.h"
  16. #ifdef AFX_AUX_SEG
  17. #pragma code_seg(AFX_AUX_SEG)
  18. #endif
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. */
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CTime - absolute time
  26. CTime::CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST)
  27. {
  28. struct tm atm;
  29. atm.tm_sec = nSec;
  30. atm.tm_min = nMin;
  31. atm.tm_hour = nHour;
  32. ASSERT(nDay >= 1 && nDay <= 31);
  33. atm.tm_mday = nDay;
  34. ASSERT(nMonth >= 1 && nMonth <= 12);
  35. atm.tm_mon = nMonth - 1; // tm_mon is 0 based
  36. ASSERT(nYear >= 1900);
  37. atm.tm_year = nYear - 1900; // tm_year is 1900 based
  38. atm.tm_isdst = nDST;
  39. m_time = mktime(&atm);
  40. ASSERT(m_time != -1); // indicates an illegal input time
  41. }
  42. CTime::CTime(WORD wDosDate, WORD wDosTime, int nDST)
  43. {
  44. struct tm atm;
  45. atm.tm_sec = (wDosTime & ~0xFFE0) << 1;
  46. atm.tm_min = (wDosTime & ~0xF800) >> 5;
  47. atm.tm_hour = wDosTime >> 11;
  48. atm.tm_mday = wDosDate & ~0xFFE0;
  49. atm.tm_mon = ((wDosDate & ~0xFE00) >> 5) - 1;
  50. atm.tm_year = (wDosDate >> 9) + 80;
  51. atm.tm_isdst = nDST;
  52. m_time = mktime(&atm);
  53. ASSERT(m_time != -1); // indicates an illegal input time
  54. }
  55. CTime::CTime(const SYSTEMTIME& sysTime, int nDST)
  56. {
  57. if (sysTime.wYear < 1900)
  58. {
  59. time_t time0 = 0L;
  60. CTime timeT(time0);
  61. *this = timeT;
  62. }
  63. else
  64. {
  65. CTime timeT(
  66. (int)sysTime.wYear, (int)sysTime.wMonth, (int)sysTime.wDay,
  67. (int)sysTime.wHour, (int)sysTime.wMinute, (int)sysTime.wSecond,
  68. nDST);
  69. *this = timeT;
  70. }
  71. }
  72. CTime::CTime(const FILETIME& fileTime, int nDST)
  73. {
  74. // first convert file time (UTC time) to local time
  75. FILETIME localTime;
  76. if (!FileTimeToLocalFileTime(&fileTime, &localTime))
  77. {
  78. m_time = 0;
  79. return;
  80. }
  81. // then convert that time to system time
  82. SYSTEMTIME sysTime;
  83. if (!FileTimeToSystemTime(&localTime, &sysTime))
  84. {
  85. m_time = 0;
  86. return;
  87. }
  88. // then convert the system time to a time_t (C-runtime local time)
  89. CTime timeT(sysTime, nDST);
  90. *this = timeT;
  91. }
  92. CTime PASCAL CTime::GetCurrentTime()
  93. // return the current system time
  94. {
  95. return CTime(::time(NULL));
  96. }
  97. struct tm* CTime::GetGmtTm(struct tm* ptm) const
  98. {
  99. if (ptm != NULL)
  100. {
  101. *ptm = *gmtime(&m_time);
  102. return ptm;
  103. }
  104. else
  105. return gmtime(&m_time);
  106. }
  107. struct tm* CTime::GetLocalTm(struct tm* ptm) const
  108. {
  109. if (ptm != NULL)
  110. {
  111. struct tm* ptmTemp = localtime(&m_time);
  112. if (ptmTemp == NULL)
  113. return NULL; // indicates the m_time was not initialized!
  114. *ptm = *ptmTemp;
  115. return ptm;
  116. }
  117. else
  118. return localtime(&m_time);
  119. }
  120. #ifdef _DEBUG
  121. CDumpContext& AFXAPI operator <<(CDumpContext& dc, CTime time)
  122. {
  123. char* psz = ctime(&time.m_time);
  124. if ((psz == NULL) || (time.m_time == 0))
  125. return dc << "CTime(invalid #" << time.m_time << ")";
  126. // format it
  127. psz[24] = '\0'; // nuke newline
  128. return dc << "CTime(\"" << psz << "\")";
  129. }
  130. #endif
  131. CArchive& AFXAPI operator <<(CArchive& ar, CTime time)
  132. {
  133. //return ar << (DWORD) time.m_time;
  134. ASSERT(FALSE);
  135. return ar;
  136. }
  137. CArchive& AFXAPI operator >>(CArchive& ar, CTime& rtime)
  138. {
  139. //return ar >> (DWORD&) rtime.m_time;
  140. ASSERT(FALSE);
  141. return ar;
  142. }
  143. /////////////////////////////////////////////////////////////////////////////
  144. // CTimeSpan - relative time
  145. #ifdef _DEBUG
  146. CDumpContext& AFXAPI operator <<(CDumpContext& dc, CTimeSpan timeSpan)
  147. {
  148. return dc << "CTimeSpan(" << timeSpan.GetDays() << " days, " <<
  149. timeSpan.GetHours() << " hours, " <<
  150. timeSpan.GetMinutes() << " minutes and " <<
  151. timeSpan.GetSeconds() << " seconds)";
  152. }
  153. #endif
  154. CArchive& AFXAPI operator <<(CArchive& ar, CTimeSpan timeSpan)
  155. {
  156. //return ar << (DWORD) timeSpan.m_timeSpan;
  157. ASSERT(FALSE);
  158. return ar;
  159. }
  160. CArchive& AFXAPI operator >>(CArchive& ar, CTimeSpan& rtimeSpan)
  161. {
  162. //return ar >> (DWORD&) rtimeSpan.m_timeSpan;
  163. ASSERT(FALSE);
  164. return ar;
  165. }
  166. /////////////////////////////////////////////////////////////////////////////
  167. // String formatting
  168. #define maxTimeBufferSize 128
  169. // Verifies will fail if the needed buffer size is too large
  170. #ifdef _UNICODE
  171. #endif
  172. CString CTimeSpan::Format(LPCTSTR pFormat) const
  173. // formatting timespans is a little trickier than formatting CTimes
  174. // * we are only interested in relative time formats, ie. it is illegal
  175. // to format anything dealing with absolute time (i.e. years, months,
  176. // day of week, day of year, timezones, ...)
  177. // * the only valid formats:
  178. // %D - # of days -- NEW !!!
  179. // %H - hour in 24 hour format
  180. // %M - minute (0-59)
  181. // %S - seconds (0-59)
  182. // %% - percent sign
  183. {
  184. TCHAR szBuffer[maxTimeBufferSize];
  185. TCHAR ch;
  186. LPTSTR pch = szBuffer;
  187. while ((ch = *pFormat++) != '\0')
  188. {
  189. ASSERT(pch < &szBuffer[maxTimeBufferSize]);
  190. if (ch == '%')
  191. {
  192. switch (ch = *pFormat++)
  193. {
  194. default:
  195. ASSERT(FALSE); // probably a bad format character
  196. case '%':
  197. *pch++ = ch;
  198. break;
  199. case 'D':
  200. pch += wsprintf(pch, _T("%ld"), GetDays());
  201. break;
  202. case 'H':
  203. pch += wsprintf(pch, _T("%02d"), GetHours());
  204. break;
  205. case 'M':
  206. pch += wsprintf(pch, _T("%02d"), GetMinutes());
  207. break;
  208. case 'S':
  209. pch += wsprintf(pch, _T("%02d"), GetSeconds());
  210. break;
  211. }
  212. }
  213. else
  214. {
  215. *pch++ = ch;
  216. if (_istlead(ch))
  217. {
  218. ASSERT(pch < &szBuffer[maxTimeBufferSize]);
  219. *pch++ = *pFormat++;
  220. }
  221. }
  222. }
  223. *pch = '\0';
  224. return szBuffer;
  225. }
  226. CString CTime::Format(LPCTSTR pFormat) const
  227. // formatting timespans is a little trickier than formatting CTimes
  228. // * we are only interested in relative time formats, ie. it is illegal
  229. // to format anything dealing with absolute time (i.e. years, months,
  230. // day of week, day of year, timezones, ...)
  231. // * the only valid formats:
  232. // %D - # of days -- NEW !!!
  233. // %H - hour in 24 hour format
  234. // %M - minute (0-59)
  235. // %S - seconds (0-59)
  236. // %% - percent sign
  237. {
  238. TCHAR szBuffer[maxTimeBufferSize];
  239. TCHAR ch;
  240. LPTSTR pch = szBuffer;
  241. while ((ch = *pFormat++) != '\0')
  242. {
  243. ASSERT(pch < &szBuffer[maxTimeBufferSize]);
  244. if (ch == '%')
  245. {
  246. switch (ch = *pFormat++)
  247. {
  248. default:
  249. ASSERT(FALSE); // probably a bad format character
  250. case '%':
  251. *pch++ = ch;
  252. break;
  253. case 'D':
  254. pch += wsprintf(pch, _T("%ld"), GetDay());
  255. break;
  256. case 'H':
  257. pch += wsprintf(pch, _T("%02d"), GetHour());
  258. break;
  259. case 'M':
  260. pch += wsprintf(pch, _T("%02d"), GetMinute());
  261. break;
  262. case 'S':
  263. pch += wsprintf(pch, _T("%02d"), GetSecond());
  264. break;
  265. }
  266. }
  267. else
  268. {
  269. *pch++ = ch;
  270. if (_istlead(ch))
  271. {
  272. ASSERT(pch < &szBuffer[maxTimeBufferSize]);
  273. *pch++ = *pFormat++;
  274. }
  275. }
  276. }
  277. *pch = '\0';
  278. return szBuffer;
  279. }
  280. #ifdef _ALL_CTIME_FORMATS_
  281. CString CTimeSpan::Format(UINT nFormatID) const
  282. {
  283. CString strFormat;
  284. VERIFY(strFormat.LoadString(nFormatID) != 0);
  285. return Format(strFormat);
  286. }
  287. CString CTime::Format(LPCTSTR pFormat) const
  288. {
  289. TCHAR szBuffer[maxTimeBufferSize];
  290. struct tm* ptmTemp = localtime(&m_time);
  291. if (ptmTemp == NULL ||
  292. !_tcsftime(szBuffer, _countof(szBuffer), pFormat, ptmTemp))
  293. szBuffer[0] = '\0';
  294. return szBuffer;
  295. }
  296. CString CTime::FormatGmt(LPCTSTR pFormat) const
  297. {
  298. TCHAR szBuffer[maxTimeBufferSize];
  299. struct tm* ptmTemp = gmtime(&m_time);
  300. if (ptmTemp == NULL ||
  301. !_tcsftime(szBuffer, _countof(szBuffer), pFormat, ptmTemp))
  302. szBuffer[0] = '\0';
  303. return szBuffer;
  304. }
  305. CString CTime::Format(UINT nFormatID) const
  306. {
  307. CString strFormat;
  308. VERIFY(strFormat.LoadString(nFormatID) != 0);
  309. return Format(strFormat);
  310. }
  311. CString CTime::FormatGmt(UINT nFormatID) const
  312. {
  313. CString strFormat;
  314. VERIFY(strFormat.LoadString(nFormatID) != 0);
  315. return FormatGmt(strFormat);
  316. }
  317. #ifdef _UNICODE
  318. // These functions are provided for compatibility with MFC 3.x
  319. CString CTime::Format(LPCSTR pFormat) const
  320. {
  321. CString strFormat(pFormat);
  322. return Format((LPCTSTR)strFormat);
  323. }
  324. CString CTime::FormatGmt(LPCSTR pFormat) const
  325. {
  326. CString strFormat(pFormat);
  327. return FormatGmt((LPCTSTR)strFormat);
  328. }
  329. CString CTimeSpan::Format(LPCSTR pFormat) const
  330. {
  331. CString strFormat = pFormat;
  332. return Format((LPCTSTR)strFormat);
  333. }
  334. #endif //_ALL_CTIME_FORMATS_
  335. #endif // _UNICODE
  336. /////////////////////////////////////////////////////////////////////////////