ALib C++ Framework
by
Library Version: 2605 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 the \aliblong. It does not belong to an \alibmod and is
4/// included in any \alibbuild.
5///
6/// Copyright 2013-2026 A-Worx GmbH, Germany.
7/// Published under #"mainpage_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 #"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 #"TimePointBase".
23///
24/// To construct an object of this type using calendrical date and time values, helper-class
25/// #"util::CalendarDateTime" with its constructor
26/// #"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 #"alibtools_debug_helpers_gdb;pretty printer" for the
34/// GNU debugger is provided.
35//==================================================================================================
36class DateTime : public TimePointBase<std::chrono::system_clock, DateTime> {
37 public:
38
39 //! @cond NO_DOX
40 // doxygen bug: generates "return type not documented"
41 /// Use constructors of parent class.
43 //! @endcond
44
45 //################################################################################################
46 // Conversion to time platform/language specific values
47 //################################################################################################
48
49 /// Converts the internal value into seconds since January 1, 1970, 00:00:00 GMT.
50 /// The conversion is dependent on time zone and system clock setting of the host.
51 ///
52 /// @return Seconds in the epoch.
53 time_t InEpochSeconds() const { return std::chrono::system_clock::to_time_t( stamp ); }
54
55
56 /// Static method that creates a #"%time::DateTime" object representing the given system
57 /// point in time measured in seconds since January 1st 1970, 00:00:00 GMT.
58 ///
59 /// @param epochSeconds The milliseconds in the epoch to convert.
60 /// @return A time stamp object
61 static
62 DateTime FromEpochSeconds ( time_t epochSeconds )
63 { return DateTime( std::chrono::system_clock::from_time_t( epochSeconds ) ); }
64
65
66 #if defined (_WIN32)
67 //======================================================================================
68 /// Converts the internal value into windows specific file time, a 64-bit value that
69 /// represents the number of 100 nanosecond intervals that have elapsed since
70 /// 12:00 A.M. January 1, 1601 UTC. The conversion is dependent on time zone and system
71 /// clock setting of the host.
72 ///
73 /// \note
74 /// Microsoft Windows specific.
75 ///
76 /// @return The Windows OS file time value represented by this object.
77 //======================================================================================
78 ALIB_DLL FILETIME ToFileTime() const;
79
80 //======================================================================================
81 /// Converts the internal value into windows specific file time, a 64-bit value that
82 /// represents the number of 100 nanosecond intervals that have elapsed since
83 /// 12:00 A.M. January 1, 1601 UTC. The conversion is dependent on time zone and system
84 /// clock setting of the host.
85 ///
86 /// \note
87 /// Microsoft Windows specific.
88 ///
89 /// @return The Windows OS file time represented by this object as type
90 /// struct \b ULARGE_INTEGER.
91 //======================================================================================
92 ALIB_DLL ULARGE_INTEGER ToFileTimeLI() const;
93
94 //======================================================================================
95 /// Static method that creates a #"%time::DateTime" object representing the given
96 /// "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 #"%time::DateTime" object representing the given
110 /// "file time".
111 /// File time is a 64-bit value that represents the number of 100 nanosecond intervals
112 /// that have elapsed since 12:00 A.M. January 1, 1601 UTC.
113 ///
114 /// \note
115 /// Microsoft Windows specific.
116 ///
117 /// @param fileTime The file time to use.
118 /// @return A time stamp object
119 //======================================================================================
120 ALIB_DLL static DateTime FromFileTime( const ULARGE_INTEGER& fileTime );
121
122 //======================================================================================
123 /// Converts the internal value into windows specific system time struct.
124 ///
125 /// \note
126 /// Microsoft Windows specific.
127 ///
128 /// @param timezone Denote if the time that is returned should be local or UTC.
129 /// Defaults to \c TimeZone::Local.
130 /// @return The time point as \c SYSTEMTIME.
131 //======================================================================================
133
134 //======================================================================================
135 /// Static method that creates a #"%time::DateTime" object representing the given as
136 /// windows system time.
137 ///
138 /// \note
139 /// Microsoft Windows specific.
140 ///
141 /// @param systemTime Pointer to a \c SYSTEMTIME struct that holds the system time to
142 /// use.
143 /// @param timezone Denote if the time is interpreted as local or UTC.
144 /// Defaults to #"Timezone::Local;2".
145 /// @return Seconds in the epoch.
146 //======================================================================================
147 ALIB_DLL static DateTime FromSystemTime( const SYSTEMTIME& systemTime,
148 lang::Timezone timezone
150 #endif
151
152
153 /// Equal to operator.
154 /// @param other The time stamp to compare.
155 /// @return The result of the comparison.
156 bool operator==( const DateTime& other ) const { return stamp == other.stamp; }
157
158
159 /// Not equal to operator.
160 /// @param other The time stamp to compare.
161 /// @return The result of the comparison.
162 bool operator!=( const DateTime& other ) const { return stamp != other.stamp; }
163
164 /// Less than operator.
165 /// @param other The time stamp to compare.
166 /// @return A reference to this object.
167 bool operator<( const DateTime& other ) const { return stamp < other.stamp; }
168
169 /// Less than or equal to operator.
170 /// @param other The time stamp to compare.
171 /// @return The result of the comparison.
172 bool operator<=( const DateTime& other ) const { return stamp <= other.stamp; }
173
174 /// Greater than operator.
175 /// @param other The time stamp to compare.
176 /// @return The result of the comparison.
177 bool operator>( const DateTime& other ) const { return stamp > other.stamp; }
178
179 /// Greater than or equal to operator.
180 /// @param other The time stamp to compare.
181 /// @return The result of the comparison.
182 bool operator>=( const DateTime& other ) const { return stamp >= other.stamp; }
183};
184
185} // namespace time
186
187/// Type alias in namespace #"%alib".
189
190} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
bool operator!=(const DateTime &other) const
Definition datetime.hpp:162
static DateTime FromFileTime(const ULARGE_INTEGER &fileTime)
static DateTime FromFileTime(const FILETIME &fileTime)
static DateTime FromSystemTime(const SYSTEMTIME &systemTime, lang::Timezone timezone=lang::Timezone::Local)
ULARGE_INTEGER ToFileTimeLI() const
time_t InEpochSeconds() const
Definition datetime.hpp:53
bool operator<(const DateTime &other) const
Definition datetime.hpp:167
bool operator<=(const DateTime &other) const
Definition datetime.hpp:172
bool operator>=(const DateTime &other) const
Definition datetime.hpp:182
SYSTEMTIME ToSystemTime(lang::Timezone timezone=lang::Timezone::Local) const
bool operator==(const DateTime &other) const
Definition datetime.hpp:156
FILETIME ToFileTime() const
bool operator>(const DateTime &other) const
Definition datetime.hpp:177
static DateTime FromEpochSeconds(time_t epochSeconds)
Definition datetime.hpp:62
constexpr TimePointBase(const lang::Initialization init=lang::Initialization::Default)
Timezone
Denotes whether a time value represents local time or UTC.
@ Local
Denotes local time.
Definition alox.cpp:14
time::DateTime DateTime
Type alias in namespace #"%alib".
Definition datetime.hpp:188