ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
datetime.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
15#include "alib/alib.inl"
16// =========================================== Module ==========================================
17#if ALIB_C20_MODULES
18 module ALib.Time;
19#else
20# include "ALib.Time.H"
21#endif
22// ====================================== Implementation =======================================
23using namespace std::chrono;
24namespace alib::time {
25
26// #################################################################################################
27// Windows OS specific: file time, system time
28// #################################################################################################
29#if defined( _WIN32 ) && !DOXYGEN
30
31// filetime_duration has the same layout as FILETIME; 100ns intervals
32using filetime_duration = duration<int64_t, std::ratio<1, 10000000> >;
33
34// January 1, 1601 (NT epoch) - January 1, 1970 (Unix epoch):
35constexpr duration<int64_t> nt_to_unix_epoch{INT64_C(-11644473600)};
36
37FILETIME DateTime::ToFileTime() const
38{
39 const auto asDuration = duration_cast<filetime_duration>( stamp.time_since_epoch() );
40 const auto withNtEpoch= asDuration - nt_to_unix_epoch;
41 const auto rawCount = withNtEpoch.count();
42
43 FILETIME result;
44 result.dwLowDateTime = static_cast<DWORD>(rawCount); // discards upper bits
45 result.dwHighDateTime = static_cast<DWORD>(rawCount >> 32);
46 return result;
47
48}
49
50ULARGE_INTEGER DateTime::ToFileTimeLI() const
51{
52 FILETIME ft= ToFileTime();
53 ULARGE_INTEGER result;
54 result.HighPart= ft.dwHighDateTime;
55 result.LowPart= ft.dwLowDateTime;
56 return result;
57}
58
59DateTime DateTime::FromFileTime( const FILETIME& fileTime )
60{
61 const filetime_duration ftDuration { int64_t( (uint64_t( fileTime.dwHighDateTime) << 32)
62 | fileTime.dwLowDateTime ) };
63 return DateTime( TTimePoint(ftDuration + nt_to_unix_epoch));
64}
65
66DateTime DateTime::FromFileTime( const ULARGE_INTEGER& ft )
67{
68 FILETIME fileTime;
69 fileTime.dwLowDateTime = ft.LowPart;
70 fileTime.dwHighDateTime = ft.HighPart;
71 return FromFileTime( fileTime );
72}
73
74SYSTEMTIME DateTime::ToSystemTime( lang::Timezone timezone ) const
75{
76 FILETIME ft= ToFileTime();
77 SYSTEMTIME result;
78 if ( timezone == lang::Timezone::UTC )
79 FileTimeToSystemTime( &ft, &result );
80 else
81 {
82 SYSTEMTIME utc;
83 FileTimeToSystemTime( &ft, &utc );
84 SystemTimeToTzSpecificLocalTime( NULL, &utc, &result );
85 }
86 return result;
87}
88
89DateTime DateTime::FromSystemTime( const SYSTEMTIME& st, lang::Timezone timezone )
90{
91 FILETIME ft;
92 if ( timezone == lang::Timezone::UTC )
93 SystemTimeToFileTime( &st, &ft );
94 else
95 {
96 SYSTEMTIME utc;
97 TzSpecificLocalTimeToSystemTime( NULL, &st, &utc);
98 SystemTimeToFileTime( &utc, &ft );
99 }
100 return DateTime::FromFileTime( ft );
101}
102#endif // defined( _WIN32 ) && !DOXYGEN
103} // namespace [alib::time]
104
static ALIB_DLL DateTime FromFileTime(const FILETIME &fileTime)
ALIB_DLL ULARGE_INTEGER ToFileTimeLI() const
ALIB_DLL FILETIME ToFileTime() const
static ALIB_DLL DateTime FromSystemTime(const SYSTEMTIME &systemTime, lang::Timezone timezone=lang::Timezone::Local)
ALIB_DLL SYSTEMTIME ToSystemTime(lang::Timezone timezone=lang::Timezone::Local) 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.inl:224