home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SRC / TIMECORE.CP_ / TIMECORE.CP
Encoding:
Text File  |  1993-02-08  |  5.6 KB  |  244 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library. 
  2. // Copyright (C) 1992 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 Microsoft 
  7. // QuickHelp and/or WinHelp 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 BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #ifdef _WINDOWS
  23. #define sprintf wsprintf
  24. #endif //_WINDOWS
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CTime - absolute time
  28.  
  29. CTime::CTime(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec)
  30. {
  31.     struct tm atm;
  32.     atm.tm_sec = nSec;
  33.     atm.tm_min = nMin;
  34.     atm.tm_hour = nHour;
  35.     ASSERT(nDay >= 1 && nDay <= 31);
  36.     atm.tm_mday = nDay;
  37.     ASSERT(nMonth >= 1 && nMonth <= 12);
  38.     atm.tm_mon = nMonth - 1;        // tm_mon is 0 based
  39.     ASSERT(nYear >= 1900);
  40.     atm.tm_year = nYear - 1900;     // tm_year is 1900 based
  41.     atm.tm_isdst = -1;
  42.     m_time = mktime(&atm);
  43.     ASSERT(m_time != -1);       // indicates an illegal input time
  44. }
  45.  
  46. CTime::CTime(WORD wDosDate, WORD wDosTime)
  47. {
  48.     struct tm atm;
  49.     atm.tm_sec = (wDosTime & ~0xFFE0) << 1;;
  50.     atm.tm_min = (wDosTime & ~0xF800) >> 5;
  51.     atm.tm_hour = wDosTime >> 11;
  52.  
  53.     atm.tm_mday = wDosDate & ~0xFFE0;
  54.     atm.tm_mon = ((wDosDate & ~0xFE00) >> 5) - 1;
  55.     atm.tm_year = (wDosDate >> 9) + 80;
  56.     atm.tm_isdst = -1;
  57.     m_time = mktime(&atm);
  58.     ASSERT(m_time != -1);       // indicates an illegal input time
  59. }
  60.  
  61.  
  62. CTime PASCAL
  63. CTime::GetCurrentTime()
  64. // return the current system time
  65. {
  66.     return CTime(::time(NULL));
  67. }
  68.  
  69. struct tm*
  70. CTime::GetGmtTm(struct tm* ptm /* = NULL */) const
  71. // NOTE: uses global static buffer
  72. {
  73.     if (ptm != NULL)
  74.     {
  75.         *ptm = *gmtime(&m_time);
  76.         return ptm;
  77.     }
  78.     else
  79.         return gmtime(&m_time);
  80. }
  81.  
  82. struct tm*
  83. CTime::GetLocalTm(struct tm* ptm /* = NULL */) const
  84. // NOTE: uses global static buffer
  85. {
  86.     if (ptm != NULL)
  87.     {
  88.         struct tm* ptmTemp = localtime(&m_time);
  89.         if (ptmTemp == NULL)
  90.             return NULL;    // indicates the m_time was not initialized!
  91.  
  92.         *ptm = *ptmTemp;
  93.         return ptm;
  94.     }
  95.     else
  96.         return localtime(&m_time);
  97. }
  98.  
  99. #ifdef _DEBUG
  100. CDumpContext&
  101. AFXAPI operator <<(CDumpContext& dc, CTime time)
  102. {
  103.     char* psz = ctime(&time.m_time);
  104.     if ((psz == NULL) || (time.m_time == 0))
  105.         return dc << "CTime(invalid #" << time.m_time << ")";
  106.  
  107.     // format it
  108.     psz[24] = '\0';         // nuke newline
  109.     return dc << "CTime(\"" << psz << "\")";
  110. }
  111. #endif
  112.  
  113. CArchive&
  114. AFXAPI operator <<(CArchive& ar, CTime time)
  115. {
  116.     return ar << (DWORD) time.m_time;
  117. }
  118.  
  119. CArchive&
  120. AFXAPI operator >>(CArchive& ar, CTime& rtime)
  121. {
  122.     return ar >> (DWORD&) rtime.m_time;
  123. }
  124.  
  125.  
  126. /////////////////////////////////////////////////////////////////////////////
  127. // CTimeSpan - relative time
  128.  
  129. #ifdef _DEBUG
  130. CDumpContext&
  131. AFXAPI operator <<(CDumpContext& dc, CTimeSpan timeSpan)
  132. {
  133.     return dc << "CTimeSpan(" << timeSpan.GetDays() << " days, " <<
  134.          timeSpan.GetHours() << " hours, " <<
  135.          timeSpan.GetMinutes() << " minutes and " <<
  136.          timeSpan.GetSeconds() << " seconds)";
  137. }
  138. #endif
  139.  
  140. CArchive&
  141. AFXAPI operator <<(CArchive& ar, CTimeSpan timeSpan)
  142. {
  143.     return ar << (DWORD) timeSpan.m_timeSpan;
  144. }
  145.  
  146. CArchive&
  147. AFXAPI operator >>(CArchive& ar, CTimeSpan& rtimeSpan)
  148. {
  149.     return ar >> (DWORD&) rtimeSpan.m_timeSpan;
  150. }
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // String formatting
  155.  
  156. #if !defined(_AFXDLL) && !defined(_USRDLL)
  157.  
  158. #define maxTimeBufferSize       128
  159.     // Verifies will fail if the needed buffer size is too large
  160.  
  161. CString
  162. CTimeSpan::Format(const char* pFormat) const
  163. // formatting timespans is a little trickier than formatting CTimes
  164. //  * we are only interested in relative time formats, ie. it is illegal
  165. //      to format anything dealing with absolute time (i.e. years, months,
  166. //         day of week, day of year, timezones, ...)
  167. //  * the only valid formats:
  168. //      %D - # of days -- NEW !!!
  169. //      %H - hour in 24 hour format
  170. //      %M - minute (0-59)
  171. //      %S - seconds (0-59)
  172. //      %% - percent sign
  173. {
  174.     char szBuffer[maxTimeBufferSize];
  175.     char ch;
  176.     char* pch = szBuffer;
  177.  
  178.     while ((ch = *pFormat++) != '\0')
  179.     {
  180.         ASSERT(pch < &szBuffer[maxTimeBufferSize]);
  181.         if (ch == '%')
  182.         {
  183.             int num;
  184.             switch (ch = *pFormat++)
  185.             {
  186.             default:
  187.                 ASSERT(FALSE);      // probably a bad format character
  188.             case '%':
  189.                 *pch++ = ch;
  190.                 break;
  191.             case 'D':
  192.                 pch += sprintf(pch, "%ld", GetDays());
  193.                 break;
  194.             case 'H':
  195.                 num = GetHours();
  196.                 goto format_num;
  197.             case 'M':
  198.                 num = GetMinutes();
  199.                 goto format_num;
  200.             case 'S':
  201.                 num = GetSeconds();
  202. format_num:
  203.                 pch += sprintf(pch, "%02d", num);
  204.                 break;
  205.             }
  206.         }
  207.         else
  208.         {
  209.             *pch++ = ch;
  210.         }
  211.     }
  212.     *pch = '\0';
  213.  
  214.     return szBuffer;
  215. }
  216.  
  217. CString
  218. CTime::Format(const char* pFormat) const
  219. {
  220.     char    szBuffer[maxTimeBufferSize];
  221.  
  222.     struct tm* ptmTemp = localtime(&m_time);
  223.     ASSERT(ptmTemp != NULL); // make sure the time has been initialized!
  224.  
  225.     if (!strftime(szBuffer, sizeof(szBuffer), pFormat, ptmTemp))
  226.         szBuffer[0] = '\0';
  227.  
  228.     return szBuffer;
  229. }
  230.  
  231. CString
  232. CTime::FormatGmt(const char* pFormat) const
  233. {
  234.     char    szBuffer[maxTimeBufferSize];
  235.  
  236.     if (!strftime(szBuffer, sizeof(szBuffer), pFormat, gmtime(&m_time)))
  237.         szBuffer[0] = '\0';
  238.  
  239.     return szBuffer;
  240. }
  241. #endif //! DLL variant
  242.  
  243. /////////////////////////////////////////////////////////////////////////////
  244.