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