home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / TIMECORE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  7.5 KB  |  327 lines

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