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