TNB Library
TnbTime.h
[詳解]
1#pragma once
11#include "TnbStr.h"
12#include "TnbComparable.h"
13#ifndef _WIN32_WCE
14 #include <time.h>
15 #include <tchar.h>
16#endif
17
18
19
20//TNB Library
21namespace TNB
22{
23
24
25
44class CFileTimeEx : public FILETIME, public IComparableT<TNB::CFileTimeEx>
45{
46 DEFSUPER(FILETIME);
47public:
48
53 CFileTimeEx(void) : _super()
54 {
55 Empty();
56 }
57
63 CFileTimeEx(bool b) : _super()
64 {
66 }
67
72 CFileTimeEx(const FILETIME& ft) : _super()
73 {
74 operator=(ft);
75 }
76
81 CFileTimeEx(const SYSTEMTIME& st) : _super()
82 {
83 operator=(st);
84 }
85
90 CFileTimeEx(const time_t& tm) : _super()
91 {
92 operator=(tm);
93 }
94
101 {
102 _super::dwLowDateTime = ft.dwLowDateTime;
103 _super::dwHighDateTime = ft.dwHighDateTime;
104 return *this;
105 }
106
113 {
114 FILETIME ft;
115 if ( ! ::SystemTimeToFileTime(&st, &ft) || ! ::LocalFileTimeToFileTime(&ft, this) )
116 {
117 Empty();
118 }
119 return *this;
120 }
121
127 CFileTimeEx& operator=(const time_t& tm)
128 {
129 ULONGLONG ull = tm;
130 ull += 11644473600;
131 ull *= 10000000;
132 m_Refer() = ull;
133 return *this;
134 }
135
140 time_t ToTimet(void)
141 {
142 ULONGLONG ull = m_Refer();
143 ull /= 10000000;
144 ull -= 11644473600;
145 return static_cast<time_t>(ull & DWORD_MAX);
146 }
147
151 void Empty(void)
152 {
153 m_Refer() = 0;
154 }
155
161 bool IsEmpty(void) const
162 {
163 return m_Refer() == 0;
164 }
165
173 virtual INT_PTR Compare(const CFileTimeEx& t) const
174 {
175 return ::CompareFileTime(this, &t);
176 }
177
183 CFileTimeEx& operator+=(LONGLONG llMs)
184 {
185 (m_Refer()) += (llMs * 10000);
186 return *this;
187 }
188
194 CFileTimeEx& operator-=(LONGLONG llMs)
195 {
196 (m_Refer()) -= (llMs * 10000);
197 return *this;
198 }
199
205 CFileTimeEx operator+(LONGLONG llMs) const
206 {
207 CFileTimeEx t = *this;
208 return t += llMs;
209 }
210
216 CFileTimeEx operator-=(LONGLONG llMs) const
217 {
218 CFileTimeEx t = *this;
219 return t -= llMs;
220 }
221
227 LONGLONG operator-(const CFileTimeEx& t) const
228 {
229 LONGLONG r = (m_Refer() - t.m_Refer()) / 10000;
230 return r;
231 }
232
239 CStr Format(LPCTSTR lpszFormat = NULL) const;
240
245 operator FILETIME*(void)
246 {
247 return this;
248 }
249
254 static CFileTimeEx GetCurrent(void);
255
256private:
258 const ULONGLONG& m_Refer(void) const
259 {
260 return *reinterpret_cast<const ULONGLONG*>(&(_super::dwLowDateTime));
261 }
263 ULONGLONG& m_Refer(void)
264 {
265 return *reinterpret_cast<ULONGLONG*>(&(_super::dwLowDateTime));
266 }
267};
268
269
270
293class CSystemTime : public SYSTEMTIME, public IComparableT<TNB::CSystemTime>
294{
295 DEFSUPER(SYSTEMTIME);
296public:
297
302 CSystemTime(void) : _super()
303 {
304 Empty();
305 }
306
311 CSystemTime(const SYSTEMTIME& st) : _super()
312 {
313 operator=(st);
314 }
315
320 CSystemTime(const FILETIME& ft) : _super()
321 {
322 operator=(ft);
323 }
324
329 CSystemTime(const time_t& tm) : _super()
330 {
331 operator=(tm);
332 }
333
340 {
341 ASSERTLIB( offsetof(CSystemTime, wYear) == sizeof(this) ); //wYearが vtable の次にあることを確認
342 SYSTEMTIME* P = this;
343 *P = st;
344 return *this;
345 }
346
353 {
354 FILETIME ff;
355 if ( ! ::FileTimeToLocalFileTime(&ft, &ff) || ! ::FileTimeToSystemTime(&ff, this) )
356 {
357 Empty();
358 }
359 return *this;
360 }
361
367 CSystemTime& operator=(const time_t& tm)
368 {
369 CFileTimeEx ft(tm);
370 return operator=(ft);
371 }
372
380 void Set(int iYear, int iMonth, int iDay)
381 {
382 Empty();
383 _super::wYear = ToWord(iYear);
384 _super::wMonth = ToWord(iMonth);
385 _super::wDay = ToWord(iDay);
386 }
387
389 WORD GetYear(void) const { return _super::wYear; }
391 WORD GetMonth(void) const { return _super::wMonth; }
393 WORD GetDay(void) const { return _super::wDay; }
395 WORD GetHour(void) const { return _super::wHour; }
397 WORD GetMinute(void) const { return _super::wMinute; }
399 WORD GetSecond(void) const { return _super::wSecond; }
401 WORD GetMillSec(void) const { return _super::wMilliseconds; }
402
406 void Empty(void)
407 {
408 memset(&(_super::wYear), 0, sizeof(SYSTEMTIME));
409 }
410
416 bool IsEmpty(void) const
417 {
418 return _super::wYear == 0;
419 }
420
428 bool IsValid(void) const
429 {
430 FILETIME ft;
431 return !! ::SystemTimeToFileTime(this, &ft);
432 }
433
441 virtual INT_PTR Compare(const CSystemTime& t) const
442 {
443 int r = wYear - t.wYear;
444 if ( r != 0 ) { return r; }
445 r = wMonth - t.wMonth;
446 if ( r != 0 ) { return r; }
447 r = wDay - t.wDay;
448 if ( r != 0 ) { return r; }
449 r = wHour - t.wHour;
450 if ( r != 0 ) { return r; }
451 r = wMinute - t.wMinute;
452 if ( r != 0 ) { return r; }
453 r = wSecond - t.wSecond;
454 if ( r != 0 ) { return r; }
455 return wMilliseconds - t.wMilliseconds;
456 }
457
464 {
465 int y = _super::wYear + iYear;
466 _super::wYear = static_cast<WORD>(y);
467 return *this;
468 }
469
476 {
477 if ( iMonth != 0 )
478 {
479 FILETIME ft;
480 bool boRc = !! ::SystemTimeToFileTime(this, &ft);
481 iMonth += _super::wMonth;
482 while ( iMonth > 12 )
483 {
484 iMonth -= 12;
485 _super::wYear++;
486 }
487 while ( iMonth < 1 )
488 {
489 iMonth += 12;
490 _super::wYear--;
491 }
492 _super::wMonth = static_cast<WORD>(iMonth);
493 if ( boRc )
494 {
495 while ( ! ::SystemTimeToFileTime(this, &ft) )
496 {
497 _super::wDay--;
498 if ( _super::wDay == 0 )
499 {
500 Empty();
501 break;
502 }
503 }
504 }
505 }
506 return *this;
507 }
508
515 {
516 return AddHour(iDay * 24);
517 }
518
525 {
526 return AddMinute(iHour * 60);
527 }
528
534 CSystemTime& AddMinute(int iMinute)
535 {
536 return AddSecond(iMinute * 60);
537 }
538
544 CSystemTime& AddSecond(LONGLONG iSecond)
545 {
546 return AddMillSec(iSecond * 1000);
547 }
548
554 CSystemTime& AddMillSec(LONGLONG llMillSec)
555 {
556 return operator=(CFileTimeEx(*this) + llMillSec);
557 }
558
564 LONGLONG operator-(const CSystemTime& t) const
565 {
566 return CFileTimeEx(*this) - CFileTimeEx(t);
567 }
568
575 {
576 FILETIME ft1;
577 FILETIME ft2;
578 CSystemTime st;
579 if ( ! ::SystemTimeToFileTime(this, &ft1)
580 || ! ::LocalFileTimeToFileTime(&ft1, &ft2)
581 || ! ::FileTimeToSystemTime(&ft2, &st) )
582 {
583 st.Empty();
584 }
585 return st;
586 }
587
594 {
595 FILETIME ft1;
596 FILETIME ft2;
597 CSystemTime st;
598 if ( ! ::SystemTimeToFileTime(this, &ft1)
599 || ! ::FileTimeToLocalFileTime(&ft1, &ft2)
600 || ! ::FileTimeToSystemTime(&ft2, &st) )
601 {
602 st.Empty();
603 }
604 return st;
605 }
606
607 #ifndef _WIN32_WCE
608
617 CSystemTime LocalToTzSpecificLocal(LPTIME_ZONE_INFORMATION lpTzInfo) const
618 {
619 CSystemTime r;
620 CSystemTime st = LocalToUtc();
621 if ( ! ::SystemTimeToTzSpecificLocalTime(lpTzInfo, &r, &st) )
622 {
623 r.Empty();
624 }
625 return r;
626 }
627
628 #endif
629
642 void StringTo(LPCTSTR lpsz)
643 {
644 CStr s = lpsz;
645 s.Remove(_T('-'));
646 s.Remove(_T(':'));
647 s.Remove(_T('/'));
648 s.Remove(_T('.'));
649 s.Remove(_T('\''));
650 s.Remove(_T(' '));
651 s += _T("000000000000000000");
652 _super::wYear = static_cast<WORD>(s.Mid(0, 4).ToInt());
653 _super::wMonth = static_cast<WORD>(s.Mid(4, 2).ToInt());
654 _super::wDay = static_cast<WORD>(s.Mid(6, 2).ToInt());
655 _super::wHour = static_cast<WORD>(s.Mid(8, 2).ToInt());
656 _super::wMinute = static_cast<WORD>(s.Mid(10, 2).ToInt());
657 _super::wSecond = static_cast<WORD>(s.Mid(12, 2).ToInt());
658 _super::wMilliseconds = static_cast<WORD>(s.Mid(14, 3).ToInt());
659 _super::wDayOfWeek = 0;
660 }
661
668 CStr Format(LPCTSTR lpszFormat = NULL) const
669 {
670 if ( lpszFormat == NULL )
671 {
672 lpszFormat = _T("%Y-%m-%d %H:%M:%S");
673 }
674 CStr s;
675 if ( IsValid() )
676 {
677 tm tTime;
678 tTime.tm_year = wYear - 1900; /* years since 1900 */
679 tTime.tm_mon = wMonth - 1; /* months since January - [0,11] */
680 tTime.tm_mday = wDay; /* day of the month - [1,31] */
681 tTime.tm_hour = wHour; /* hours since midnight - [0,23] */
682 tTime.tm_min = wMinute; /* minutes after the hour - [0,59] */
683 tTime.tm_sec = wSecond; /* seconds after the minute - [0,59] */
684 if ( _tcsftime(s.GetBuffer(1024), 1024, lpszFormat, &tTime) == 0 )
685 {
686 s.Empty();
687 }
688 s.ReleaseBuffer();
689 }
690 return s;
691 }
692
698 CStr ToTimeString(void) const
699 {
700 return CStr::Fmt(_T("%02d:%02d:%02d.%03d"), wHour, wMinute, wSecond, wMilliseconds);
701 }
702
717 int ToJapaneseEra(int& _iYear) const
718 {
719 CSystemTime st;
720 st.Set(1868, 1, 25);
721 if ( *this < st )
722 {
723 return -1;
724 }
725 st.Set(1912, 7, 30);
726 if ( *this < st )
727 {
728 //明治だ
729 _iYear = _super::wYear - 1868 +1;
730 return 0;
731 }
732 st.Set(1926, 12, 25);
733 if ( *this < st )
734 {
735 //大正だ
736 _iYear = _super::wYear - 1912 +1;
737 return 1;
738 }
739 st.Set(1989, 1, 7);
740 if ( *this < st )
741 {
742 //昭和だ
743 _iYear = _super::wYear - 1926 +1;
744 return 2;
745 }
746 st.Set(2019, 5, 1);
747 if ( *this < st )
748 {
749 //平成だ
750 _iYear = _super::wYear - 1989 +1;
751 return 3;
752 }
753 _iYear = _super::wYear - 2019 +1;
754 return 4;
755 }
756
762 {
763 SYSTEMTIME st;
764 ::GetLocalTime(&st);
765 return st;
766 }
767
773 {
774 SYSTEMTIME st;
775 ::GetSystemTime(&st);
776 return st;
777 }
778
785 static DWORD GetDayOfTheWeek(const SYSTEMTIME& t)
786 {
787 DWORD dwYear = t.wYear;
788 DWORD dwMonth = t.wMonth;
789 DWORD dwDay = t.wDay;
790 if ( dwMonth == 1 || dwMonth == 2 )
791 {
792 dwMonth += 12;
793 dwYear--;
794 }
795 double d = dwYear + (dwYear / 4) - (dwYear / 100) + (dwYear / 400) + (2.6 * dwMonth) + 1.6 + dwDay;
796 return DWORD(d) % 7;
797 }
798
805 static bool IsLeap(DWORD dwYear)
806 {
807 if ( (dwYear % 4) == 0 && (dwYear % 100) != 0 || (dwYear % 400) == 0 )
808 {
809 return true;
810 }
811 return false;
812 }
813
820 static DWORD GetEndOfMonth(DWORD dwYear, DWORD dwMonth)
821 {
822 switch ( dwMonth )
823 {
824 case 2:
825 return IsLeap(dwYear) ? 29 : 28;
826 case 4:
827 case 6:
828 case 9:
829 case 11:
830 return 30;
831 default:
832 break;
833 }
834 return 31;
835 }
836};
837
838
839
841inline CStr CFileTimeEx::Format(LPCTSTR lpszFormat) const
842{
843 CSystemTime st;
844 ::FileTimeToSystemTime(this, &st);
845 return st.Format(lpszFormat);
846}
847
848
849
852{
853 #ifndef _WIN32_WCE
854 FILETIME ft;
855 ::GetSystemTimeAsFileTime(&ft);
856 return ft;
857 #else
859 #endif
860}
861
862
863
864};//TNB
865
866
867
868#ifdef _TnbDOXYGEN //Document作成用シンボル
869
876{
877 WORD wYear;
878 WORD wMonth;
880 WORD wDay;
881 WORD wHour;
882 WORD wMinute;
883 WORD wSecond;
885};
886
892struct FILETIME
893{
896};
897
898#endif
899
比較機能関係のヘッダ
文字列管理関係のヘッダ
ファイルタイム管理クラス
Definition: TnbTime.h:45
CFileTimeEx(void)
コンストラクタ
Definition: TnbTime.h:53
static CFileTimeEx GetCurrent(void)
[取得] 現在の時間
Definition: TnbTime.h:851
CFileTimeEx operator+(LONGLONG llMs) const
[取得] 時間加算
Definition: TnbTime.h:205
CFileTimeEx operator-=(LONGLONG llMs) const
[取得] 時間減算
Definition: TnbTime.h:216
CStr Format(LPCTSTR lpszFormat=NULL) const
[作成] 文字列作成
Definition: TnbTime.h:841
bool IsEmpty(void) const
[確認] 状態確認
Definition: TnbTime.h:161
CFileTimeEx & operator=(const FILETIME &ft)
[代入] 代入
Definition: TnbTime.h:100
CFileTimeEx(const SYSTEMTIME &st)
代入コンストラクタ
Definition: TnbTime.h:81
CFileTimeEx & operator=(const SYSTEMTIME &st)
[代入] 代入
Definition: TnbTime.h:112
time_t ToTimet(void)
[取得] time_t型で取得
Definition: TnbTime.h:140
LONGLONG operator-(const CFileTimeEx &t) const
[取得] 時間差
Definition: TnbTime.h:227
CFileTimeEx(bool b)
コンストラクタ
Definition: TnbTime.h:63
CFileTimeEx(const FILETIME &ft)
代入コンストラクタ
Definition: TnbTime.h:72
CFileTimeEx(const time_t &tm)
代入コンストラクタ
Definition: TnbTime.h:90
virtual INT_PTR Compare(const CFileTimeEx &t) const
[確認] 比較
Definition: TnbTime.h:173
void Empty(void)
[設定] 空化
Definition: TnbTime.h:151
CFileTimeEx & operator=(const time_t &tm)
[代入] 代入
Definition: TnbTime.h:127
CFileTimeEx & operator-=(LONGLONG llMs)
[取得] 時間減算
Definition: TnbTime.h:194
CFileTimeEx & operator+=(LONGLONG llMs)
[取得] 時間加算
Definition: TnbTime.h:183
void ReleaseBuffer(void)
[操作] 割り当てたバッファを開放.
Definition: TnbStr.h:954
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
Definition: TnbStr.h:1206
int Remove(TYP t)
[処理] 文字削除.
Definition: TnbStr.h:1108
void Empty(void)
[削除] 空化
Definition: TnbStr.h:197
CStrT Mid(INDEX iOffset, size_t iSize=INVALID_SIZE) const
[作成] 範囲取得.
Definition: TnbStr.h:766
int ToInt(INDEX iOffset=0, int iBase=10) const
[取得] 数値(int)へ変換
Definition: TnbStr.h:842
TYP * GetBuffer(size_t iLength=0)
[操作] 書き込みバッファ要求.
Definition: TnbStr.h:914
システムタイム管理クラス
Definition: TnbTime.h:294
static DWORD GetDayOfTheWeek(const SYSTEMTIME &t)
[計算] 曜日計算.
Definition: TnbTime.h:785
CSystemTime LocalToUtc(void) const
[取得] UTC取得
Definition: TnbTime.h:574
virtual INT_PTR Compare(const CSystemTime &t) const
[確認] 比較
Definition: TnbTime.h:441
CSystemTime UtcToLocal(void) const
[取得] ローカルタイム取得
Definition: TnbTime.h:593
CStr Format(LPCTSTR lpszFormat=NULL) const
[作成] 文字列作成
Definition: TnbTime.h:668
static DWORD GetEndOfMonth(DWORD dwYear, DWORD dwMonth)
[計算] 末日計算
Definition: TnbTime.h:820
CSystemTime(const FILETIME &ft)
代入コンストラクタ
Definition: TnbTime.h:320
bool IsEmpty(void) const
[確認] 状態確認
Definition: TnbTime.h:416
CSystemTime & AddMinute(int iMinute)
[加算] 分加算
Definition: TnbTime.h:534
static bool IsLeap(DWORD dwYear)
[確認] 閏年判定
Definition: TnbTime.h:805
CSystemTime(const time_t &tm)
代入コンストラクタ
Definition: TnbTime.h:329
LONGLONG operator-(const CSystemTime &t) const
[取得] 時間差
Definition: TnbTime.h:564
WORD GetSecond(void) const
[取得] 秒取得
Definition: TnbTime.h:399
WORD GetMinute(void) const
[取得] 分取得
Definition: TnbTime.h:397
static CSystemTime GetCurrent(void)
[取得] 現在の時間(LOCAL)
Definition: TnbTime.h:761
CSystemTime LocalToTzSpecificLocal(LPTIME_ZONE_INFORMATION lpTzInfo) const
[取得] 指定ローカルタイム取得
Definition: TnbTime.h:617
WORD GetHour(void) const
[取得] 時取得
Definition: TnbTime.h:395
int ToJapaneseEra(int &_iYear) const
[作成] 和年号作成.
Definition: TnbTime.h:717
void StringTo(LPCTSTR lpsz)
[代入] 文字列による代入.
Definition: TnbTime.h:642
bool IsValid(void) const
[確認] 有効確認.
Definition: TnbTime.h:428
CSystemTime & AddMonth(int iMonth)
[加算] 月加算
Definition: TnbTime.h:475
WORD GetMillSec(void) const
[取得] ミリ秒取得
Definition: TnbTime.h:401
CSystemTime & operator=(const FILETIME &ft)
[代入] 代入
Definition: TnbTime.h:352
CSystemTime & operator=(const SYSTEMTIME &st)
[代入] 代入
Definition: TnbTime.h:339
WORD GetMonth(void) const
[取得] 月取得
Definition: TnbTime.h:391
CSystemTime & AddMillSec(LONGLONG llMillSec)
[加算] ミリ秒加算
Definition: TnbTime.h:554
void Set(int iYear, int iMonth, int iDay)
[代入] 代入.
Definition: TnbTime.h:380
CSystemTime & AddSecond(LONGLONG iSecond)
[加算] 秒加算
Definition: TnbTime.h:544
CSystemTime & AddDay(int iDay)
[加算] 日加算
Definition: TnbTime.h:514
CSystemTime & AddYear(int iYear)
[加算] 年加算
Definition: TnbTime.h:463
void Empty(void)
[設定] 空化
Definition: TnbTime.h:406
CSystemTime & AddHour(int iHour)
[加算] 時加算
Definition: TnbTime.h:524
CSystemTime & operator=(const time_t &tm)
[代入] 代入
Definition: TnbTime.h:367
CStr ToTimeString(void) const
[作成] 文字列作成
Definition: TnbTime.h:698
CSystemTime(void)
コンストラクタ
Definition: TnbTime.h:302
static CSystemTime GetCurrentUtc(void)
[取得] 現在の時間(UTC)
Definition: TnbTime.h:772
WORD GetYear(void) const
[取得] 年取得
Definition: TnbTime.h:389
CSystemTime(const SYSTEMTIME &st)
代入コンストラクタ
Definition: TnbTime.h:311
WORD GetDay(void) const
[取得] 日取得
Definition: TnbTime.h:393
TNB Library
Definition: TnbDoxyTitle.txt:2
ファイルタイム型.
Definition: TnbTime.h:893
DWORD dwHighDateTime
high 32bits
Definition: TnbTime.h:895
DWORD dwLowDateTime
low 32bits
Definition: TnbTime.h:894
システムタイム型.
Definition: TnbTime.h:876
WORD wYear
Definition: TnbTime.h:877
WORD wSecond
Definition: TnbTime.h:883
WORD wMilliseconds
ミリ秒
Definition: TnbTime.h:884
WORD wMonth
Definition: TnbTime.h:878
WORD wHour
Definition: TnbTime.h:881
WORD wDayOfWeek
曜日
Definition: TnbTime.h:879
WORD wDay
Definition: TnbTime.h:880
WORD wMinute
Definition: TnbTime.h:882
比較機能インターフェース.
Definition: TnbComparable.h:54