Annotation of mstools/mfc/src/time.cpp, revision 1.1.1.1

1.1       root        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: #ifdef _NTWIN
                     68: CTime::CTime(const _SYSTEMTIME& sysTime)
                     69: {
                     70:        if (sysTime.wYear < 1900)
                     71:        {
                     72:                        time_t time0 = 0L;
                     73:                        CTime timeT(time0);
                     74:                        *this = timeT;
                     75:        }
                     76:        else
                     77:        {
                     78:                CTime timeT(
                     79:                        (int)sysTime.wYear,
                     80:                        (int)sysTime.wMonth,
                     81:                        (int)sysTime.wDay,
                     82:                        (int)sysTime.wHour,
                     83:                        (int)sysTime.wMinute,
                     84:                        (int)sysTime.wSecond
                     85:                );
                     86:                *this = timeT;
                     87:        }
                     88: }
                     89: 
                     90: CTime::CTime(const _FILETIME& fileTime) 
                     91: {      
                     92:        _SYSTEMTIME sysTime;
                     93:        VERIFY(FileTimeToSystemTime((LPFILETIME)&fileTime, (LPSYSTEMTIME)&sysTime));
                     94:        CTime timeT(sysTime);
                     95:        *this = timeT;
                     96: }
                     97: #endif
                     98: 
                     99: CTime
                    100: CTime::GetCurrentTime()
                    101: /*
                    102:   -- return the current system time
                    103: */
                    104: {
                    105:        return CTime(::time(NULL));
                    106: }
                    107: 
                    108: struct tm*
                    109: CTime::GetGmtTm(struct tm* ptm /* = NULL */) const
                    110: /*
                    111:   -- note uses global static buffer
                    112: */
                    113: {
                    114:        if (ptm != NULL)
                    115:                return &(*ptm = *gmtime(&m_time));
                    116:        else
                    117:                return gmtime(&m_time);
                    118: }
                    119: 
                    120: struct tm*
                    121: CTime::GetLocalTm(struct tm* ptm /* = NULL */) const
                    122: /*
                    123:   -- note uses global static buffer
                    124: */
                    125: {
                    126:        if (ptm != NULL)
                    127:                return &(*ptm = *localtime(&m_time));
                    128:        else
                    129:                return localtime(&m_time);
                    130: }
                    131: 
                    132: #ifdef _DEBUG
                    133: CDumpContext&
                    134: operator <<(CDumpContext& dc, CTime time)
                    135: {
                    136:        char* psz = ctime(&time.m_time);
                    137:        if (psz == NULL)
                    138:                return dc << "CTime(invalid #" << time.m_time << ")";
                    139: 
                    140:        // format it
                    141:        psz[24] = '\0';                 // nuke newline
                    142:        return dc << "CTime(\"" << psz << "\")";
                    143: }
                    144: #endif
                    145: 
                    146: CArchive&
                    147: operator <<(CArchive& ar, CTime time)
                    148: {
                    149:        return ar << (DWORD) time.m_time;
                    150: }
                    151: 
                    152: CArchive&
                    153: operator >>(CArchive& ar, CTime& rtime)
                    154: {
                    155:        return ar >> (DWORD&) rtime.m_time;
                    156: }
                    157: 
                    158: 
                    159: /////////////////////////////////////////////////////////////////////////////
                    160: // CTimeSpan - relative time
                    161: 
                    162: #ifdef _DEBUG
                    163: CDumpContext&
                    164: operator <<(CDumpContext& dc, CTimeSpan timeSpan)
                    165: {
                    166:        return dc << "CTimeSpan(" << timeSpan.GetDays() << " days, " <<
                    167:                 timeSpan.GetHours() << " hours, " <<
                    168:                 timeSpan.GetMinutes() << " minutes and " <<
                    169:                 timeSpan.GetSeconds() << " seconds)";
                    170: }
                    171: #endif
                    172: 
                    173: CArchive&
                    174: operator <<(CArchive& ar, CTimeSpan timeSpan)
                    175: {
                    176:        return ar << (DWORD) timeSpan.m_timeSpan;
                    177: }
                    178: 
                    179: CArchive&
                    180: operator >>(CArchive& ar, CTimeSpan& rtimeSpan)
                    181: {
                    182:        return ar >> (DWORD&) rtimeSpan.m_timeSpan;
                    183: }
                    184: 
                    185: 
                    186: /////////////////////////////////////////////////////////////////////////////
                    187: // String formatting
                    188: 
                    189: #ifndef _WINDLL
                    190: 
                    191: #define maxTimeBufferSize               128
                    192:        // Verifies will fail if the needed buffer size is too large
                    193: 
                    194: CString
                    195: CTimeSpan::Format(const char* pFormat)
                    196: /*
                    197:   -- formatting timespans is a little trickier than formatting CTimes
                    198:   -- we are only interested in relative time formats, ie. it is illegal
                    199:         to format anything dealing with absolute time (i.e. years, months,
                    200:         day of week, day of year, timezones, ...)
                    201:   -- only valid formats:
                    202:                %D - # of days -- NEW !!!
                    203:                %H - hour in 24 hour format
                    204:                %M - minute (0-59)
                    205:                %S - seconds (0-59)
                    206:                %% - percent sign
                    207: */
                    208: {
                    209:        char szBuffer[maxTimeBufferSize];
                    210:        char ch;
                    211:        char* pch = szBuffer;
                    212: 
                    213:        while ((ch = *pFormat++) != '\0')
                    214:        {
                    215:                ASSERT(pch < &szBuffer[maxTimeBufferSize]);
                    216:                if (ch == '%')
                    217:                {
                    218:                        int num;
                    219:                        switch (ch = *pFormat++)
                    220:                        {
                    221:                        default:
                    222:                                ASSERT(FALSE);          // probably a bad format character
                    223:                        case '%':
                    224:                                *pch++ = ch;
                    225:                                break;
                    226:                        case 'D':
                    227:                                pch += sprintf(pch, "%ld", GetDays());
                    228:                                break;
                    229:                        case 'H':
                    230:                                num = GetHours();
                    231:                                goto format_num;
                    232:                        case 'M':
                    233:                                num = GetMinutes();
                    234:                                goto format_num;
                    235:                        case 'S':
                    236:                                num = GetSeconds();
                    237: format_num:
                    238:                                pch += sprintf(pch, "%02d", num);
                    239:                                break;
                    240:                        }
                    241:                }
                    242:                else
                    243:                {
                    244:                        *pch++ = ch;
                    245:                }
                    246:        }
                    247:        *pch = '\0';
                    248: 
                    249:        return szBuffer;
                    250: }
                    251: 
                    252: CString
                    253: CTime::Format(const char* pFormat)
                    254: {
                    255:        char    szBuffer[maxTimeBufferSize];
                    256: 
                    257:        VERIFY(strftime(szBuffer, sizeof(szBuffer), pFormat,
                    258:                localtime(&m_time)));
                    259:        return szBuffer;
                    260: }
                    261: 
                    262: CString
                    263: CTime::FormatGmt(const char* pFormat)
                    264: {
                    265:        char    szBuffer[maxTimeBufferSize];
                    266: 
                    267:        VERIFY(strftime(szBuffer, sizeof(szBuffer), pFormat,
                    268:                gmtime(&m_time)));
                    269:        return szBuffer;
                    270: }
                    271: #endif //_WINDLL
                    272: 
                    273: /////////////////////////////////////////////////////////////////////////////

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.