azure-core
datetime.hpp
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // SPDX-License-Identifier: MIT
3 
9 #pragma once
10 
12 
13 #include <chrono>
14 #include <ostream>
15 #include <string>
16 
17 namespace Azure {
18 
19 namespace _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() noexcept;
43  };
44 } // namespace _detail
45 
54 class DateTime final : public _detail::Clock::time_point {
55 
56 private:
57  AZ_CORE_DLLEXPORT static DateTime const SystemClockEpoch;
58 
59  DateTime(
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 
86 public:
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  {
148  DropTrailingZeros,
149 
152  AllDigits,
153 
155  Truncate
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 
205 inline _detail::Clock::time_point _detail::Clock::now() noexcept
206 {
207  return DateTime(std::chrono::system_clock::now());
208 }
209 
210 inline bool operator==(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
211 {
212  return dt == DateTime(tp);
213 }
214 
215 inline bool operator<(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
216 {
217  return dt < DateTime(tp);
218 }
219 
220 inline bool operator<=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
221 {
222  return dt <= DateTime(tp);
223 }
224 
225 inline bool operator!=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
226 {
227  return !(dt == tp);
228 }
229 
230 inline bool operator>(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
231 {
232  return !(dt <= tp);
233 }
234 
235 inline bool operator>=(DateTime const& dt, std::chrono::system_clock::time_point const& tp)
236 {
237  return !(dt < tp);
238 }
239 
240 inline bool operator==(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
241 {
242  return dt == tp;
243 }
244 
245 inline bool operator!=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
246 {
247  return dt != tp;
248 }
249 
250 inline bool operator<(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
251 {
252  return (dt > tp);
253 }
254 
255 inline bool operator<=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
256 {
257  return (dt >= tp);
258 }
259 
260 inline bool operator>(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
261 {
262  return (dt < tp);
263 }
264 
265 inline bool operator>=(std::chrono::system_clock::time_point const& tp, DateTime const& dt)
266 {
267  return (dt <= tp);
268 }
269 
270 namespace Core { namespace _internal {
275  class PosixTimeConverter final {
276  public:
283  static DateTime PosixTimeToDateTime(int64_t posixTime)
284  {
285  return {DateTime(1970) + std::chrono::seconds(posixTime)};
286  }
287 
294  static int64_t DateTimeToPosixTime(DateTime const& dateTime)
295  {
296  // This count starts at the POSIX epoch which is January 1st, 1970 UTC.
297  return std::chrono::duration_cast<std::chrono::seconds>(dateTime - DateTime(1970)).count();
298  }
299 
300  private:
305  PosixTimeConverter() = delete;
306 
312  ~PosixTimeConverter() = delete;
313  };
314 }} // namespace Core::_internal
315 
316 } // namespace Azure
Azure::DateTime::DateFormat
DateFormat
Defines the supported date and time string formats.
Definition: datetime.hpp:163
Azure::DateTime::DateTime
constexpr DateTime()
Constructs a default instance of DateTime (00:00:00.0000000 on January 1st, 0001).
Definition: datetime.hpp:91
Azure::DateTime::DateTime
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
AZ_CORE_DLLEXPORT
#define AZ_CORE_DLLEXPORT
Applies DLL export attribute, when applicable.
Definition: dll_import_export.hpp:93
Azure::DateTime::DateTime
constexpr DateTime(time_point const &timePoint)
Constructs an instance of DateTime from a time_point.
Definition: datetime.hpp:120
Azure
Azure SDK abstractions.
Definition: azure_assert.hpp:55
dll_import_export.hpp
DLL export macro.
Azure::DateTime::DateTime
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
Azure::DateTime
Manages date and time in standardized string formats.
Definition: datetime.hpp:54
Azure::DateTime::TimeFractionFormat
TimeFractionFormat
Defines the format applied to the fraction part of any Azure::DateTime.
Definition: datetime.hpp:146