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