ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
datetime.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_time of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_TIME_DATETIME
9#define HPP_ALIB_TIME_DATETIME 1
10#pragma once
12
13namespace alib { namespace time {
14
15//==================================================================================================
16/// This class provides an interface into the system's clock values. In contrast to values of sibling
17/// class \alib{time;Ticks}, the underlying system timer is not guaranteed to be steady.
18/// This means that an object created or set at a later point in time of the program execution,
19/// might represent an earlier point in time.
20/// This might happen when the system clock gets adjusted in-between two measurements.
21///
22/// Therefore, the class is to be used to represent calendar clock values which usually get
23/// converted to human-readable formats (calendar dates and 24/60/60 clock times).
24///
25/// Apart from a few system-dependent conversion methods, the class has no specific interface, but
26/// the methods and operators inherited from base \alib{time;TimePointBase}.
27///
28/// To construct an object of this type using calendrical date an time values, helper-class
29/// \alib{lang::system;CalendarDateTime} with its constructor
30/// \alib{lang::system::CalendarDateTime;CalendarDateTime(int,int,int,int,int,int,int)} is
31/// available if module \alib_basecamp is included in the \alibdist.
32/// A typical construction with this helper could look like this:
33///
34/// DateTime myDate= CalendarDateTime(2023,1,31,14,5).Get( Timezone::UTC );
35///
36/// @see
37/// For this class, a \ref alibtools_debug_helpers_gdb "pretty printer" for the
38/// GNU debugger is provided.
39//==================================================================================================
40class DateTime : public TimePointBase<std::chrono::system_clock, DateTime>
41{
42 public:
43
44//! @cond NO_DOX
45// doxygen bug: generates "return type not documented"
46 /// Use constructors of parent class.
48//! @endcond
49
50 // #############################################################################################
51 // Conversion to time platform/language specific values
52 // #############################################################################################
53
54 //==========================================================================================
55 /// Converts the internal value into seconds since January 1, 1970, 00:00:00 GMT.
56 /// The conversion is dependent on time zone and system clock setting of the host.
57 ///
58 /// @return Seconds in the epoch.
59 //==========================================================================================
60 time_t InEpochSeconds() const
61 {
62 return std::chrono::system_clock::to_time_t( stamp );
63 }
64
65
66 //==========================================================================================
67 /// Static method that creates a \b %DateTime object representing the given system
68 /// point in time measured in seconds since January 1st 1970, 00:00:00 GMT.
69 ///
70 /// @param epochSeconds The milliseconds in the epoch to convert.
71 /// @return A time stamp object
72 //==========================================================================================
73 static
74 DateTime FromEpochSeconds ( time_t epochSeconds )
75 {
76 return DateTime( std::chrono::system_clock::from_time_t( epochSeconds ) );
77 }
78
79
80 #if defined (_WIN32)
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 value represented by this object.
91 //======================================================================================
92 ALIB_API FILETIME ToFileTime() const;
93
94 //======================================================================================
95 /// Converts the internal value into windows specific file time, a 64-bit value that
96 /// represents the number of 100 nanosecond intervals that have elapsed since
97 /// 12:00 A.M. January 1, 1601 UTC. The conversion is dependent on time zone and system
98 /// clock setting of the host.
99 ///
100 /// \note
101 /// Microsoft Windows specific.
102 ///
103 /// @return The Windows OS file time represented by this object as type
104 /// struct \b %ULARGE_INTEGER.
105 //======================================================================================
106 ALIB_API ULARGE_INTEGER ToFileTimeLI() const;
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_API static DateTime FromFileTime( const FILETIME& fileTime );
120
121 //======================================================================================
122 /// Static method that creates a \b %DateTime object representing the given "file time".
123 /// File time is a 64-bit value that represents the number of 100 nanosecond intervals
124 /// that have elapsed since 12:00 A.M. January 1, 1601 UTC.
125 ///
126 /// \note
127 /// Microsoft Windows specific.
128 ///
129 /// @param fileTime The file time to use.
130 /// @return A time stamp object
131 //======================================================================================
132 ALIB_API static DateTime FromFileTime( const ULARGE_INTEGER& fileTime );
133
134 //======================================================================================
135 /// Converts the internal value into windows specific system time struct.
136 ///
137 /// \note
138 /// Microsoft Windows specific.
139 ///
140 /// @param timezone Denote if the time that is returned should be local or UTC.
141 /// Defaults to \c TimeZone::Local.
142 /// @return The time point as \b %SYSTEMTIME.
143 //======================================================================================
145
146 //======================================================================================
147 /// Static method that creates a \b %DateTime object representing the given as windows
148 /// system time.
149 ///
150 /// \note
151 /// Microsoft Windows specific.
152 ///
153 /// @param systemTime Pointer to a SYSTEMTIME struct that holds the system time to use.
154 /// @param timezone Denote if the time is interpreted as local or UTC.
155 /// Defaults to \c TimeZone::Local.
156 /// @return Seconds in the epoch.
157 //======================================================================================
158 ALIB_API static DateTime FromSystemTime( const SYSTEMTIME& systemTime,
160 #endif
161
162
163 //==========================================================================================
164 /// Equal to operator.
165 /// @param other The time stamp to compare.
166 /// @return The result of the comparison.
167 //==========================================================================================
168 bool operator==( const DateTime& other ) const
169 {
170 return stamp == other.stamp;
171 }
172
173
174 //==========================================================================================
175 /// Not equal to operator.
176 /// @param other The time stamp to compare.
177 /// @return The result of the comparison.
178 //==========================================================================================
179 bool operator!=( const DateTime& other ) const
180 {
181 return stamp != other.stamp;
182 }
183
184 //==========================================================================================
185 /// Less than operator.
186 /// @param other The time stamp to compare.
187 /// @return A reference to this object.
188 //==========================================================================================
189 bool operator<( const DateTime& other ) const
190 {
191 return stamp < other.stamp;
192 }
193
194 //==========================================================================================
195 /// Less than or equal to operator.
196 /// @param other The time stamp to compare.
197 /// @return The result of the comparison.
198 //==========================================================================================
199 bool operator<=( const DateTime& other ) const
200 {
201 return stamp <= other.stamp;
202 }
203
204 //==========================================================================================
205 /// Greater than operator.
206 /// @param other The time stamp to compare.
207 /// @return The result of the comparison.
208 //==========================================================================================
209 bool operator>( const DateTime& other ) const
210 {
211 return stamp > other.stamp;
212 }
213
214 //==========================================================================================
215 /// Greater than or equal to operator.
216 /// @param other The time stamp to compare.
217 /// @return The result of the comparison.
218 //==========================================================================================
219 bool operator>=( const DateTime& other ) const
220 {
221 return stamp >= other.stamp;
222 }
223};
224
225} // namespace time
226
227/// Type alias in namespace \b alib.
229
230} // namespace [alib]
231
232#if ALIB_BOXING
233 #if !defined(HPP_ALIB_BOXING_BOXING)
234 # include "alib/boxing/boxing.hpp"
235 #endif
236
237 ALIB_BOXING_VTABLE_DECLARE( alib::DateTime , vt_time_datetime )
238 ALIB_BOXING_VTABLE_DECLARE( alib::DateTime::Duration , vt_time_datetime_duration )
239#endif
240
241#endif // HPP_ALIB_TIME_DATETIME
242
bool operator!=(const DateTime &other) const
Definition datetime.hpp:179
ALIB_API ULARGE_INTEGER ToFileTimeLI() const
bool operator<(const DateTime &other) const
Definition datetime.hpp:189
static ALIB_API DateTime FromFileTime(const FILETIME &fileTime)
static DateTime FromEpochSeconds(time_t epochSeconds)
Definition datetime.hpp:74
time_t InEpochSeconds() const
Definition datetime.hpp:60
static ALIB_API DateTime FromFileTime(const ULARGE_INTEGER &fileTime)
static ALIB_API DateTime FromSystemTime(const SYSTEMTIME &systemTime, lang::Timezone timezone=lang::Timezone::Local)
bool operator>(const DateTime &other) const
Definition datetime.hpp:209
ALIB_API SYSTEMTIME ToSystemTime(lang::Timezone timezone=lang::Timezone::Local) const
bool operator==(const DateTime &other) const
Definition datetime.hpp:168
bool operator<=(const DateTime &other) const
Definition datetime.hpp:199
ALIB_API FILETIME ToFileTime() const
bool operator>=(const DateTime &other) const
Definition datetime.hpp:219
constexpr TimePointBase(const lang::Initialization init=lang::Initialization::Default)
#define ALIB_API
Definition alib.hpp:639
#define ALIB_BOXING_VTABLE_DECLARE(TMapped, Identifier)
Definition vtable.inl:460
Timezone
Denotes whether a time value represents local time or UTC.
@ Local
Denotes local time.
Definition alib.cpp:69
time::DateTime DateTime
Type alias in namespace alib.
Definition datetime.hpp:228