azure-core
Loading...
Searching...
No Matches
datetime.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9#pragma once
10
11#include "azure/core/dll_import_export.hpp"
12
13#include <chrono>
14#include <ostream>
15#include <string>
16
17namespace Azure {
18
19namespace _detail {
20 class Clock final {
21 public:
22 using rep = int64_t;
23 using period = std::ratio<1, 10000000>;
24 using duration = std::chrono::duration<rep, period>;
25 using time_point = std::chrono::time_point<Clock>;
26
27 // since now() calls system_clock::now(), we have the same to say about the clock steadiness.
28 // system_clock is not a steady clock. It is calendar-based, which means it can be adjusted,
29 // and it may go backwards in time after adjustments, or jump forward faster than the actual
30 // time passes, if you catch the moment before and after syncing the clock.
31 // Steady clock would be good for measuring elapsed time without reboots (or hibernation?).
32 // Steady clock's epoch = boot time, and it would only go forward in steady fashion, after the
33 // system has started.
34 // Using this clock in combination with system_clock is common scenario.
35 // It would not be possible to base this clock on steady_clock and provide an implementation
36 // that universally works in any context in predictable manner. However, it does not mean that
37 // implementation can't use steady_clock in conjunction with this clock: an author can get a
38 // duration between two time_points of this clock (or between system_clock::time point at
39 // this clock's time_point), and add that duration to steady clock's time_point to get a new
40 // time_point in the steady clock's "coordinate system".
41 static constexpr bool is_steady = std::chrono::system_clock::is_steady;
42 static time_point now();
43 };
44} // namespace _detail
45
54class DateTime final : public _detail::Clock::time_point {
55
56private:
57 AZ_CORE_DLLEXPORT static DateTime const SystemClockEpoch;
58
60 int16_t year,
61 int8_t month,
62 int8_t day,
63 int8_t hour,
64 int8_t minute,
65 int8_t second,
66 int32_t fracSec,
67 int8_t dayOfWeek,
68 int8_t localDiffHours,
69 int8_t localDiffMinutes,
70 bool roundFracSecUp = false);
71
72 void ThrowIfUnsupportedYear() const;
73
74 void GetDateTimeParts(
75 int16_t* year,
76 int8_t* month,
77 int8_t* day,
78 int8_t* hour,
79 int8_t* minute,
80 int8_t* second,
81 int32_t* fracSec,
82 int8_t* dayOfWeek) const;
83
84 std::string ToStringRfc1123() const;
85
86public:
91 constexpr DateTime() : time_point() {}
92
105 explicit DateTime(
106 int16_t year,
107 int8_t month = 1,
108 int8_t day = 1,
109 int8_t hour = 0,
110 int8_t minute = 0,
111 int8_t second = 0)
112 : DateTime(year, month, day, hour, minute, second, 0, -1, 0, 0)
113 {
114 }
115
120 constexpr DateTime(time_point const& timePoint) : time_point(timePoint) {}
121
127 DateTime(std::chrono::system_clock::time_point const& systemTime)
128 : DateTime(
129 SystemClockEpoch + std::chrono::duration_cast<duration>(systemTime.time_since_epoch()))
130 {
131 }
132
139 explicit operator std::chrono::system_clock::time_point() const;
140
146 {
149
152 AllDigits,
153
156 };
157
162 enum class DateFormat
163 {
165 Rfc1123,
166
168 Rfc3339,
169 };
170
182 static DateTime Parse(std::string const& dateTime, DateFormat format);
183
191 std::string ToString(DateFormat format = DateFormat::Rfc3339) const;
192
202 std::string ToString(DateFormat format, TimeFractionFormat fractionFormat) const;
203};
204
206inline _detail::Clock::time_point _detail::Clock::now()
207{
208 return DateTime(std::chrono::system_clock::now());
209}
210
212inline bool operator==(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
213{
214 return dt == DateTime(tp);
215}
216
218inline bool operator<(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
219{
220 return dt < DateTime(tp);
221}
222
224inline bool operator<=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
225{
226 return dt <= DateTime(tp);
227}
228
230inline bool operator!=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
231{
232 return !(dt == tp);
233}
234
236inline bool operator>(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
237{
238 return !(dt <= tp);
239}
240
242inline bool operator>=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
243{
244 return !(dt < tp);
245}
246
248inline bool operator==(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
249{
250 return dt == tp;
251}
252
254inline bool operator!=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
255{
256 return dt != tp;
257}
258
260inline bool operator<(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
261{
262 return (dt > tp);
263}
264
266inline bool operator<=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
267{
268 return (dt >= tp);
269}
270
272inline bool operator>(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
273{
274 return (dt < tp);
275}
276
278inline bool operator>=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
279{
280 return (dt <= tp);
281}
282
283namespace Core { namespace _internal {
288 class PosixTimeConverter final {
289 public:
296 static DateTime PosixTimeToDateTime(int64_t posixTime)
297 {
298 return {DateTime(1970) + std::chrono::seconds(posixTime)};
299 }
300
307 static int64_t DateTimeToPosixTime(DateTime const& dateTime)
308 {
309 // This count starts at the POSIX epoch which is January 1st, 1970 UTC.
310 return std::chrono::duration_cast<std::chrono::seconds>(dateTime - DateTime(1970)).count();
311 }
312
313 private:
318 PosixTimeConverter() = delete;
319
325 ~PosixTimeConverter() = delete;
326 };
327}} // namespace Core::_internal
328
329} // namespace Azure
Manages date and time in standardized string formats.
Definition datetime.hpp:54
constexpr DateTime(time_point const &timePoint)
Constructs an instance of DateTime from a time_point.
Definition datetime.hpp:120
DateTime(int16_t year, int8_t month=1, int8_t day=1, int8_t hour=0, int8_t minute=0, int8_t second=0)
Constructs an instance of DateTime.
Definition datetime.hpp:105
operator std::chrono::system_clock::time_point() const
Convert an instance of Azure::DateTime to std::chrono::system_clock::time_point.
Definition datetime.cpp:424
DateTime(std::chrono::system_clock::time_point const &systemTime)
Construct an instance of DateTime from std::chrono::system_clock::time_point.
Definition datetime.hpp:127
constexpr DateTime()
Constructs a default instance of DateTime (00:00:00.0000000 on January 1st, 0001).
Definition datetime.hpp:91
TimeFractionFormat
Defines the format applied to the fraction part of any Azure::DateTime.
Definition datetime.hpp:146
@ Truncate
Drop all the fractional time digits.
@ DropTrailingZeros
Include only meaningful fractional time digits, up to and excluding trailing zeroes.
DateFormat
Defines the supported date and time string formats.
Definition datetime.hpp:163
static DateTime Parse(std::string const &dateTime, DateFormat format)
Create Azure::DateTime from a string representing time in UTC in the specified format.
Definition datetime.cpp:451
std::string ToString(DateFormat format=DateFormat::Rfc3339) const
Get a string representation of the Azure::DateTime.
Definition datetime.cpp:850
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57
bool operator<=(DateTime const &dt, std::chrono::system_clock::time_point const &tp)
Compare a DateTime object with a std::chrono::system_clock::time_point object.
Definition datetime.hpp:224
bool operator>=(DateTime const &dt, std::chrono::system_clock::time_point const &tp)
Compare a DateTime object with a std::chrono::system_clock::time_point object.
Definition datetime.hpp:242
bool operator>(DateTime const &dt, std::chrono::system_clock::time_point const &tp)
Compare a DateTime object with a std::chrono::system_clock::time_point object.
Definition datetime.hpp:236
bool operator!=(DateTime const &dt, std::chrono::system_clock::time_point const &tp)
Compare a DateTime object with a std::chrono::system_clock::time_point object.
Definition datetime.hpp:230
bool operator<(DateTime const &dt, std::chrono::system_clock::time_point const &tp)
Compare a DateTime object with a std::chrono::system_clock::time_point object.
Definition datetime.hpp:218
bool operator==(DateTime const &dt, std::chrono::system_clock::time_point const &tp)
Compare a DateTime object with a std::chrono::system_clock::time_point object.
Definition datetime.hpp:212