1 // PROJECT: ReactOS ATL CTime, CFileTime, CTimeSpan, CFileTimeSpan
2 // LICENSE: Public Domain
3 // PURPOSE: Provides compatibility to Microsoft ATL
4 // PROGRAMMERS: Benedikt Freisen
9 // WARNING: Untested code
28 // leave uninitialized
31 CTimeSpan(__time64_t time
) throw()
36 CTimeSpan(LONG lDays
, int nHours
, int nMins
, int nSecs
) throw()
38 ATLASSERT(lDays
>= 0 && nHours
>= 0 && nHours
<= 23 && nMins
>= 0 && nMins
<= 59 && nSecs
>= 0 && nSecs
<= 59);
39 m_nSpan
= ((((LONGLONG
)lDays
) * 24 + nHours
) * 60 + nMins
) * 60 + nSecs
;
42 CString
Format(LPCSTR pFormat
) const
45 _localtime64_s(&time
, &m_nSpan
);
47 strftime(strTime
.GetBuffer(256), 256, pFormat
, &time
);
48 strTime
.ReleaseBuffer();
49 return CString(strTime
);
52 CString
Format(LPCTSTR pszFormat
) const
55 _localtime64_s(&time
, &m_nSpan
);
58 wcsftime(strTime
.GetBuffer(256), 256, pszFormat
, &time
);
60 strftime(strTime
.GetBuffer(256), 256, pszFormat
, &time
);
62 strTime
.ReleaseBuffer();
66 CString
Format(UINT nID
) const
69 _localtime64_s(&time
, &m_nSpan
);
71 strFormat
.LoadString(nID
);
74 wcsftime(strTime
.GetBuffer(256), 256, strFormat
, &time
);
76 strftime(strTime
.GetBuffer(256), 256, strFormat
, &time
);
78 strTime
.ReleaseBuffer();
82 LONGLONG
GetTotalHours() const throw()
84 return m_nSpan
/ 60 / 60;
87 LONGLONG
GetTotalMinutes() const throw()
92 LONGLONG
GetTotalSeconds() const throw()
97 LONGLONG
GetDays() const throw()
99 return m_nSpan
/ 60 / 60 / 24;
102 LONG
GetHours() const throw()
104 return GetTotalHours() - GetDays() * 24;
107 LONG
GetMinutes() const throw()
109 return GetTotalMinutes() - GetTotalHours() * 60;
112 LONG
GetSeconds() const throw()
114 return GetTotalSeconds() - GetTotalMinutes() * 60;
117 __time64_t
GetTimeSpan() const throw()
122 // CArchive& Serialize64(CArchive& ar) // MFC only
135 // leave uninitialized
138 CTime(__time64_t time
) throw()
143 CTime(int nYear
, int nMonth
, int nDay
, int nHour
, int nMin
, int nSec
, int nDST
= -1)
146 time
.tm_year
= nYear
;
147 time
.tm_mon
= nMonth
;
149 time
.tm_hour
= nHour
;
152 time
.tm_isdst
= nDST
;
153 m_nTime
= _mktime64(&time
);
156 CTime(WORD wDosDate
, WORD wDosTime
, int nDST
= -1)
159 DosDateTimeToFileTime(wDosDate
, wDosTime
, &ft
);
161 FileTimeToSystemTime(&ft
, &st
);
163 time
.tm_year
= st
.wYear
;
164 time
.tm_mon
= st
.wMonth
;
165 time
.tm_wday
= st
.wDayOfWeek
;
166 time
.tm_hour
= st
.wHour
;
167 time
.tm_min
= st
.wMinute
;
168 time
.tm_sec
= st
.wSecond
;
169 time
.tm_isdst
= nDST
;
170 m_nTime
= _mktime64(&time
);
173 CTime(const SYSTEMTIME
& st
, int nDST
= -1) throw()
176 time
.tm_year
= st
.wYear
;
177 time
.tm_mon
= st
.wMonth
;
178 time
.tm_wday
= st
.wDayOfWeek
;
179 time
.tm_hour
= st
.wHour
;
180 time
.tm_min
= st
.wMinute
;
181 time
.tm_sec
= st
.wSecond
;
182 time
.tm_isdst
= nDST
;
183 m_nTime
= _mktime64(&time
);
186 CTime(const FILETIME
& ft
, int nDST
= -1)
189 FileTimeToSystemTime(&ft
, &st
);
191 time
.tm_year
= st
.wYear
;
192 time
.tm_mon
= st
.wMonth
;
193 time
.tm_wday
= st
.wDayOfWeek
;
194 time
.tm_hour
= st
.wHour
;
195 time
.tm_min
= st
.wMinute
;
196 time
.tm_sec
= st
.wSecond
;
197 time
.tm_isdst
= nDST
;
198 m_nTime
= _mktime64(&time
);
201 CTime(const DBTIMESTAMP
& dbts
, int nDST
= -1) throw()
204 time
.tm_year
= dbts
.year
;
205 time
.tm_mon
= dbts
.month
;
206 time
.tm_hour
= dbts
.hour
;
207 time
.tm_min
= dbts
.minute
;
208 time
.tm_sec
= dbts
.second
;
209 time
.tm_isdst
= nDST
;
210 m_nTime
= _mktime64(&time
);
213 CString
Format(LPCTSTR pszFormat
) const
216 _localtime64_s(&time
, &m_nTime
);
219 wcsftime(strTime
.GetBuffer(256), 256, pszFormat
, &time
);
221 strftime(strTime
.GetBuffer(256), 256, pszFormat
, &time
);
223 strTime
.ReleaseBuffer();
227 CString
Format(UINT nFormatID
) const
230 _localtime64_s(&time
, &m_nTime
);
232 strFormat
.LoadString(nFormatID
);
235 wcsftime(strTime
.GetBuffer(256), 256, strFormat
, &time
);
237 strftime(strTime
.GetBuffer(256), 256, strFormat
, &time
);
239 strTime
.ReleaseBuffer();
243 CString
FormatGmt(LPCTSTR pszFormat
) const
246 _gmtime64_s(&time
, &m_nTime
);
249 wcsftime(strTime
.GetBuffer(256), 256, pszFormat
, &time
);
251 strftime(strTime
.GetBuffer(256), 256, pszFormat
, &time
);
253 strTime
.ReleaseBuffer();
257 CString
FormatGmt(UINT nFormatID
) const
260 _gmtime64_s(&time
, &m_nTime
);
262 strFormat
.LoadString(nFormatID
);
265 wcsftime(strTime
.GetBuffer(256), 256, strFormat
, &time
);
267 strftime(strTime
.GetBuffer(256), 256, strFormat
, &time
);
269 strTime
.ReleaseBuffer();
273 bool GetAsDBTIMESTAMP(DBTIMESTAMP
& dbts
) const throw()
276 _gmtime64_s(&time
, &m_nTime
);
277 dbts
.year
= time
.tm_year
;
278 dbts
.month
= time
.tm_mon
;
279 dbts
.day
= time
.tm_mday
;
280 dbts
.hour
= time
.tm_hour
;
281 dbts
.minute
= time
.tm_min
;
282 dbts
.second
= time
.tm_sec
;
284 return true; // TODO: error handling?
287 bool GetAsSystemTime(SYSTEMTIME
& st
) const throw()
290 _gmtime64_s(&time
, &m_nTime
);
291 st
.wYear
= time
.tm_year
;
292 st
.wMonth
= time
.tm_mon
;
293 st
.wDayOfWeek
= time
.tm_wday
;
294 st
.wDay
= time
.tm_mday
;
295 st
.wHour
= time
.tm_hour
;
296 st
.wMinute
= time
.tm_min
;
297 st
.wSecond
= time
.tm_sec
;
298 st
.wMilliseconds
= 0;
299 return true; // TODO: error handling?
302 static CTime WINAPI
GetCurrentTime() throw()
309 int GetDay() const throw()
312 _localtime64_s(&time
, &m_nTime
);
316 int GetDayOfWeek() const throw()
319 _localtime64_s(&time
, &m_nTime
);
323 struct tm
* GetGmtTm(struct tm
* ptm
) const
325 _gmtime64_s(ptm
, &m_nTime
);
329 int GetHour() const throw()
332 _localtime64_s(&time
, &m_nTime
);
336 struct tm
* GetLocalTm(struct tm
* ptm
) const
338 _localtime64_s(ptm
, &m_nTime
);
342 int GetMinute() const throw()
345 _localtime64_s(&time
, &m_nTime
);
349 int GetMonth() const throw()
352 _localtime64_s(&time
, &m_nTime
);
356 int GetSecond() const throw()
359 _localtime64_s(&time
, &m_nTime
);
363 __time64_t
GetTime() const throw()
371 _localtime64_s(&time
, &m_nTime
);
375 // CArchive& Serialize64(CArchive& ar) // MFC only
380 CTime
operator+(CTimeSpan timeSpan
) const throw()
382 return CTime(m_nTime
+ timeSpan
.GetTimeSpan());
385 CTime
operator-(CTimeSpan timeSpan
) const throw()
387 return CTime(m_nTime
- timeSpan
.GetTimeSpan());
390 CTimeSpan
operator-(CTime time
) const throw()
392 return CTimeSpan(m_nTime
- time
.GetTime());
395 CTime
& operator+=(CTimeSpan span
) throw()
397 m_nTime
+= span
.GetTimeSpan();
401 CTime
& operator-=(CTimeSpan span
) throw()
403 m_nTime
-= span
.GetTimeSpan();
407 CTime
& operator=(__time64_t time
) throw()
413 bool operator==(CTime time
) const throw()
415 return m_nTime
== time
.GetTime();
418 bool operator!=(CTime time
) const throw()
420 return m_nTime
!= time
.GetTime();
423 bool operator<(CTime time
) const throw()
425 return m_nTime
< time
.GetTime();
428 bool operator>(CTime time
) const throw()
430 return m_nTime
> time
.GetTime();
433 bool operator<=(CTime time
) const throw()
435 return m_nTime
<= time
.GetTime();
438 bool operator>=(CTime time
) const throw()
440 return m_nTime
>= time
.GetTime();
449 CFileTimeSpan() throw()
454 CFileTimeSpan(const CFileTimeSpan
& span
) throw()
456 m_nSpan
= span
.GetTimeSpan();
459 CFileTimeSpan(LONGLONG nSpan
) throw()
464 LONGLONG
GetTimeSpan() const throw()
469 void SetTimeSpan(LONGLONG nSpan
) throw()
474 CFileTimeSpan
operator-(CFileTimeSpan span
) const throw()
476 return CFileTimeSpan(m_nSpan
- span
.GetTimeSpan());
479 bool operator!=(CFileTimeSpan span
) const throw()
481 return m_nSpan
!= span
.GetTimeSpan();
484 CFileTimeSpan
operator+(CFileTimeSpan span
) const throw()
486 return CFileTimeSpan(m_nSpan
+ span
.GetTimeSpan());
489 CFileTimeSpan
& operator+=(CFileTimeSpan span
) throw()
491 m_nSpan
+= span
.GetTimeSpan();
495 bool operator<(CFileTimeSpan span
) const throw()
497 return m_nSpan
< span
.GetTimeSpan();
500 bool operator<=(CFileTimeSpan span
) const throw()
502 return m_nSpan
<= span
.GetTimeSpan();
505 CFileTimeSpan
& operator=(const CFileTimeSpan
& span
) throw()
507 m_nSpan
= span
.GetTimeSpan();
511 CFileTimeSpan
& operator-=(CFileTimeSpan span
) throw()
513 m_nSpan
-= span
.GetTimeSpan();
517 bool operator==(CFileTimeSpan span
) const throw()
519 return m_nSpan
== span
.GetTimeSpan();
522 bool operator>(CFileTimeSpan span
) const throw()
524 return m_nSpan
> span
.GetTimeSpan();
527 bool operator>=(CFileTimeSpan span
) const throw()
529 return m_nSpan
>= span
.GetTimeSpan();
534 class CFileTime
: public FILETIME
537 static const ULONGLONG Millisecond
= 10000;
538 static const ULONGLONG Second
= Millisecond
* 1000;
539 static const ULONGLONG Minute
= Second
* 60;
540 static const ULONGLONG Hour
= Minute
* 60;
541 static const ULONGLONG Day
= Hour
* 24;
542 static const ULONGLONG Week
= Day
* 7;
546 this->dwLowDateTime
= 0;
547 this->dwHighDateTime
= 0;
550 CFileTime(const FILETIME
& ft
) throw()
552 this->dwLowDateTime
= ft
.dwLowDateTime
;
553 this->dwHighDateTime
= ft
.dwHighDateTime
;
556 CFileTime(ULONGLONG nTime
) throw()
558 this->dwLowDateTime
= (DWORD
) nTime
;
559 this->dwHighDateTime
= nTime
>> 32;
562 static CFileTime
GetCurrentTime() throw()
565 GetSystemTimeAsFileTime(&ft
);
566 return CFileTime(ft
);
569 ULONGLONG
GetTime() const throw()
571 return ((ULONGLONG
)this->dwLowDateTime
) | (((ULONGLONG
)this->dwHighDateTime
) << 32);
574 CFileTime
LocalToUTC() const throw()
577 LocalFileTimeToFileTime(this, &ft
);
578 return CFileTime(ft
);
581 void SetTime(ULONGLONG nTime
) throw()
583 this->dwLowDateTime
= (DWORD
) nTime
;
584 this->dwHighDateTime
= nTime
>> 32;
587 CFileTime
UTCToLocal() const throw()
590 FileTimeToLocalFileTime(this, &ft
);
591 return CFileTime(ft
);
594 CFileTime
operator-(CFileTimeSpan span
) const throw()
596 return CFileTime(this->GetTime() - span
.GetTimeSpan());
599 CFileTimeSpan
operator-(CFileTime ft
) const throw()
601 return CFileTimeSpan(this->GetTime() - ft
.GetTime());
604 bool operator!=(CFileTime ft
) const throw()
606 return this->GetTime() != ft
.GetTime();
609 CFileTime
operator+(CFileTimeSpan span
) const throw()
611 return CFileTime(this->GetTime() + span
.GetTimeSpan());
614 CFileTime
& operator+=(CFileTimeSpan span
) throw()
616 this->SetTime(this->GetTime() + span
.GetTimeSpan());
620 bool operator<(CFileTime ft
) const throw()
622 return this->GetTime() < ft
.GetTime();
625 bool operator<=(CFileTime ft
) const throw()
627 return this->GetTime() <= ft
.GetTime();
630 CFileTime
& operator=(const FILETIME
& ft
) throw()
632 this->dwLowDateTime
= ft
.dwLowDateTime
;
633 this->dwHighDateTime
= ft
.dwHighDateTime
;
637 CFileTime
& operator-=(CFileTimeSpan span
) throw()
639 this->SetTime(this->GetTime() - span
.GetTimeSpan());
643 bool operator==(CFileTime ft
) const throw()
645 return this->GetTime() == ft
.GetTime();
648 bool operator>(CFileTime ft
) const throw()
650 return this->GetTime() > ft
.GetTime();
653 bool operator>=(CFileTime ft
) const throw()
655 return this->GetTime() >= ft
.GetTime();