ALib C++ Library
Library Version: 2510 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 //==========================================================================================
51 /// Converts the internal value into seconds since January 1, 1970, 00:00:00 GMT.
52 /// The conversion is dependent on time zone and system clock setting of the host.
53 ///
54 /// @return Seconds in the epoch.
55 //==========================================================================================
56 time_t InEpochSeconds() const
57 {
58 return std::chrono::system_clock::to_time_t( stamp );
59 }
60
61
62 //==========================================================================================
63 /// Static method that creates a \b %DateTime object representing the given system
64 /// point in time measured in seconds since January 1st 1970, 00:00:00 GMT.
65 ///
66 /// @param epochSeconds The milliseconds in the epoch to convert.
67 /// @return A time stamp object
68 //==========================================================================================
69 static
70 DateTime FromEpochSeconds ( time_t epochSeconds )
71 {
72 return DateTime( std::chrono::system_clock::from_time_t( epochSeconds ) );
73 }
74
75
76 #if defined (_WIN32)
77 //======================================================================================
78 /// Converts the internal value into windows specific file time, a 64-bit value that
79 /// represents the number of 100 nanosecond intervals that have elapsed since
80 /// 12:00 A.M. January 1, 1601 UTC. The conversion is dependent on time zone and system
81 /// clock setting of the host.
82 ///
83 /// \note
84 /// Microsoft Windows specific.
85 ///
86 /// @return The Windows OS file time value represented by this object.
87 //======================================================================================
88 ALIB_DLL FILETIME ToFileTime() const;
89
90 //======================================================================================
91 /// Converts the internal value into windows specific file time, a 64-bit value that
92 /// represents the number of 100 nanosecond intervals that have elapsed since
93 /// 12:00 A.M. January 1, 1601 UTC. The conversion is dependent on time zone and system
94 /// clock setting of the host.
95 ///
96 /// \note
97 /// Microsoft Windows specific.
98 ///
99 /// @return The Windows OS file time represented by this object as type
100 /// struct \b %ULARGE_INTEGER.
101 //======================================================================================
102 ALIB_DLL ULARGE_INTEGER ToFileTimeLI() const;
103
104 //======================================================================================
105 /// Static method that creates a \b %DateTime object representing the given "file time".
106 /// File time is a 64-bit value that represents the number of 100 nanosecond intervals
107 /// that have elapsed since 12:00 A.M. January 1, 1601 UTC.
108 ///
109 /// \note
110 /// Microsoft Windows specific.
111 ///
112 /// @param fileTime The file time to use.
113 /// @return A time stamp object
114 //======================================================================================
115 ALIB_DLL static DateTime FromFileTime( const FILETIME& fileTime );
116
117 //======================================================================================
118 /// Static method that creates a \b %DateTime object representing the given "file time".
119 /// File time is a 64-bit value that represents the number of 100 nanosecond intervals
120 /// that have elapsed since 12:00 A.M. January 1, 1601 UTC.
121 ///
122 /// \note
123 /// Microsoft Windows specific.
124 ///
125 /// @param fileTime The file time to use.
126 /// @return A time stamp object
127 //======================================================================================
128 ALIB_DLL static DateTime FromFileTime( const ULARGE_INTEGER& fileTime );
129
130 //======================================================================================
131 /// Converts the internal value into windows specific system time struct.
132 ///
133 /// \note
134 /// Microsoft Windows specific.
135 ///
136 /// @param timezone Denote if the time that is returned should be local or UTC.
137 /// Defaults to \c TimeZone::Local.
138 /// @return The time point as \b %SYSTEMTIME.
139 //======================================================================================
141
142 //======================================================================================
143 /// Static method that creates a \b %DateTime object representing the given as windows
144 /// system time.
145 ///
146 /// \note
147 /// Microsoft Windows specific.
148 ///
149 /// @param systemTime Pointer to a SYSTEMTIME struct that holds the system time to use.
150 /// @param timezone Denote if the time is interpreted as local or UTC.
151 /// Defaults to \c TimeZone::Local.
152 /// @return Seconds in the epoch.
153 //======================================================================================
154 ALIB_DLL static DateTime FromSystemTime( const SYSTEMTIME& systemTime,
156 #endif
157
158
159 //==========================================================================================
160 /// Equal to operator.
161 /// @param other The time stamp to compare.
162 /// @return The result of the comparison.
163 //==========================================================================================
164 bool operator==( const DateTime& other ) const
165 {
166 return stamp == other.stamp;
167 }
168
169
170 //==========================================================================================
171 /// Not equal to operator.
172 /// @param other The time stamp to compare.
173 /// @return The result of the comparison.
174 //==========================================================================================
175 bool operator!=( const DateTime& other ) const
176 {
177 return stamp != other.stamp;
178 }
179
180 //==========================================================================================
181 /// Less than operator.
182 /// @param other The time stamp to compare.
183 /// @return A reference to this object.
184 //==========================================================================================
185 bool operator<( const DateTime& other ) const
186 {
187 return stamp < other.stamp;
188 }
189
190 //==========================================================================================
191 /// Less than or equal to operator.
192 /// @param other The time stamp to compare.
193 /// @return The result of the comparison.
194 //==========================================================================================
195 bool operator<=( const DateTime& other ) const
196 {
197 return stamp <= other.stamp;
198 }
199
200 //==========================================================================================
201 /// Greater than operator.
202 /// @param other The time stamp to compare.
203 /// @return The result of the comparison.
204 //==========================================================================================
205 bool operator>( const DateTime& other ) const
206 {
207 return stamp > other.stamp;
208 }
209
210 //==========================================================================================
211 /// Greater than or equal to operator.
212 /// @param other The time stamp to compare.
213 /// @return The result of the comparison.
214 //==========================================================================================
215 bool operator>=( const DateTime& other ) const
216 {
217 return stamp >= other.stamp;
218 }
219};
220
221} // namespace time
222
223/// Type alias in namespace \b alib.
225
226} // namespace [alib]
227
228
229
static ALIB_DLL DateTime FromFileTime(const FILETIME &fileTime)
ALIB_DLL ULARGE_INTEGER ToFileTimeLI() const
bool operator!=(const DateTime &other) const
Definition datetime.inl:175
ALIB_DLL FILETIME ToFileTime() const
time_t InEpochSeconds() const
Definition datetime.inl:56
bool operator<(const DateTime &other) const
Definition datetime.inl:185
static ALIB_DLL DateTime FromSystemTime(const SYSTEMTIME &systemTime, lang::Timezone timezone=lang::Timezone::Local)
bool operator<=(const DateTime &other) const
Definition datetime.inl:195
bool operator>=(const DateTime &other) const
Definition datetime.inl:215
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:164
bool operator>(const DateTime &other) const
Definition datetime.inl:205
static DateTime FromEpochSeconds(time_t epochSeconds)
Definition datetime.inl:70
constexpr TimePointBase(const lang::Initialization init=lang::Initialization::Default)
#define ALIB_DLL
Definition alib.inl:496
#define ALIB_EXPORT
Definition alib.inl:488
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:224