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 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
135 explicit operator std::chrono::system_clock::time_point() const;
136
142 {
145
148 AllDigits,
149
152 };
153
158 enum class DateFormat
159 {
161 Rfc1123,
162
164 Rfc3339,
165 };
166
178 static DateTime Parse(std::string const& dateTime, DateFormat format);
179
187 std::string ToString(DateFormat format = DateFormat::Rfc3339) const;
188
198 std::string ToString(DateFormat format, TimeFractionFormat fractionFormat) const;
199};
200
202inline _detail::Clock::time_point _detail::Clock::now()
203{
204 return DateTime(std::chrono::system_clock::now());
205}
206
208inline bool operator==(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
209{
210 return dt == DateTime(tp);
211}
212
214inline bool operator<(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
215{
216 return dt < DateTime(tp);
217}
218
220inline bool operator<=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
221{
222 return dt <= DateTime(tp);
223}
224
226inline bool operator!=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
227{
228 return !(dt == tp);
229}
230
232inline bool operator>(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
233{
234 return !(dt <= tp);
235}
236
238inline bool operator>=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
239{
240 return !(dt < tp);
241}
242
244inline bool operator==(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
245{
246 return dt == tp;
247}
248
250inline bool operator!=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
251{
252 return dt != tp;
253}
254
256inline bool operator<(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
257{
258 return (dt > tp);
259}
260
262inline bool operator<=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
263{
264 return (dt >= tp);
265}
266
268inline bool operator>(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
269{
270 return (dt < tp);
271}
272
274inline bool operator>=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
275{
276 return (dt <= tp);
277}
278
279namespace Core { namespace _internal {
284 class PosixTimeConverter final {
285 public:
292 static DateTime PosixTimeToDateTime(int64_t posixTime)
293 {
294 return {DateTime(1970) + std::chrono::seconds(posixTime)};
295 }
296
303 static int64_t DateTimeToPosixTime(DateTime const& dateTime)
304 {
305 // This count starts at the POSIX epoch which is January 1st, 1970 UTC.
306 return std::chrono::duration_cast<std::chrono::seconds>(dateTime - DateTime(1970)).count();
307 }
308
309 private:
314 PosixTimeConverter() = delete;
315
321 ~PosixTimeConverter() = delete;
322 };
323}} // namespace Core::_internal
324
325} // namespace Azure
Manages date and time in standardized string formats.
Definition datetime.hpp:54
static DateTime Parse(std::string const &dateTime, DateFormat format)
Create Azure::DateTime from a string representing time in UTC in the specified format.
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
DateTime(std::chrono::system_clock::time_point const &systemTime)
Construct an instance of DateTime from std::chrono::system_clock::time_point.
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:142
@ 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:159
std::string ToString(DateFormat format, TimeFractionFormat fractionFormat) const
Get a string representation of the Azure::DateTime.
operator std::chrono::system_clock::time_point() const
Convert an instance of Azure::DateTime to std::chrono::system_clock::time_point.
std::string ToString(DateFormat format=DateFormat::Rfc3339) const
Get a string representation of the Azure::DateTime.
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:220
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:238
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:232
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:226
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:214
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:208