ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
datetime.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the \aliblong. It does not belong to an \alibmod and is
4/// included in any \alibbuild.
5///
6/// \emoji :copyright: 2013-2025 A-Worx GmbH, Germany.
7/// Published under \ref mainpage_license "Boost Software License".
8//==================================================================================================
9ALIB_EXPORT namespace alib { namespace time {
10
11//==================================================================================================
12/// This class provides an interface into the system's clock values. In contrast to values of sibling
13/// class \alib{time;Ticks}, the underlying system timer is not guaranteed to be steady.
14/// This means that an object created or set at a later point in time of the program execution,
15/// might represent an earlier point in time.
16/// This might happen when the system clock gets adjusted in-between two measurements.
17///
18/// Therefore, the class is to be used to represent calendar clock values which usually get
19/// converted to human-readable formats (calendar dates and 24/60/60 clock times).
20///
21/// Apart from a few system-dependent conversion methods, the class has no specific interface, but
22/// the methods and operators inherited from base \alib{time;TimePointBase}.
23///
24/// To construct an object of this type using calendrical date and time values, helper-class
25/// \alib{strings::util;CalendarDateTime} with its constructor
26/// \alib{strings::util::CalendarDateTime;CalendarDateTime(int,int,int,int,int,int,int)} is
27/// available in case module \alib_strings is included in the \alibbuild.
28/// A typical construction with this helper could look like this:
29///
30/// DateTime myDate= CalendarDateTime(2025,1,31,14,5).Get( Timezone::UTC );
31///
32/// @see
33/// For this class, a \ref alibtools_debug_helpers_gdb "pretty printer" for the
34/// GNU debugger is provided.
35//==================================================================================================
36class DateTime : public TimePointBase<std::chrono::system_clock, DateTime>
37{
38 public:
39
40//! @cond NO_DOX
41// doxygen bug: generates "return type not documented"
42 /// Use constructors of parent class.
44//! @endcond
45
46 //################################################################################################
47 // Conversion to time platform/language specific values
48 //################################################################################################
49
50 /// Converts the internal value into seconds since January 1, 1970, 00:00:00 GMT.
51 /// The conversion is dependent on time zone and system clock setting of the host.
52 ///
53 /// @return Seconds in the epoch.
54 time_t InEpochSeconds() const { return std::chrono::system_clock::to_time_t( stamp ); }
55
56
57 /// Static method that creates a \b %DateTime object representing the given system
58 /// point in time measured in seconds since January 1st 1970, 00:00:00 GMT.
59 ///
60 /// @param epochSeconds The milliseconds in the epoch to convert.
61 /// @return A time stamp object
62 static
63 DateTime FromEpochSeconds ( time_t epochSeconds )
64 { return DateTime( std::chrono::system_clock::from_time_t( epochSeconds ) ); }
65
66
67 #if defined (_WIN32)
68 //======================================================================================
69 /// Converts the internal value into windows specific file time, a 64-bit value that
70 /// represents the number of 100 nanosecond intervals that have elapsed since
71 /// 12:00 A.M. January 1, 1601 UTC. The conversion is dependent on time zone and system
72 /// clock setting of the host.
73 ///
74 /// \note
75 /// Microsoft Windows specific.
76 ///
77 /// @return The Windows OS file time value represented by this object.
78 //======================================================================================
79 ALIB_DLL FILETIME ToFileTime() const;
80
81 //======================================================================================
82 /// Converts the internal value into windows specific file time, a 64-bit value that
83 /// represents the number of 100 nanosecond intervals that have elapsed since
84 /// 12:00 A.M. January 1, 1601 UTC. The conversion is dependent on time zone and system
85 /// clock setting of the host.
86 ///
87 /// \note
88 /// Microsoft Windows specific.
89 ///
90 /// @return The Windows OS file time represented by this object as type
91 /// struct \b %ULARGE_INTEGER.
92 //======================================================================================
93 ALIB_DLL ULARGE_INTEGER ToFileTimeLI() const;
94
95 //======================================================================================
96 /// Static method that creates a \b %DateTime object representing the given "file time".
97 /// File time is a 64-bit value that represents the number of 100 nanosecond intervals
98 /// that have elapsed since 12:00 A.M. January 1, 1601 UTC.
99 ///
100 /// \note
101 /// Microsoft Windows specific.
102 ///
103 /// @param fileTime The file time to use.
104 /// @return A time stamp object
105 //======================================================================================
106 ALIB_DLL static DateTime FromFileTime( const FILETIME& fileTime );
107
108 //======================================================================================
109 /// Static method that creates a \b %DateTime object representing the given "file time".
110 /// File time is a 64-bit value that represents the number of 100 nanosecond intervals
111 /// that have elapsed since 12:00 A.M. January 1, 1601 UTC.
112 ///
113 /// \note
114 /// Microsoft Windows specific.
115 ///
116 /// @param fileTime The file time to use.
117 /// @return A time stamp object
118 //======================================================================================
119 ALIB_DLL static DateTime FromFileTime( const ULARGE_INTEGER& fileTime );
120
121 //======================================================================================
122 /// Converts the internal value into windows specific system time struct.
123 ///
124 /// \note
125 /// Microsoft Windows specific.
126 ///
127 /// @param timezone Denote if the time that is returned should be local or UTC.
128 /// Defaults to \c TimeZone::Local.
129 /// @return The time point as \b %SYSTEMTIME.
130 //======================================================================================
132
133 //======================================================================================
134 /// Static method that creates a \b %DateTime object representing the given as windows
135 /// system time.
136 ///
137 /// \note
138 /// Microsoft Windows specific.
139 ///
140 /// @param systemTime Pointer to a SYSTEMTIME struct that holds the system time to use.
141 /// @param timezone Denote if the time is interpreted as local or UTC.
142 /// Defaults to \c TimeZone::Local.
143 /// @return Seconds in the epoch.
144 //======================================================================================
145 ALIB_DLL static DateTime FromSystemTime( const SYSTEMTIME& systemTime,
147 #endif
148
149
150 /// Equal to operator.
151 /// @param other The time stamp to compare.
152 /// @return The result of the comparison.
153 bool operator==( const DateTime& other ) const { return stamp == other.stamp; }
154
155
156 /// Not equal to operator.
157 /// @param other The time stamp to compare.
158 /// @return The result of the comparison.
159 bool operator!=( const DateTime& other ) const { return stamp != other.stamp; }
160
161 /// Less than operator.
162 /// @param other The time stamp to compare.
163 /// @return A reference to this object.
164 bool operator<( const DateTime& other ) const { return stamp < other.stamp; }
165
166 /// Less than or equal to operator.
167 /// @param other The time stamp to compare.
168 /// @return The result of the comparison.
169 bool operator<=( const DateTime& other ) const { return stamp <= other.stamp; }
170
171 /// Greater than operator.
172 /// @param other The time stamp to compare.
173 /// @return The result of the comparison.
174 bool operator>( const DateTime& other ) const { return stamp > other.stamp; }
175
176 /// Greater than or equal to operator.
177 /// @param other The time stamp to compare.
178 /// @return The result of the comparison.
179 bool operator>=( const DateTime& other ) const { return stamp >= other.stamp; }
180};
181
182} // namespace time
183
184/// Type alias in namespace \b alib.
186
187} // namespace [alib]
static ALIB_DLL DateTime FromFileTime(const FILETIME &fileTime)
ALIB_DLL ULARGE_INTEGER ToFileTimeLI() const
bool operator!=(const DateTime &other) const
Definition datetime.inl:159
ALIB_DLL FILETIME ToFileTime() const
time_t InEpochSeconds() const
Definition datetime.inl:54
bool operator<(const DateTime &other) const
Definition datetime.inl:164
static ALIB_DLL DateTime FromSystemTime(const SYSTEMTIME &systemTime, lang::Timezone timezone=lang::Timezone::Local)
bool operator<=(const DateTime &other) const
Definition datetime.inl:169
bool operator>=(const DateTime &other) const
Definition datetime.inl:179
static ALIB_DLL DateTime FromFileTime(const ULARGE_INTEGER &fileTime)
ALIB_DLL SYSTEMTIME ToSystemTime(lang::Timezone timezone=lang::Timezone::Local) const
bool operator==(const DateTime &other) const
Definition datetime.inl:153
bool operator>(const DateTime &other) const
Definition datetime.inl:174
static DateTime FromEpochSeconds(time_t epochSeconds)
Definition datetime.inl:63
constexpr TimePointBase(const lang::Initialization init=lang::Initialization::Default)
#define ALIB_DLL
Definition alib.inl:503
#define ALIB_EXPORT
Definition alib.inl:497
Timezone
Denotes whether a time value represents local time or UTC.
@ Local
Denotes local time.
time::DateTime DateTime
Type alias in namespace alib.
Definition datetime.inl:185