timecore.cpp 8.0 KB

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