ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
datetime.cpp
1using namespace std::chrono;
2namespace alib::time {
3
4//##################################################################################################
5// Windows OS specific: file time, system time
6//##################################################################################################
7#if defined( _WIN32 ) && !DOXYGEN
8
9// filetime_duration has the same layout as FILETIME; 100ns intervals
10using filetime_duration = duration<int64_t, std::ratio<1, 10000000> >;
11
12// January 1, 1601 (NT epoch) - January 1, 1970 (Unix epoch):
13constexpr duration<int64_t> nt_to_unix_epoch{INT64_C(-11644473600)};
14
15FILETIME DateTime::ToFileTime() const
16{
17 const auto asDuration = duration_cast<filetime_duration>( stamp.time_since_epoch() );
18 const auto withNtEpoch= asDuration - nt_to_unix_epoch;
19 const auto rawCount = withNtEpoch.count();
20
21 FILETIME result;
22 result.dwLowDateTime = static_cast<DWORD>(rawCount); // discards upper bits
23 result.dwHighDateTime = static_cast<DWORD>(rawCount >> 32);
24 return result;
25
26}
27
28ULARGE_INTEGER DateTime::ToFileTimeLI() const
29{
30 FILETIME ft= ToFileTime();
31 ULARGE_INTEGER result;
32 result.HighPart= ft.dwHighDateTime;
33 result.LowPart= ft.dwLowDateTime;
34 return result;
35}
36
37DateTime DateTime::FromFileTime( const FILETIME& fileTime )
38{
39 const filetime_duration ftDuration { int64_t( (uint64_t( fileTime.dwHighDateTime) << 32)
40 | fileTime.dwLowDateTime ) };
41 return DateTime( TTimePoint(ftDuration + nt_to_unix_epoch));
42}
43
44DateTime DateTime::FromFileTime( const ULARGE_INTEGER& ft )
45{
46 FILETIME fileTime;
47 fileTime.dwLowDateTime = ft.LowPart;
48 fileTime.dwHighDateTime = ft.HighPart;
49 return FromFileTime( fileTime );
50}
51
52SYSTEMTIME DateTime::ToSystemTime( lang::Timezone timezone ) const
53{
54 FILETIME ft= ToFileTime();
55 SYSTEMTIME result;
56 if ( timezone == lang::Timezone::UTC )
57 FileTimeToSystemTime( &ft, &result );
58 else
59 {
60 SYSTEMTIME utc;
61 FileTimeToSystemTime( &ft, &utc );
62 SystemTimeToTzSpecificLocalTime( NULL, &utc, &result );
63 }
64 return result;
65}
66
67DateTime DateTime::FromSystemTime( const SYSTEMTIME& st, lang::Timezone timezone )
68{
69 FILETIME ft;
70 if ( timezone == lang::Timezone::UTC )
71 SystemTimeToFileTime( &st, &ft );
72 else
73 {
74 SYSTEMTIME utc;
75 TzSpecificLocalTimeToSystemTime( NULL, &st, &utc);
76 SystemTimeToFileTime( &utc, &ft );
77 }
78 return DateTime::FromFileTime( ft );
79}
80#endif // defined( _WIN32 ) && !DOXYGEN
81} // namespace [alib::time]
static DateTime FromFileTime(const FILETIME &fileTime)
static DateTime FromSystemTime(const SYSTEMTIME &systemTime, lang::Timezone timezone=lang::Timezone::Local)
ULARGE_INTEGER ToFileTimeLI() const
SYSTEMTIME ToSystemTime(lang::Timezone timezone=lang::Timezone::Local) const
FILETIME ToFileTime() const
typename std::chrono::system_clock::time_point TTimePoint
Timezone
Denotes whether a time value represents local time or UTC.
@ UTC
Denotes UTC (coordinated universal time).
time::DateTime DateTime
Type alias in namespace #"%alib".
Definition datetime.hpp:188