/src/abseil-cpp/absl/time/time.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2017 The Abseil Authors. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | // ----------------------------------------------------------------------------- |
16 | | // File: time.h |
17 | | // ----------------------------------------------------------------------------- |
18 | | // |
19 | | // This header file defines abstractions for computing with absolute points |
20 | | // in time, durations of time, and formatting and parsing time within a given |
21 | | // time zone. The following abstractions are defined: |
22 | | // |
23 | | // * `absl::Time` defines an absolute, specific instance in time |
24 | | // * `absl::Duration` defines a signed, fixed-length span of time |
25 | | // * `absl::TimeZone` defines geopolitical time zone regions (as collected |
26 | | // within the IANA Time Zone database (https://www.iana.org/time-zones)). |
27 | | // |
28 | | // Note: Absolute times are distinct from civil times, which refer to the |
29 | | // human-scale time commonly represented by `YYYY-MM-DD hh:mm:ss`. The mapping |
30 | | // between absolute and civil times can be specified by use of time zones |
31 | | // (`absl::TimeZone` within this API). That is: |
32 | | // |
33 | | // Civil Time = F(Absolute Time, Time Zone) |
34 | | // Absolute Time = G(Civil Time, Time Zone) |
35 | | // |
36 | | // See civil_time.h for abstractions related to constructing and manipulating |
37 | | // civil time. |
38 | | // |
39 | | // Example: |
40 | | // |
41 | | // absl::TimeZone nyc; |
42 | | // // LoadTimeZone() may fail so it's always better to check for success. |
43 | | // if (!absl::LoadTimeZone("America/New_York", &nyc)) { |
44 | | // // handle error case |
45 | | // } |
46 | | // |
47 | | // // My flight leaves NYC on Jan 2, 2017 at 03:04:05 |
48 | | // absl::CivilSecond cs(2017, 1, 2, 3, 4, 5); |
49 | | // absl::Time takeoff = absl::FromCivil(cs, nyc); |
50 | | // |
51 | | // absl::Duration flight_duration = absl::Hours(21) + absl::Minutes(35); |
52 | | // absl::Time landing = takeoff + flight_duration; |
53 | | // |
54 | | // absl::TimeZone syd; |
55 | | // if (!absl::LoadTimeZone("Australia/Sydney", &syd)) { |
56 | | // // handle error case |
57 | | // } |
58 | | // std::string s = absl::FormatTime( |
59 | | // "My flight will land in Sydney on %Y-%m-%d at %H:%M:%S", |
60 | | // landing, syd); |
61 | | |
62 | | #ifndef ABSL_TIME_TIME_H_ |
63 | | #define ABSL_TIME_TIME_H_ |
64 | | |
65 | | #if !defined(_MSC_VER) |
66 | | #include <sys/time.h> |
67 | | #else |
68 | | // We don't include `winsock2.h` because it drags in `windows.h` and friends, |
69 | | // and they define conflicting macros like OPAQUE, ERROR, and more. This has the |
70 | | // potential to break Abseil users. |
71 | | // |
72 | | // Instead we only forward declare `timeval` and require Windows users include |
73 | | // `winsock2.h` themselves. This is both inconsistent and troublesome, but so is |
74 | | // including 'windows.h' so we are picking the lesser of two evils here. |
75 | | struct timeval; |
76 | | #endif |
77 | | |
78 | | #include "absl/base/config.h" |
79 | | |
80 | | // For feature testing and determining which headers can be included. |
81 | | #if ABSL_INTERNAL_CPLUSPLUS_LANG >= 202002L |
82 | | #include <version> |
83 | | #endif |
84 | | |
85 | | #include <chrono> // NOLINT(build/c++11) |
86 | | #include <cmath> |
87 | | #ifdef __cpp_lib_three_way_comparison |
88 | | #include <compare> |
89 | | #endif // __cpp_lib_three_way_comparison |
90 | | #include <cstdint> |
91 | | #include <ctime> |
92 | | #include <limits> |
93 | | #include <ostream> |
94 | | #include <ratio> // NOLINT(build/c++11) |
95 | | #include <string> |
96 | | #include <type_traits> |
97 | | #include <utility> |
98 | | |
99 | | #include "absl/base/attributes.h" |
100 | | #include "absl/base/macros.h" |
101 | | #include "absl/strings/string_view.h" |
102 | | #include "absl/time/civil_time.h" |
103 | | #include "absl/time/internal/cctz/include/cctz/time_zone.h" |
104 | | |
105 | | #if defined(__cpp_impl_three_way_comparison) && \ |
106 | | defined(__cpp_lib_three_way_comparison) |
107 | | #define ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON 1 |
108 | | #endif |
109 | | |
110 | | namespace absl { |
111 | | ABSL_NAMESPACE_BEGIN |
112 | | |
113 | | class Duration; // Defined below |
114 | | class Time; // Defined below |
115 | | class TimeZone; // Defined below |
116 | | |
117 | | namespace time_internal { |
118 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixDuration(Duration d); |
119 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ToUnixDuration(Time t); |
120 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t GetRepHi(Duration d); |
121 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr uint32_t GetRepLo(Duration d); |
122 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi, |
123 | | uint32_t lo); |
124 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi, |
125 | | int64_t lo); |
126 | | ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration MakePosDoubleDuration(double n); |
127 | | constexpr int64_t kTicksPerNanosecond = 4; |
128 | | constexpr int64_t kTicksPerSecond = 1000 * 1000 * 1000 * kTicksPerNanosecond; |
129 | | template <std::intmax_t N> |
130 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, |
131 | | std::ratio<1, N>); |
132 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, |
133 | | std::ratio<60>); |
134 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, |
135 | | std::ratio<3600>); |
136 | | template <typename T> |
137 | | using EnableIfIntegral = typename std::enable_if< |
138 | | std::is_integral<T>::value || std::is_enum<T>::value, int>::type; |
139 | | template <typename T> |
140 | | using EnableIfFloat = |
141 | | typename std::enable_if<std::is_floating_point<T>::value, int>::type; |
142 | | } // namespace time_internal |
143 | | |
144 | | // Duration |
145 | | // |
146 | | // The `absl::Duration` class represents a signed, fixed-length amount of time. |
147 | | // A `Duration` is generated using a unit-specific factory function, or is |
148 | | // the result of subtracting one `absl::Time` from another. Durations behave |
149 | | // like unit-safe integers and they support all the natural integer-like |
150 | | // arithmetic operations. Arithmetic overflows and saturates at +/- infinity. |
151 | | // `Duration` is trivially destructible and should be passed by value rather |
152 | | // than const reference. |
153 | | // |
154 | | // Factory functions `Nanoseconds()`, `Microseconds()`, `Milliseconds()`, |
155 | | // `Seconds()`, `Minutes()`, `Hours()` and `InfiniteDuration()` allow for |
156 | | // creation of constexpr `Duration` values |
157 | | // |
158 | | // Examples: |
159 | | // |
160 | | // constexpr absl::Duration ten_ns = absl::Nanoseconds(10); |
161 | | // constexpr absl::Duration min = absl::Minutes(1); |
162 | | // constexpr absl::Duration hour = absl::Hours(1); |
163 | | // absl::Duration dur = 60 * min; // dur == hour |
164 | | // absl::Duration half_sec = absl::Milliseconds(500); |
165 | | // absl::Duration quarter_sec = 0.25 * absl::Seconds(1); |
166 | | // |
167 | | // `Duration` values can be easily converted to an integral number of units |
168 | | // using the division operator. |
169 | | // |
170 | | // Example: |
171 | | // |
172 | | // constexpr absl::Duration dur = absl::Milliseconds(1500); |
173 | | // int64_t ns = dur / absl::Nanoseconds(1); // ns == 1500000000 |
174 | | // int64_t ms = dur / absl::Milliseconds(1); // ms == 1500 |
175 | | // int64_t sec = dur / absl::Seconds(1); // sec == 1 (subseconds truncated) |
176 | | // int64_t min = dur / absl::Minutes(1); // min == 0 |
177 | | // |
178 | | // See the `IDivDuration()` and `FDivDuration()` functions below for details on |
179 | | // how to access the fractional parts of the quotient. |
180 | | // |
181 | | // Alternatively, conversions can be performed using helpers such as |
182 | | // `ToInt64Microseconds()` and `ToDoubleSeconds()`. |
183 | | class Duration { |
184 | | public: |
185 | | // Value semantics. |
186 | 0 | constexpr Duration() : rep_hi_(0), rep_lo_(0) {} // zero-length duration |
187 | | |
188 | | // Copyable. |
189 | | #if !defined(__clang__) && defined(_MSC_VER) && _MSC_VER < 1930 |
190 | | // Explicitly defining the constexpr copy constructor avoids an MSVC bug. |
191 | | constexpr Duration(const Duration& d) |
192 | | : rep_hi_(d.rep_hi_), rep_lo_(d.rep_lo_) {} |
193 | | #else |
194 | | constexpr Duration(const Duration& d) = default; |
195 | | #endif |
196 | | Duration& operator=(const Duration& d) = default; |
197 | | |
198 | | // Compound assignment operators. |
199 | | Duration& operator+=(Duration d); |
200 | | Duration& operator-=(Duration d); |
201 | | Duration& operator*=(int64_t r); |
202 | | Duration& operator*=(double r); |
203 | | Duration& operator/=(int64_t r); |
204 | | Duration& operator/=(double r); |
205 | | Duration& operator%=(Duration rhs); |
206 | | |
207 | | // Overloads that forward to either the int64_t or double overloads above. |
208 | | // Integer operands must be representable as int64_t. Integer division is |
209 | | // truncating, so values less than the resolution will be returned as zero. |
210 | | // Floating-point multiplication and division is rounding (halfway cases |
211 | | // rounding away from zero), so values less than the resolution may be |
212 | | // returned as either the resolution or zero. In particular, `d / 2.0` |
213 | | // can produce `d` when it is the resolution and "even". |
214 | | template <typename T, time_internal::EnableIfIntegral<T> = 0> |
215 | 0 | Duration& operator*=(T r) { |
216 | 0 | int64_t x = r; |
217 | 0 | return *this *= x; |
218 | 0 | } |
219 | | |
220 | | template <typename T, time_internal::EnableIfIntegral<T> = 0> |
221 | | Duration& operator/=(T r) { |
222 | | int64_t x = r; |
223 | | return *this /= x; |
224 | | } |
225 | | |
226 | | template <typename T, time_internal::EnableIfFloat<T> = 0> |
227 | | Duration& operator*=(T r) { |
228 | | double x = r; |
229 | | return *this *= x; |
230 | | } |
231 | | |
232 | | template <typename T, time_internal::EnableIfFloat<T> = 0> |
233 | | Duration& operator/=(T r) { |
234 | | double x = r; |
235 | | return *this /= x; |
236 | | } |
237 | | |
238 | | template <typename H> |
239 | | friend H AbslHashValue(H h, Duration d) { |
240 | | return H::combine(std::move(h), d.rep_hi_.Get(), d.rep_lo_); |
241 | | } |
242 | | |
243 | | private: |
244 | | friend constexpr int64_t time_internal::GetRepHi(Duration d); |
245 | | friend constexpr uint32_t time_internal::GetRepLo(Duration d); |
246 | | friend constexpr Duration time_internal::MakeDuration(int64_t hi, |
247 | | uint32_t lo); |
248 | 0 | constexpr Duration(int64_t hi, uint32_t lo) : rep_hi_(hi), rep_lo_(lo) {} |
249 | | |
250 | | // We store `rep_hi_` 4-byte rather than 8-byte aligned to avoid 4 bytes of |
251 | | // tail padding. |
252 | | class HiRep { |
253 | | public: |
254 | | // Default constructor default-initializes `hi_`, which has the same |
255 | | // semantics as default-initializing an `int64_t` (undetermined value). |
256 | | HiRep() = default; |
257 | | |
258 | | HiRep(const HiRep&) = default; |
259 | | HiRep& operator=(const HiRep&) = default; |
260 | | |
261 | | explicit constexpr HiRep(const int64_t value) |
262 | | : // C++17 forbids default-initialization in constexpr contexts. We can |
263 | | // remove this in C++20. |
264 | | #if defined(ABSL_IS_BIG_ENDIAN) && ABSL_IS_BIG_ENDIAN |
265 | | hi_(0), |
266 | | lo_(0) |
267 | | #else |
268 | 0 | lo_(0), |
269 | 0 | hi_(0) |
270 | | #endif |
271 | 0 | { |
272 | 0 | *this = value; |
273 | 0 | } |
274 | | |
275 | 0 | constexpr int64_t Get() const { |
276 | 0 | const uint64_t unsigned_value = |
277 | 0 | (static_cast<uint64_t>(hi_) << 32) | static_cast<uint64_t>(lo_); |
278 | | // `static_cast<int64_t>(unsigned_value)` is implementation-defined |
279 | | // before c++20. On all supported platforms the behaviour is that mandated |
280 | | // by c++20, i.e. "If the destination type is signed, [...] the result is |
281 | | // the unique value of the destination type equal to the source value |
282 | | // modulo 2^n, where n is the number of bits used to represent the |
283 | | // destination type." |
284 | 0 | static_assert( |
285 | 0 | (static_cast<int64_t>((std::numeric_limits<uint64_t>::max)()) == |
286 | 0 | int64_t{-1}) && |
287 | 0 | (static_cast<int64_t>(static_cast<uint64_t>( |
288 | 0 | (std::numeric_limits<int64_t>::max)()) + |
289 | 0 | 1) == |
290 | 0 | (std::numeric_limits<int64_t>::min)()), |
291 | 0 | "static_cast<int64_t>(uint64_t) does not have c++20 semantics"); |
292 | 0 | return static_cast<int64_t>(unsigned_value); |
293 | 0 | } |
294 | | |
295 | 0 | constexpr HiRep& operator=(const int64_t value) { |
296 | | // "If the destination type is unsigned, the resulting value is the |
297 | | // smallest unsigned value equal to the source value modulo 2^n |
298 | | // where `n` is the number of bits used to represent the destination |
299 | | // type". |
300 | 0 | const auto unsigned_value = static_cast<uint64_t>(value); |
301 | 0 | hi_ = static_cast<uint32_t>(unsigned_value >> 32); |
302 | 0 | lo_ = static_cast<uint32_t>(unsigned_value); |
303 | 0 | return *this; |
304 | 0 | } |
305 | | |
306 | | private: |
307 | | // Notes: |
308 | | // - Ideally we would use a `char[]` and `std::bitcast`, but the latter |
309 | | // does not exist (and is not constexpr in `absl`) before c++20. |
310 | | // - Order is optimized depending on endianness so that the compiler can |
311 | | // turn `Get()` (resp. `operator=()`) into a single 8-byte load (resp. |
312 | | // store). |
313 | | #if defined(ABSL_IS_BIG_ENDIAN) && ABSL_IS_BIG_ENDIAN |
314 | | uint32_t hi_; |
315 | | uint32_t lo_; |
316 | | #else |
317 | | uint32_t lo_; |
318 | | uint32_t hi_; |
319 | | #endif |
320 | | }; |
321 | | HiRep rep_hi_; |
322 | | uint32_t rep_lo_; |
323 | | }; |
324 | | |
325 | | // Relational Operators |
326 | | |
327 | | #ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON |
328 | | |
329 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>( |
330 | | Duration lhs, Duration rhs); |
331 | | |
332 | | #endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON |
333 | | |
334 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Duration lhs, |
335 | | Duration rhs); |
336 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>(Duration lhs, |
337 | 0 | Duration rhs) { |
338 | 0 | return rhs < lhs; |
339 | 0 | } |
340 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>=(Duration lhs, |
341 | 0 | Duration rhs) { |
342 | 0 | return !(lhs < rhs); |
343 | 0 | } |
344 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<=(Duration lhs, |
345 | 0 | Duration rhs) { |
346 | 0 | return !(rhs < lhs); |
347 | 0 | } |
348 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Duration lhs, |
349 | | Duration rhs); |
350 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator!=(Duration lhs, |
351 | 0 | Duration rhs) { |
352 | 0 | return !(lhs == rhs); |
353 | 0 | } |
354 | | |
355 | | // Additive Operators |
356 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration operator-(Duration d); |
357 | | ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator+(Duration lhs, |
358 | 0 | Duration rhs) { |
359 | 0 | return lhs += rhs; |
360 | 0 | } |
361 | | ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator-(Duration lhs, |
362 | 0 | Duration rhs) { |
363 | 0 | return lhs -= rhs; |
364 | 0 | } |
365 | | |
366 | | // IDivDuration() |
367 | | // |
368 | | // Divides a numerator `Duration` by a denominator `Duration`, returning the |
369 | | // quotient and remainder. The remainder always has the same sign as the |
370 | | // numerator. The returned quotient and remainder respect the identity: |
371 | | // |
372 | | // numerator = denominator * quotient + remainder |
373 | | // |
374 | | // Returned quotients are capped to the range of `int64_t`, with the difference |
375 | | // spilling into the remainder to uphold the above identity. This means that the |
376 | | // remainder returned could differ from the remainder returned by |
377 | | // `Duration::operator%` for huge quotients. |
378 | | // |
379 | | // See also the notes on `InfiniteDuration()` below regarding the behavior of |
380 | | // division involving zero and infinite durations. |
381 | | // |
382 | | // Example: |
383 | | // |
384 | | // constexpr absl::Duration a = |
385 | | // absl::Seconds(std::numeric_limits<int64_t>::max()); // big |
386 | | // constexpr absl::Duration b = absl::Nanoseconds(1); // small |
387 | | // |
388 | | // absl::Duration rem = a % b; |
389 | | // // rem == absl::ZeroDuration() |
390 | | // |
391 | | // // Here, q would overflow int64_t, so rem accounts for the difference. |
392 | | // int64_t q = absl::IDivDuration(a, b, &rem); |
393 | | // // q == std::numeric_limits<int64_t>::max(), rem == a - b * q |
394 | | int64_t IDivDuration(Duration num, Duration den, Duration* rem); |
395 | | |
396 | | // FDivDuration() |
397 | | // |
398 | | // Divides a `Duration` numerator into a fractional number of units of a |
399 | | // `Duration` denominator. |
400 | | // |
401 | | // See also the notes on `InfiniteDuration()` below regarding the behavior of |
402 | | // division involving zero and infinite durations. |
403 | | // |
404 | | // Example: |
405 | | // |
406 | | // double d = absl::FDivDuration(absl::Milliseconds(1500), absl::Seconds(1)); |
407 | | // // d == 1.5 |
408 | | ABSL_ATTRIBUTE_CONST_FUNCTION double FDivDuration(Duration num, Duration den); |
409 | | |
410 | | // Multiplicative Operators |
411 | | // Integer operands must be representable as int64_t. |
412 | | template <typename T> |
413 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator*(Duration lhs, T rhs) { |
414 | 0 | return lhs *= rhs; |
415 | 0 | } |
416 | | template <typename T> |
417 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator*(T lhs, Duration rhs) { |
418 | 0 | return rhs *= lhs; |
419 | 0 | } Unexecuted instantiation: absl::Duration absl::operator*<int>(int, absl::Duration) Unexecuted instantiation: absl::Duration absl::operator*<long>(long, absl::Duration) Unexecuted instantiation: absl::Duration absl::operator*<double>(double, absl::Duration) |
420 | | template <typename T> |
421 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator/(Duration lhs, T rhs) { |
422 | 0 | return lhs /= rhs; |
423 | 0 | } |
424 | | ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t operator/(Duration lhs, |
425 | 0 | Duration rhs) { |
426 | 0 | return IDivDuration(lhs, rhs, |
427 | 0 | &lhs); // trunc towards zero |
428 | 0 | } |
429 | | ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator%(Duration lhs, |
430 | 0 | Duration rhs) { |
431 | 0 | return lhs %= rhs; |
432 | 0 | } |
433 | | |
434 | | // ZeroDuration() |
435 | | // |
436 | | // Returns a zero-length duration. This function behaves just like the default |
437 | | // constructor, but the name helps make the semantics clear at call sites. |
438 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ZeroDuration() { |
439 | 0 | return Duration(); |
440 | 0 | } |
441 | | |
442 | | // AbsDuration() |
443 | | // |
444 | | // Returns the absolute value of a duration. |
445 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration AbsDuration(Duration d) { |
446 | 0 | return (d < ZeroDuration()) ? -d : d; |
447 | 0 | } |
448 | | |
449 | | // Trunc() |
450 | | // |
451 | | // Truncates a duration (toward zero) to a multiple of a non-zero unit. |
452 | | // |
453 | | // Example: |
454 | | // |
455 | | // absl::Duration d = absl::Nanoseconds(123456789); |
456 | | // absl::Duration a = absl::Trunc(d, absl::Microseconds(1)); // 123456us |
457 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Trunc(Duration d, Duration unit); |
458 | | |
459 | | // Floor() |
460 | | // |
461 | | // Floors a duration using the passed duration unit to its largest value not |
462 | | // greater than the duration. |
463 | | // |
464 | | // Example: |
465 | | // |
466 | | // absl::Duration d = absl::Nanoseconds(123456789); |
467 | | // absl::Duration b = absl::Floor(d, absl::Microseconds(1)); // 123456us |
468 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Floor(Duration d, Duration unit); |
469 | | |
470 | | // Ceil() |
471 | | // |
472 | | // Returns the ceiling of a duration using the passed duration unit to its |
473 | | // smallest value not less than the duration. |
474 | | // |
475 | | // Example: |
476 | | // |
477 | | // absl::Duration d = absl::Nanoseconds(123456789); |
478 | | // absl::Duration c = absl::Ceil(d, absl::Microseconds(1)); // 123457us |
479 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Ceil(Duration d, Duration unit); |
480 | | |
481 | | // InfiniteDuration() |
482 | | // |
483 | | // Returns an infinite `Duration`. To get a `Duration` representing negative |
484 | | // infinity, use `-InfiniteDuration()`. |
485 | | // |
486 | | // Duration arithmetic overflows to +/- infinity and saturates. In general, |
487 | | // arithmetic with `Duration` infinities is similar to IEEE 754 infinities |
488 | | // except where IEEE 754 NaN would be involved, in which case +/- |
489 | | // `InfiniteDuration()` is used in place of a "nan" Duration. |
490 | | // |
491 | | // Examples: |
492 | | // |
493 | | // constexpr absl::Duration inf = absl::InfiniteDuration(); |
494 | | // const absl::Duration d = ... any finite duration ... |
495 | | // |
496 | | // inf == inf + inf |
497 | | // inf == inf + d |
498 | | // inf == inf - inf |
499 | | // -inf == d - inf |
500 | | // |
501 | | // inf == d * 1e100 |
502 | | // inf == inf / 2 |
503 | | // 0 == d / inf |
504 | | // INT64_MAX == inf / d |
505 | | // |
506 | | // d < inf |
507 | | // -inf < d |
508 | | // |
509 | | // // Division by zero returns infinity, or INT64_MIN/MAX where appropriate. |
510 | | // inf == d / 0 |
511 | | // INT64_MAX == d / absl::ZeroDuration() |
512 | | // |
513 | | // The examples involving the `/` operator above also apply to `IDivDuration()` |
514 | | // and `FDivDuration()`. |
515 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration InfiniteDuration(); |
516 | | |
517 | | // Nanoseconds() |
518 | | // Microseconds() |
519 | | // Milliseconds() |
520 | | // Seconds() |
521 | | // Minutes() |
522 | | // Hours() |
523 | | // |
524 | | // Factory functions for constructing `Duration` values from an integral number |
525 | | // of the unit indicated by the factory function's name. The number must be |
526 | | // representable as int64_t. |
527 | | // |
528 | | // NOTE: no "Days()" factory function exists because "a day" is ambiguous. |
529 | | // Civil days are not always 24 hours long, and a 24-hour duration often does |
530 | | // not correspond with a civil day. If a 24-hour duration is needed, use |
531 | | // `absl::Hours(24)`. If you actually want a civil day, use absl::CivilDay |
532 | | // from civil_time.h. |
533 | | // |
534 | | // Example: |
535 | | // |
536 | | // absl::Duration a = absl::Seconds(60); |
537 | | // absl::Duration b = absl::Minutes(1); // b == a |
538 | | template <typename T, time_internal::EnableIfIntegral<T> = 0> |
539 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Nanoseconds(T n) { |
540 | 0 | return time_internal::FromInt64(n, std::nano{}); |
541 | 0 | } Unexecuted instantiation: _ZN4absl11NanosecondsIiTnNSt3__19enable_ifIXoosr3std11is_integralIT_EE5valuesr3std7is_enumIS3_EE5valueEiE4typeELi0EEENS_8DurationES3_ Unexecuted instantiation: _ZN4absl11NanosecondsIlTnNSt3__19enable_ifIXoosr3std11is_integralIT_EE5valuesr3std7is_enumIS3_EE5valueEiE4typeELi0EEENS_8DurationES3_ |
542 | | template <typename T, time_internal::EnableIfIntegral<T> = 0> |
543 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Microseconds(T n) { |
544 | 0 | return time_internal::FromInt64(n, std::micro{}); |
545 | 0 | } Unexecuted instantiation: _ZN4absl12MicrosecondsIiTnNSt3__19enable_ifIXoosr3std11is_integralIT_EE5valuesr3std7is_enumIS3_EE5valueEiE4typeELi0EEENS_8DurationES3_ Unexecuted instantiation: _ZN4absl12MicrosecondsIlTnNSt3__19enable_ifIXoosr3std11is_integralIT_EE5valuesr3std7is_enumIS3_EE5valueEiE4typeELi0EEENS_8DurationES3_ |
546 | | template <typename T, time_internal::EnableIfIntegral<T> = 0> |
547 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Milliseconds(T n) { |
548 | 0 | return time_internal::FromInt64(n, std::milli{}); |
549 | 0 | } Unexecuted instantiation: _ZN4absl12MillisecondsIiTnNSt3__19enable_ifIXoosr3std11is_integralIT_EE5valuesr3std7is_enumIS3_EE5valueEiE4typeELi0EEENS_8DurationES3_ Unexecuted instantiation: _ZN4absl12MillisecondsIlTnNSt3__19enable_ifIXoosr3std11is_integralIT_EE5valuesr3std7is_enumIS3_EE5valueEiE4typeELi0EEENS_8DurationES3_ |
550 | | template <typename T, time_internal::EnableIfIntegral<T> = 0> |
551 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Seconds(T n) { |
552 | 0 | return time_internal::FromInt64(n, std::ratio<1>{}); |
553 | 0 | } Unexecuted instantiation: _ZN4absl7SecondsIlTnNSt3__19enable_ifIXoosr3std11is_integralIT_EE5valuesr3std7is_enumIS3_EE5valueEiE4typeELi0EEENS_8DurationES3_ Unexecuted instantiation: _ZN4absl7SecondsIiTnNSt3__19enable_ifIXoosr3std11is_integralIT_EE5valuesr3std7is_enumIS3_EE5valueEiE4typeELi0EEENS_8DurationES3_ |
554 | | template <typename T, time_internal::EnableIfIntegral<T> = 0> |
555 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Minutes(T n) { |
556 | 0 | return time_internal::FromInt64(n, std::ratio<60>{}); |
557 | 0 | } |
558 | | template <typename T, time_internal::EnableIfIntegral<T> = 0> |
559 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Hours(T n) { |
560 | 0 | return time_internal::FromInt64(n, std::ratio<3600>{}); |
561 | 0 | } |
562 | | |
563 | | // Factory overloads for constructing `Duration` values from a floating-point |
564 | | // number of the unit indicated by the factory function's name. These functions |
565 | | // exist for convenience, but they are not as efficient as the integral |
566 | | // factories, which should be preferred. |
567 | | // |
568 | | // Example: |
569 | | // |
570 | | // auto a = absl::Seconds(1.5); // OK |
571 | | // auto b = absl::Milliseconds(1500); // BETTER |
572 | | template <typename T, time_internal::EnableIfFloat<T> = 0> |
573 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Nanoseconds(T n) { |
574 | | return n * Nanoseconds(1); |
575 | | } |
576 | | template <typename T, time_internal::EnableIfFloat<T> = 0> |
577 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Microseconds(T n) { |
578 | | return n * Microseconds(1); |
579 | | } |
580 | | template <typename T, time_internal::EnableIfFloat<T> = 0> |
581 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Milliseconds(T n) { |
582 | 0 | return n * Milliseconds(1); |
583 | 0 | } |
584 | | template <typename T, time_internal::EnableIfFloat<T> = 0> |
585 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Seconds(T n) { |
586 | | if (n >= 0) { // Note: `NaN >= 0` is false. |
587 | | if (n >= static_cast<T>((std::numeric_limits<int64_t>::max)())) { |
588 | | return InfiniteDuration(); |
589 | | } |
590 | | return time_internal::MakePosDoubleDuration(n); |
591 | | } else { |
592 | | if (std::isnan(n)) return -InfiniteDuration(); |
593 | | if (n <= static_cast<T>((std::numeric_limits<int64_t>::min)())) { |
594 | | return -InfiniteDuration(); |
595 | | } |
596 | | return -time_internal::MakePosDoubleDuration(-n); |
597 | | } |
598 | | } |
599 | | template <typename T, time_internal::EnableIfFloat<T> = 0> |
600 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Minutes(T n) { |
601 | | return n * Minutes(1); |
602 | | } |
603 | | template <typename T, time_internal::EnableIfFloat<T> = 0> |
604 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration Hours(T n) { |
605 | | return n * Hours(1); |
606 | | } |
607 | | |
608 | | // ToInt64Nanoseconds() |
609 | | // ToInt64Microseconds() |
610 | | // ToInt64Milliseconds() |
611 | | // ToInt64Seconds() |
612 | | // ToInt64Minutes() |
613 | | // ToInt64Hours() |
614 | | // |
615 | | // Helper functions that convert a Duration to an integral count of the |
616 | | // indicated unit. These return the same results as the `IDivDuration()` |
617 | | // function, though they usually do so more efficiently; see the |
618 | | // documentation of `IDivDuration()` for details about overflow, etc. |
619 | | // |
620 | | // Example: |
621 | | // |
622 | | // absl::Duration d = absl::Milliseconds(1500); |
623 | | // int64_t isec = absl::ToInt64Seconds(d); // isec == 1 |
624 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Nanoseconds(Duration d); |
625 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Microseconds(Duration d); |
626 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Milliseconds(Duration d); |
627 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Seconds(Duration d); |
628 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Minutes(Duration d); |
629 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Hours(Duration d); |
630 | | |
631 | | // ToDoubleNanoseconds() |
632 | | // ToDoubleMicroseconds() |
633 | | // ToDoubleMilliseconds() |
634 | | // ToDoubleSeconds() |
635 | | // ToDoubleMinutes() |
636 | | // ToDoubleHours() |
637 | | // |
638 | | // Helper functions that convert a Duration to a floating point count of the |
639 | | // indicated unit. These functions are shorthand for the `FDivDuration()` |
640 | | // function above; see its documentation for details about overflow, etc. |
641 | | // |
642 | | // Example: |
643 | | // |
644 | | // absl::Duration d = absl::Milliseconds(1500); |
645 | | // double dsec = absl::ToDoubleSeconds(d); // dsec == 1.5 |
646 | | ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleNanoseconds(Duration d); |
647 | | ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMicroseconds(Duration d); |
648 | | ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMilliseconds(Duration d); |
649 | | ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleSeconds(Duration d); |
650 | | ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMinutes(Duration d); |
651 | | ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleHours(Duration d); |
652 | | |
653 | | // FromChrono() |
654 | | // |
655 | | // Converts any of the pre-defined std::chrono durations to an absl::Duration. |
656 | | // |
657 | | // Example: |
658 | | // |
659 | | // std::chrono::milliseconds ms(123); |
660 | | // absl::Duration d = absl::FromChrono(ms); |
661 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
662 | | const std::chrono::nanoseconds& d); |
663 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
664 | | const std::chrono::microseconds& d); |
665 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
666 | | const std::chrono::milliseconds& d); |
667 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
668 | | const std::chrono::seconds& d); |
669 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
670 | | const std::chrono::minutes& d); |
671 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
672 | | const std::chrono::hours& d); |
673 | | |
674 | | // ToChronoNanoseconds() |
675 | | // ToChronoMicroseconds() |
676 | | // ToChronoMilliseconds() |
677 | | // ToChronoSeconds() |
678 | | // ToChronoMinutes() |
679 | | // ToChronoHours() |
680 | | // |
681 | | // Converts an absl::Duration to any of the pre-defined std::chrono durations. |
682 | | // If overflow would occur, the returned value will saturate at the min/max |
683 | | // chrono duration value instead. |
684 | | // |
685 | | // Example: |
686 | | // |
687 | | // absl::Duration d = absl::Microseconds(123); |
688 | | // auto x = absl::ToChronoMicroseconds(d); |
689 | | // auto y = absl::ToChronoNanoseconds(d); // x == y |
690 | | // auto z = absl::ToChronoSeconds(absl::InfiniteDuration()); |
691 | | // // z == std::chrono::seconds::max() |
692 | | ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::nanoseconds ToChronoNanoseconds( |
693 | | Duration d); |
694 | | ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::microseconds ToChronoMicroseconds( |
695 | | Duration d); |
696 | | ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::milliseconds ToChronoMilliseconds( |
697 | | Duration d); |
698 | | ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::seconds ToChronoSeconds(Duration d); |
699 | | ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::minutes ToChronoMinutes(Duration d); |
700 | | ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::hours ToChronoHours(Duration d); |
701 | | |
702 | | // FormatDuration() |
703 | | // |
704 | | // Returns a string representing the duration in the form "72h3m0.5s". |
705 | | // Returns "inf" or "-inf" for +/- `InfiniteDuration()`. |
706 | | ABSL_ATTRIBUTE_CONST_FUNCTION std::string FormatDuration(Duration d); |
707 | | |
708 | | // Output stream operator. |
709 | 0 | inline std::ostream& operator<<(std::ostream& os, Duration d) { |
710 | 0 | return os << FormatDuration(d); |
711 | 0 | } |
712 | | |
713 | | // Support for StrFormat(), StrCat() etc. |
714 | | template <typename Sink> |
715 | | void AbslStringify(Sink& sink, Duration d) { |
716 | | sink.Append(FormatDuration(d)); |
717 | | } |
718 | | |
719 | | // ParseDuration() |
720 | | // |
721 | | // Parses a duration string consisting of a possibly signed sequence of |
722 | | // decimal numbers, each with an optional fractional part and a unit |
723 | | // suffix. The valid suffixes are "ns", "us" "ms", "s", "m", and "h". |
724 | | // Simple examples include "300ms", "-1.5h", and "2h45m". Parses "0" as |
725 | | // `ZeroDuration()`. Parses "inf" and "-inf" as +/- `InfiniteDuration()`. |
726 | | bool ParseDuration(absl::string_view dur_string, Duration* d); |
727 | | |
728 | | // AbslParseFlag() |
729 | | // |
730 | | // Parses a command-line flag string representation `text` into a Duration |
731 | | // value. Duration flags must be specified in a format that is valid input for |
732 | | // `absl::ParseDuration()`. |
733 | | bool AbslParseFlag(absl::string_view text, Duration* dst, std::string* error); |
734 | | |
735 | | |
736 | | // AbslUnparseFlag() |
737 | | // |
738 | | // Unparses a Duration value into a command-line string representation using |
739 | | // the format specified by `absl::ParseDuration()`. |
740 | | std::string AbslUnparseFlag(Duration d); |
741 | | |
742 | | ABSL_DEPRECATED("Use AbslParseFlag() instead.") |
743 | | bool ParseFlag(const std::string& text, Duration* dst, std::string* error); |
744 | | ABSL_DEPRECATED("Use AbslUnparseFlag() instead.") |
745 | | std::string UnparseFlag(Duration d); |
746 | | |
747 | | // Time |
748 | | // |
749 | | // An `absl::Time` represents a specific instant in time. Arithmetic operators |
750 | | // are provided for naturally expressing time calculations. Instances are |
751 | | // created using `absl::Now()` and the `absl::From*()` factory functions that |
752 | | // accept the gamut of other time representations. Formatting and parsing |
753 | | // functions are provided for conversion to and from strings. `absl::Time` is |
754 | | // trivially destructible and should be passed by value rather than const |
755 | | // reference. |
756 | | // |
757 | | // `absl::Time` assumes there are 60 seconds in a minute, which means the |
758 | | // underlying time scales must be "smeared" to eliminate leap seconds. |
759 | | // See https://developers.google.com/time/smear. |
760 | | // |
761 | | // Even though `absl::Time` supports a wide range of timestamps, exercise |
762 | | // caution when using values in the distant past. `absl::Time` uses the |
763 | | // Proleptic Gregorian calendar, which extends the Gregorian calendar backward |
764 | | // to dates before its introduction in 1582. |
765 | | // See https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar |
766 | | // for more information. Use the ICU calendar classes to convert a date in |
767 | | // some other calendar (http://userguide.icu-project.org/datetime/calendar). |
768 | | // |
769 | | // Similarly, standardized time zones are a reasonably recent innovation, with |
770 | | // the Greenwich prime meridian being established in 1884. The TZ database |
771 | | // itself does not profess accurate offsets for timestamps prior to 1970. The |
772 | | // breakdown of future timestamps is subject to the whim of regional |
773 | | // governments. |
774 | | // |
775 | | // The `absl::Time` class represents an instant in time as a count of clock |
776 | | // ticks of some granularity (resolution) from some starting point (epoch). |
777 | | // |
778 | | // `absl::Time` uses a resolution that is high enough to avoid loss in |
779 | | // precision, and a range that is wide enough to avoid overflow, when |
780 | | // converting between tick counts in most Google time scales (i.e., resolution |
781 | | // of at least one nanosecond, and range +/-100 billion years). Conversions |
782 | | // between the time scales are performed by truncating (towards negative |
783 | | // infinity) to the nearest representable point. |
784 | | // |
785 | | // Examples: |
786 | | // |
787 | | // absl::Time t1 = ...; |
788 | | // absl::Time t2 = t1 + absl::Minutes(2); |
789 | | // absl::Duration d = t2 - t1; // == absl::Minutes(2) |
790 | | // |
791 | | class Time { |
792 | | public: |
793 | | // Value semantics. |
794 | | |
795 | | // Returns the Unix epoch. However, those reading your code may not know |
796 | | // or expect the Unix epoch as the default value, so make your code more |
797 | | // readable by explicitly initializing all instances before use. |
798 | | // |
799 | | // Example: |
800 | | // absl::Time t = absl::UnixEpoch(); |
801 | | // absl::Time t = absl::Now(); |
802 | | // absl::Time t = absl::TimeFromTimeval(tv); |
803 | | // absl::Time t = absl::InfinitePast(); |
804 | 0 | constexpr Time() = default; |
805 | | |
806 | | // Copyable. |
807 | | constexpr Time(const Time& t) = default; |
808 | | Time& operator=(const Time& t) = default; |
809 | | |
810 | | // Assignment operators. |
811 | 0 | Time& operator+=(Duration d) { |
812 | 0 | rep_ += d; |
813 | 0 | return *this; |
814 | 0 | } |
815 | 0 | Time& operator-=(Duration d) { |
816 | 0 | rep_ -= d; |
817 | 0 | return *this; |
818 | 0 | } |
819 | | |
820 | | // Time::Breakdown |
821 | | // |
822 | | // The calendar and wall-clock (aka "civil time") components of an |
823 | | // `absl::Time` in a certain `absl::TimeZone`. This struct is not |
824 | | // intended to represent an instant in time. So, rather than passing |
825 | | // a `Time::Breakdown` to a function, pass an `absl::Time` and an |
826 | | // `absl::TimeZone`. |
827 | | // |
828 | | // Deprecated. Use `absl::TimeZone::CivilInfo`. |
829 | | struct ABSL_DEPRECATED("Use `absl::TimeZone::CivilInfo`.") Breakdown { |
830 | | int64_t year; // year (e.g., 2013) |
831 | | int month; // month of year [1:12] |
832 | | int day; // day of month [1:31] |
833 | | int hour; // hour of day [0:23] |
834 | | int minute; // minute of hour [0:59] |
835 | | int second; // second of minute [0:59] |
836 | | Duration subsecond; // [Seconds(0):Seconds(1)) if finite |
837 | | int weekday; // 1==Mon, ..., 7=Sun |
838 | | int yearday; // day of year [1:366] |
839 | | |
840 | | // Note: The following fields exist for backward compatibility |
841 | | // with older APIs. Accessing these fields directly is a sign of |
842 | | // imprudent logic in the calling code. Modern time-related code |
843 | | // should only access this data indirectly by way of FormatTime(). |
844 | | // These fields are undefined for InfiniteFuture() and InfinitePast(). |
845 | | int offset; // seconds east of UTC |
846 | | bool is_dst; // is offset non-standard? |
847 | | const char* zone_abbr; // time-zone abbreviation (e.g., "PST") |
848 | | }; |
849 | | |
850 | | // Time::In() |
851 | | // |
852 | | // Returns the breakdown of this instant in the given TimeZone. |
853 | | // |
854 | | // Deprecated. Use `absl::TimeZone::At(Time)`. |
855 | | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
856 | | ABSL_DEPRECATED("Use `absl::TimeZone::At(Time)`.") |
857 | | Breakdown In(TimeZone tz) const; |
858 | | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
859 | | |
860 | | template <typename H> |
861 | | friend H AbslHashValue(H h, Time t) { |
862 | | return H::combine(std::move(h), t.rep_); |
863 | | } |
864 | | |
865 | | private: |
866 | | friend constexpr Time time_internal::FromUnixDuration(Duration d); |
867 | | friend constexpr Duration time_internal::ToUnixDuration(Time t); |
868 | | |
869 | | #ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON |
870 | | friend constexpr std::strong_ordering operator<=>(Time lhs, Time rhs); |
871 | | #endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON |
872 | | |
873 | | friend constexpr bool operator<(Time lhs, Time rhs); |
874 | | friend constexpr bool operator==(Time lhs, Time rhs); |
875 | | friend Duration operator-(Time lhs, Time rhs); |
876 | | friend constexpr Time UniversalEpoch(); |
877 | | friend constexpr Time InfiniteFuture(); |
878 | | friend constexpr Time InfinitePast(); |
879 | 0 | constexpr explicit Time(Duration rep) : rep_(rep) {} |
880 | | Duration rep_; |
881 | | }; |
882 | | |
883 | | // Relational Operators |
884 | | #ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON |
885 | | |
886 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>( |
887 | | Time lhs, Time rhs) { |
888 | | return lhs.rep_ <=> rhs.rep_; |
889 | | } |
890 | | |
891 | | #endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON |
892 | | |
893 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Time lhs, Time rhs) { |
894 | 0 | return lhs.rep_ < rhs.rep_; |
895 | 0 | } |
896 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>(Time lhs, Time rhs) { |
897 | 0 | return rhs < lhs; |
898 | 0 | } |
899 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>=(Time lhs, Time rhs) { |
900 | 0 | return !(lhs < rhs); |
901 | 0 | } |
902 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<=(Time lhs, Time rhs) { |
903 | 0 | return !(rhs < lhs); |
904 | 0 | } |
905 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Time lhs, Time rhs) { |
906 | 0 | return lhs.rep_ == rhs.rep_; |
907 | 0 | } |
908 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator!=(Time lhs, Time rhs) { |
909 | 0 | return !(lhs == rhs); |
910 | 0 | } |
911 | | |
912 | | // Additive Operators |
913 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator+(Time lhs, Duration rhs) { |
914 | 0 | return lhs += rhs; |
915 | 0 | } |
916 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator+(Duration lhs, Time rhs) { |
917 | 0 | return rhs += lhs; |
918 | 0 | } |
919 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator-(Time lhs, Duration rhs) { |
920 | 0 | return lhs -= rhs; |
921 | 0 | } |
922 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator-(Time lhs, Time rhs) { |
923 | 0 | return lhs.rep_ - rhs.rep_; |
924 | 0 | } |
925 | | |
926 | | // UnixEpoch() |
927 | | // |
928 | | // Returns the `absl::Time` representing "1970-01-01 00:00:00.0 +0000". |
929 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time UnixEpoch() { return Time(); } |
930 | | |
931 | | // UniversalEpoch() |
932 | | // |
933 | | // Returns the `absl::Time` representing "0001-01-01 00:00:00.0 +0000", the |
934 | | // epoch of the ICU Universal Time Scale. |
935 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time UniversalEpoch() { |
936 | | // 719162 is the number of days from 0001-01-01 to 1970-01-01, |
937 | | // assuming the Gregorian calendar. |
938 | 0 | return Time( |
939 | 0 | time_internal::MakeDuration(-24 * 719162 * int64_t{3600}, uint32_t{0})); |
940 | 0 | } |
941 | | |
942 | | // InfiniteFuture() |
943 | | // |
944 | | // Returns an `absl::Time` that is infinitely far in the future. |
945 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time InfiniteFuture() { |
946 | 0 | return Time(time_internal::MakeDuration((std::numeric_limits<int64_t>::max)(), |
947 | 0 | ~uint32_t{0})); |
948 | 0 | } |
949 | | |
950 | | // InfinitePast() |
951 | | // |
952 | | // Returns an `absl::Time` that is infinitely far in the past. |
953 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time InfinitePast() { |
954 | 0 | return Time(time_internal::MakeDuration((std::numeric_limits<int64_t>::min)(), |
955 | 0 | ~uint32_t{0})); |
956 | 0 | } |
957 | | |
958 | | // FromUnixNanos() |
959 | | // FromUnixMicros() |
960 | | // FromUnixMillis() |
961 | | // FromUnixSeconds() |
962 | | // FromTimeT() |
963 | | // FromUDate() |
964 | | // FromUniversal() |
965 | | // |
966 | | // Creates an `absl::Time` from a variety of other representations. See |
967 | | // https://unicode-org.github.io/icu/userguide/datetime/universaltimescale.html |
968 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixNanos(int64_t ns); |
969 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMicros(int64_t us); |
970 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMillis(int64_t ms); |
971 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixSeconds(int64_t s); |
972 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromTimeT(time_t t); |
973 | | ABSL_ATTRIBUTE_CONST_FUNCTION Time FromUDate(double udate); |
974 | | ABSL_ATTRIBUTE_CONST_FUNCTION Time FromUniversal(int64_t universal); |
975 | | |
976 | | // ToUnixNanos() |
977 | | // ToUnixMicros() |
978 | | // ToUnixMillis() |
979 | | // ToUnixSeconds() |
980 | | // ToTimeT() |
981 | | // ToUDate() |
982 | | // ToUniversal() |
983 | | // |
984 | | // Converts an `absl::Time` to a variety of other representations. See |
985 | | // https://unicode-org.github.io/icu/userguide/datetime/universaltimescale.html |
986 | | // |
987 | | // Note that these operations round down toward negative infinity where |
988 | | // necessary to adjust to the resolution of the result type. Beware of |
989 | | // possible time_t over/underflow in ToTime{T,val,spec}() on 32-bit platforms. |
990 | | ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixNanos(Time t); |
991 | | ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixMicros(Time t); |
992 | | ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixMillis(Time t); |
993 | | ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixSeconds(Time t); |
994 | | ABSL_ATTRIBUTE_CONST_FUNCTION time_t ToTimeT(Time t); |
995 | | ABSL_ATTRIBUTE_CONST_FUNCTION double ToUDate(Time t); |
996 | | ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUniversal(Time t); |
997 | | |
998 | | // DurationFromTimespec() |
999 | | // DurationFromTimeval() |
1000 | | // ToTimespec() |
1001 | | // ToTimeval() |
1002 | | // TimeFromTimespec() |
1003 | | // TimeFromTimeval() |
1004 | | // ToTimespec() |
1005 | | // ToTimeval() |
1006 | | // |
1007 | | // Some APIs use a timespec or a timeval as a Duration (e.g., nanosleep(2) |
1008 | | // and select(2)), while others use them as a Time (e.g. clock_gettime(2) |
1009 | | // and gettimeofday(2)), so conversion functions are provided for both cases. |
1010 | | // The "to timespec/val" direction is easily handled via overloading, but |
1011 | | // for "from timespec/val" the desired type is part of the function name. |
1012 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration DurationFromTimespec(timespec ts); |
1013 | | ABSL_ATTRIBUTE_CONST_FUNCTION Duration DurationFromTimeval(timeval tv); |
1014 | | ABSL_ATTRIBUTE_CONST_FUNCTION timespec ToTimespec(Duration d); |
1015 | | ABSL_ATTRIBUTE_CONST_FUNCTION timeval ToTimeval(Duration d); |
1016 | | ABSL_ATTRIBUTE_CONST_FUNCTION Time TimeFromTimespec(timespec ts); |
1017 | | ABSL_ATTRIBUTE_CONST_FUNCTION Time TimeFromTimeval(timeval tv); |
1018 | | ABSL_ATTRIBUTE_CONST_FUNCTION timespec ToTimespec(Time t); |
1019 | | ABSL_ATTRIBUTE_CONST_FUNCTION timeval ToTimeval(Time t); |
1020 | | |
1021 | | // FromChrono() |
1022 | | // |
1023 | | // Converts a std::chrono::system_clock::time_point to an absl::Time. |
1024 | | // |
1025 | | // Example: |
1026 | | // |
1027 | | // auto tp = std::chrono::system_clock::from_time_t(123); |
1028 | | // absl::Time t = absl::FromChrono(tp); |
1029 | | // // t == absl::FromTimeT(123) |
1030 | | ABSL_ATTRIBUTE_PURE_FUNCTION Time |
1031 | | FromChrono(const std::chrono::system_clock::time_point& tp); |
1032 | | |
1033 | | // ToChronoTime() |
1034 | | // |
1035 | | // Converts an absl::Time to a std::chrono::system_clock::time_point. If |
1036 | | // overflow would occur, the returned value will saturate at the min/max time |
1037 | | // point value instead. |
1038 | | // |
1039 | | // Example: |
1040 | | // |
1041 | | // absl::Time t = absl::FromTimeT(123); |
1042 | | // auto tp = absl::ToChronoTime(t); |
1043 | | // // tp == std::chrono::system_clock::from_time_t(123); |
1044 | | ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::system_clock::time_point |
1045 | | ToChronoTime(Time); |
1046 | | |
1047 | | // AbslParseFlag() |
1048 | | // |
1049 | | // Parses the command-line flag string representation `text` into a Time value. |
1050 | | // Time flags must be specified in a format that matches absl::RFC3339_full. |
1051 | | // |
1052 | | // For example: |
1053 | | // |
1054 | | // --start_time=2016-01-02T03:04:05.678+08:00 |
1055 | | // |
1056 | | // Note: A UTC offset (or 'Z' indicating a zero-offset from UTC) is required. |
1057 | | // |
1058 | | // Additionally, if you'd like to specify a time as a count of |
1059 | | // seconds/milliseconds/etc from the Unix epoch, use an absl::Duration flag |
1060 | | // and add that duration to absl::UnixEpoch() to get an absl::Time. |
1061 | | bool AbslParseFlag(absl::string_view text, Time* t, std::string* error); |
1062 | | |
1063 | | // AbslUnparseFlag() |
1064 | | // |
1065 | | // Unparses a Time value into a command-line string representation using |
1066 | | // the format specified by `absl::ParseTime()`. |
1067 | | std::string AbslUnparseFlag(Time t); |
1068 | | |
1069 | | ABSL_DEPRECATED("Use AbslParseFlag() instead.") |
1070 | | bool ParseFlag(const std::string& text, Time* t, std::string* error); |
1071 | | ABSL_DEPRECATED("Use AbslUnparseFlag() instead.") |
1072 | | std::string UnparseFlag(Time t); |
1073 | | |
1074 | | // TimeZone |
1075 | | // |
1076 | | // The `absl::TimeZone` is an opaque, small, value-type class representing a |
1077 | | // geo-political region within which particular rules are used for converting |
1078 | | // between absolute and civil times (see https://git.io/v59Ly). `absl::TimeZone` |
1079 | | // values are named using the TZ identifiers from the IANA Time Zone Database, |
1080 | | // such as "America/Los_Angeles" or "Australia/Sydney". `absl::TimeZone` values |
1081 | | // are created from factory functions such as `absl::LoadTimeZone()`. Note: |
1082 | | // strings like "PST" and "EDT" are not valid TZ identifiers. Prefer to pass by |
1083 | | // value rather than const reference. |
1084 | | // |
1085 | | // For more on the fundamental concepts of time zones, absolute times, and civil |
1086 | | // times, see https://github.com/google/cctz#fundamental-concepts |
1087 | | // |
1088 | | // Examples: |
1089 | | // |
1090 | | // absl::TimeZone utc = absl::UTCTimeZone(); |
1091 | | // absl::TimeZone pst = absl::FixedTimeZone(-8 * 60 * 60); |
1092 | | // absl::TimeZone loc = absl::LocalTimeZone(); |
1093 | | // absl::TimeZone lax; |
1094 | | // if (!absl::LoadTimeZone("America/Los_Angeles", &lax)) { |
1095 | | // // handle error case |
1096 | | // } |
1097 | | // |
1098 | | // See also: |
1099 | | // - https://github.com/google/cctz |
1100 | | // - https://www.iana.org/time-zones |
1101 | | // - https://en.wikipedia.org/wiki/Zoneinfo |
1102 | | class TimeZone { |
1103 | | public: |
1104 | 0 | explicit TimeZone(time_internal::cctz::time_zone tz) : cz_(tz) {} |
1105 | | TimeZone() = default; // UTC, but prefer UTCTimeZone() to be explicit. |
1106 | | |
1107 | | // Copyable. |
1108 | | TimeZone(const TimeZone&) = default; |
1109 | | TimeZone& operator=(const TimeZone&) = default; |
1110 | | |
1111 | 0 | explicit operator time_internal::cctz::time_zone() const { return cz_; } |
1112 | | |
1113 | 0 | std::string name() const { return cz_.name(); } |
1114 | | |
1115 | | // TimeZone::CivilInfo |
1116 | | // |
1117 | | // Information about the civil time corresponding to an absolute time. |
1118 | | // This struct is not intended to represent an instant in time. So, rather |
1119 | | // than passing a `TimeZone::CivilInfo` to a function, pass an `absl::Time` |
1120 | | // and an `absl::TimeZone`. |
1121 | | struct CivilInfo { |
1122 | | CivilSecond cs; |
1123 | | Duration subsecond; |
1124 | | |
1125 | | // Note: The following fields exist for backward compatibility |
1126 | | // with older APIs. Accessing these fields directly is a sign of |
1127 | | // imprudent logic in the calling code. Modern time-related code |
1128 | | // should only access this data indirectly by way of FormatTime(). |
1129 | | // These fields are undefined for InfiniteFuture() and InfinitePast(). |
1130 | | int offset; // seconds east of UTC |
1131 | | bool is_dst; // is offset non-standard? |
1132 | | const char* zone_abbr; // time-zone abbreviation (e.g., "PST") |
1133 | | }; |
1134 | | |
1135 | | // TimeZone::At(Time) |
1136 | | // |
1137 | | // Returns the civil time for this TimeZone at a certain `absl::Time`. |
1138 | | // If the input time is infinite, the output civil second will be set to |
1139 | | // CivilSecond::max() or min(), and the subsecond will be infinite. |
1140 | | // |
1141 | | // Example: |
1142 | | // |
1143 | | // const auto epoch = lax.At(absl::UnixEpoch()); |
1144 | | // // epoch.cs == 1969-12-31 16:00:00 |
1145 | | // // epoch.subsecond == absl::ZeroDuration() |
1146 | | // // epoch.offset == -28800 |
1147 | | // // epoch.is_dst == false |
1148 | | // // epoch.abbr == "PST" |
1149 | | CivilInfo At(Time t) const; |
1150 | | |
1151 | | // TimeZone::TimeInfo |
1152 | | // |
1153 | | // Information about the absolute times corresponding to a civil time. |
1154 | | // (Subseconds must be handled separately.) |
1155 | | // |
1156 | | // It is possible for a caller to pass a civil-time value that does |
1157 | | // not represent an actual or unique instant in time (due to a shift |
1158 | | // in UTC offset in the TimeZone, which results in a discontinuity in |
1159 | | // the civil-time components). For example, a daylight-saving-time |
1160 | | // transition skips or repeats civil times---in the United States, |
1161 | | // March 13, 2011 02:15 never occurred, while November 6, 2011 01:15 |
1162 | | // occurred twice---so requests for such times are not well-defined. |
1163 | | // To account for these possibilities, `absl::TimeZone::TimeInfo` is |
1164 | | // richer than just a single `absl::Time`. |
1165 | | struct TimeInfo { |
1166 | | enum CivilKind { |
1167 | | UNIQUE, // the civil time was singular (pre == trans == post) |
1168 | | SKIPPED, // the civil time did not exist (pre >= trans > post) |
1169 | | REPEATED, // the civil time was ambiguous (pre < trans <= post) |
1170 | | } kind; |
1171 | | Time pre; // time calculated using the pre-transition offset |
1172 | | Time trans; // when the civil-time discontinuity occurred |
1173 | | Time post; // time calculated using the post-transition offset |
1174 | | }; |
1175 | | |
1176 | | // TimeZone::At(CivilSecond) |
1177 | | // |
1178 | | // Returns an `absl::TimeInfo` containing the absolute time(s) for this |
1179 | | // TimeZone at an `absl::CivilSecond`. When the civil time is skipped or |
1180 | | // repeated, returns times calculated using the pre-transition and post- |
1181 | | // transition UTC offsets, plus the transition time itself. |
1182 | | // |
1183 | | // Examples: |
1184 | | // |
1185 | | // // A unique civil time |
1186 | | // const auto jan01 = lax.At(absl::CivilSecond(2011, 1, 1, 0, 0, 0)); |
1187 | | // // jan01.kind == TimeZone::TimeInfo::UNIQUE |
1188 | | // // jan01.pre is 2011-01-01 00:00:00 -0800 |
1189 | | // // jan01.trans is 2011-01-01 00:00:00 -0800 |
1190 | | // // jan01.post is 2011-01-01 00:00:00 -0800 |
1191 | | // |
1192 | | // // A Spring DST transition, when there is a gap in civil time |
1193 | | // const auto mar13 = lax.At(absl::CivilSecond(2011, 3, 13, 2, 15, 0)); |
1194 | | // // mar13.kind == TimeZone::TimeInfo::SKIPPED |
1195 | | // // mar13.pre is 2011-03-13 03:15:00 -0700 |
1196 | | // // mar13.trans is 2011-03-13 03:00:00 -0700 |
1197 | | // // mar13.post is 2011-03-13 01:15:00 -0800 |
1198 | | // |
1199 | | // // A Fall DST transition, when civil times are repeated |
1200 | | // const auto nov06 = lax.At(absl::CivilSecond(2011, 11, 6, 1, 15, 0)); |
1201 | | // // nov06.kind == TimeZone::TimeInfo::REPEATED |
1202 | | // // nov06.pre is 2011-11-06 01:15:00 -0700 |
1203 | | // // nov06.trans is 2011-11-06 01:00:00 -0800 |
1204 | | // // nov06.post is 2011-11-06 01:15:00 -0800 |
1205 | | TimeInfo At(CivilSecond ct) const; |
1206 | | |
1207 | | // TimeZone::NextTransition() |
1208 | | // TimeZone::PrevTransition() |
1209 | | // |
1210 | | // Finds the time of the next/previous offset change in this time zone. |
1211 | | // |
1212 | | // By definition, `NextTransition(t, &trans)` returns false when `t` is |
1213 | | // `InfiniteFuture()`, and `PrevTransition(t, &trans)` returns false |
1214 | | // when `t` is `InfinitePast()`. If the zone has no transitions, the |
1215 | | // result will also be false no matter what the argument. |
1216 | | // |
1217 | | // Otherwise, when `t` is `InfinitePast()`, `NextTransition(t, &trans)` |
1218 | | // returns true and sets `trans` to the first recorded transition. Chains |
1219 | | // of calls to `NextTransition()/PrevTransition()` will eventually return |
1220 | | // false, but it is unspecified exactly when `NextTransition(t, &trans)` |
1221 | | // jumps to false, or what time is set by `PrevTransition(t, &trans)` for |
1222 | | // a very distant `t`. |
1223 | | // |
1224 | | // Note: Enumeration of time-zone transitions is for informational purposes |
1225 | | // only. Modern time-related code should not care about when offset changes |
1226 | | // occur. |
1227 | | // |
1228 | | // Example: |
1229 | | // absl::TimeZone nyc; |
1230 | | // if (!absl::LoadTimeZone("America/New_York", &nyc)) { ... } |
1231 | | // const auto now = absl::Now(); |
1232 | | // auto t = absl::InfinitePast(); |
1233 | | // absl::TimeZone::CivilTransition trans; |
1234 | | // while (t <= now && nyc.NextTransition(t, &trans)) { |
1235 | | // // transition: trans.from -> trans.to |
1236 | | // t = nyc.At(trans.to).trans; |
1237 | | // } |
1238 | | struct CivilTransition { |
1239 | | CivilSecond from; // the civil time we jump from |
1240 | | CivilSecond to; // the civil time we jump to |
1241 | | }; |
1242 | | bool NextTransition(Time t, CivilTransition* trans) const; |
1243 | | bool PrevTransition(Time t, CivilTransition* trans) const; |
1244 | | |
1245 | | template <typename H> |
1246 | | friend H AbslHashValue(H h, TimeZone tz) { |
1247 | | return H::combine(std::move(h), tz.cz_); |
1248 | | } |
1249 | | |
1250 | | private: |
1251 | 0 | friend bool operator==(TimeZone a, TimeZone b) { return a.cz_ == b.cz_; } |
1252 | 0 | friend bool operator!=(TimeZone a, TimeZone b) { return a.cz_ != b.cz_; } |
1253 | 0 | friend std::ostream& operator<<(std::ostream& os, TimeZone tz) { |
1254 | 0 | return os << tz.name(); |
1255 | 0 | } |
1256 | | |
1257 | | time_internal::cctz::time_zone cz_; |
1258 | | }; |
1259 | | |
1260 | | // LoadTimeZone() |
1261 | | // |
1262 | | // Loads the named zone. May perform I/O on the initial load of the named |
1263 | | // zone. If the name is invalid, or some other kind of error occurs, returns |
1264 | | // `false` and `*tz` is set to the UTC time zone. |
1265 | 0 | inline bool LoadTimeZone(absl::string_view name, TimeZone* tz) { |
1266 | 0 | if (name == "localtime") { |
1267 | 0 | *tz = TimeZone(time_internal::cctz::local_time_zone()); |
1268 | 0 | return true; |
1269 | 0 | } |
1270 | 0 | time_internal::cctz::time_zone cz; |
1271 | 0 | const bool b = time_internal::cctz::load_time_zone(std::string(name), &cz); |
1272 | 0 | *tz = TimeZone(cz); |
1273 | 0 | return b; |
1274 | 0 | } |
1275 | | |
1276 | | // FixedTimeZone() |
1277 | | // |
1278 | | // Returns a TimeZone that is a fixed offset (seconds east) from UTC. |
1279 | | // Note: If the absolute value of the offset is greater than 24 hours |
1280 | | // you'll get UTC (i.e., no offset) instead. |
1281 | 0 | inline TimeZone FixedTimeZone(int seconds) { |
1282 | 0 | return TimeZone( |
1283 | 0 | time_internal::cctz::fixed_time_zone(std::chrono::seconds(seconds))); |
1284 | 0 | } |
1285 | | |
1286 | | // UTCTimeZone() |
1287 | | // |
1288 | | // Convenience method returning the UTC time zone. |
1289 | 0 | inline TimeZone UTCTimeZone() { |
1290 | 0 | return TimeZone(time_internal::cctz::utc_time_zone()); |
1291 | 0 | } |
1292 | | |
1293 | | // LocalTimeZone() |
1294 | | // |
1295 | | // Convenience method returning the local time zone, or UTC if there is |
1296 | | // no configured local zone. Warning: Be wary of using LocalTimeZone(), |
1297 | | // and particularly so in a server process, as the zone configured for the |
1298 | | // local machine should be irrelevant. Prefer an explicit zone name. |
1299 | 0 | inline TimeZone LocalTimeZone() { |
1300 | 0 | return TimeZone(time_internal::cctz::local_time_zone()); |
1301 | 0 | } |
1302 | | |
1303 | | // ToCivilSecond() |
1304 | | // ToCivilMinute() |
1305 | | // ToCivilHour() |
1306 | | // ToCivilDay() |
1307 | | // ToCivilMonth() |
1308 | | // ToCivilYear() |
1309 | | // |
1310 | | // Helpers for TimeZone::At(Time) to return particularly aligned civil times. |
1311 | | // |
1312 | | // Example: |
1313 | | // |
1314 | | // absl::Time t = ...; |
1315 | | // absl::TimeZone tz = ...; |
1316 | | // const auto cd = absl::ToCivilDay(t, tz); |
1317 | | ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilSecond ToCivilSecond(Time t, |
1318 | 0 | TimeZone tz) { |
1319 | 0 | return tz.At(t).cs; // already a CivilSecond |
1320 | 0 | } |
1321 | | ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilMinute ToCivilMinute(Time t, |
1322 | 0 | TimeZone tz) { |
1323 | 0 | return CivilMinute(tz.At(t).cs); |
1324 | 0 | } |
1325 | 0 | ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilHour ToCivilHour(Time t, TimeZone tz) { |
1326 | 0 | return CivilHour(tz.At(t).cs); |
1327 | 0 | } |
1328 | 0 | ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilDay ToCivilDay(Time t, TimeZone tz) { |
1329 | 0 | return CivilDay(tz.At(t).cs); |
1330 | 0 | } |
1331 | | ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilMonth ToCivilMonth(Time t, |
1332 | 0 | TimeZone tz) { |
1333 | 0 | return CivilMonth(tz.At(t).cs); |
1334 | 0 | } |
1335 | 0 | ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilYear ToCivilYear(Time t, TimeZone tz) { |
1336 | 0 | return CivilYear(tz.At(t).cs); |
1337 | 0 | } |
1338 | | |
1339 | | // FromCivil() |
1340 | | // |
1341 | | // Helper for TimeZone::At(CivilSecond) that provides "order-preserving |
1342 | | // semantics." If the civil time maps to a unique time, that time is |
1343 | | // returned. If the civil time is repeated in the given time zone, the |
1344 | | // time using the pre-transition offset is returned. Otherwise, the |
1345 | | // civil time is skipped in the given time zone, and the transition time |
1346 | | // is returned. This means that for any two civil times, ct1 and ct2, |
1347 | | // (ct1 < ct2) => (FromCivil(ct1) <= FromCivil(ct2)), the equal case |
1348 | | // being when two non-existent civil times map to the same transition time. |
1349 | | // |
1350 | | // Note: Accepts civil times of any alignment. |
1351 | | ABSL_ATTRIBUTE_PURE_FUNCTION inline Time FromCivil(CivilSecond ct, |
1352 | 0 | TimeZone tz) { |
1353 | 0 | const auto ti = tz.At(ct); |
1354 | 0 | if (ti.kind == TimeZone::TimeInfo::SKIPPED) return ti.trans; |
1355 | 0 | return ti.pre; |
1356 | 0 | } |
1357 | | |
1358 | | // TimeConversion |
1359 | | // |
1360 | | // An `absl::TimeConversion` represents the conversion of year, month, day, |
1361 | | // hour, minute, and second values (i.e., a civil time), in a particular |
1362 | | // `absl::TimeZone`, to a time instant (an absolute time), as returned by |
1363 | | // `absl::ConvertDateTime()`. Legacy version of `absl::TimeZone::TimeInfo`. |
1364 | | // |
1365 | | // Deprecated. Use `absl::TimeZone::TimeInfo`. |
1366 | | struct ABSL_DEPRECATED("Use `absl::TimeZone::TimeInfo`.") TimeConversion { |
1367 | | Time pre; // time calculated using the pre-transition offset |
1368 | | Time trans; // when the civil-time discontinuity occurred |
1369 | | Time post; // time calculated using the post-transition offset |
1370 | | |
1371 | | enum Kind { |
1372 | | UNIQUE, // the civil time was singular (pre == trans == post) |
1373 | | SKIPPED, // the civil time did not exist |
1374 | | REPEATED, // the civil time was ambiguous |
1375 | | }; |
1376 | | Kind kind; |
1377 | | |
1378 | | bool normalized; // input values were outside their valid ranges |
1379 | | }; |
1380 | | |
1381 | | // ConvertDateTime() |
1382 | | // |
1383 | | // Legacy version of `absl::TimeZone::At(absl::CivilSecond)` that takes |
1384 | | // the civil time as six, separate values (YMDHMS). |
1385 | | // |
1386 | | // The input month, day, hour, minute, and second values can be outside |
1387 | | // of their valid ranges, in which case they will be "normalized" during |
1388 | | // the conversion. |
1389 | | // |
1390 | | // Example: |
1391 | | // |
1392 | | // // "October 32" normalizes to "November 1". |
1393 | | // absl::TimeConversion tc = |
1394 | | // absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, lax); |
1395 | | // // tc.kind == TimeConversion::UNIQUE && tc.normalized == true |
1396 | | // // absl::ToCivilDay(tc.pre, tz).month() == 11 |
1397 | | // // absl::ToCivilDay(tc.pre, tz).day() == 1 |
1398 | | // |
1399 | | // Deprecated. Use `absl::TimeZone::At(CivilSecond)`. |
1400 | | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
1401 | | ABSL_DEPRECATED("Use `absl::TimeZone::At(CivilSecond)`.") |
1402 | | TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, |
1403 | | int min, int sec, TimeZone tz); |
1404 | | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
1405 | | |
1406 | | // FromDateTime() |
1407 | | // |
1408 | | // A convenience wrapper for `absl::ConvertDateTime()` that simply returns |
1409 | | // the "pre" `absl::Time`. That is, the unique result, or the instant that |
1410 | | // is correct using the pre-transition offset (as if the transition never |
1411 | | // happened). |
1412 | | // |
1413 | | // Example: |
1414 | | // |
1415 | | // absl::Time t = absl::FromDateTime(2017, 9, 26, 9, 30, 0, lax); |
1416 | | // // t = 2017-09-26 09:30:00 -0700 |
1417 | | // |
1418 | | // Deprecated. Use `absl::FromCivil(CivilSecond, TimeZone)`. Note that the |
1419 | | // behavior of `FromCivil()` differs from `FromDateTime()` for skipped civil |
1420 | | // times. If you care about that see `absl::TimeZone::At(absl::CivilSecond)`. |
1421 | | ABSL_DEPRECATED("Use `absl::FromCivil(CivilSecond, TimeZone)`.") |
1422 | | inline Time FromDateTime(int64_t year, int mon, int day, int hour, int min, |
1423 | 0 | int sec, TimeZone tz) { |
1424 | 0 | ABSL_INTERNAL_DISABLE_DEPRECATED_DECLARATION_WARNING |
1425 | 0 | return ConvertDateTime(year, mon, day, hour, min, sec, tz).pre; |
1426 | 0 | ABSL_INTERNAL_RESTORE_DEPRECATED_DECLARATION_WARNING |
1427 | 0 | } |
1428 | | |
1429 | | // FromTM() |
1430 | | // |
1431 | | // Converts the `tm_year`, `tm_mon`, `tm_mday`, `tm_hour`, `tm_min`, and |
1432 | | // `tm_sec` fields to an `absl::Time` using the given time zone. See ctime(3) |
1433 | | // for a description of the expected values of the tm fields. If the civil time |
1434 | | // is unique (see `absl::TimeZone::At(absl::CivilSecond)` above), the matching |
1435 | | // time instant is returned. Otherwise, the `tm_isdst` field is consulted to |
1436 | | // choose between the possible results. For a repeated civil time, `tm_isdst != |
1437 | | // 0` returns the matching DST instant, while `tm_isdst == 0` returns the |
1438 | | // matching non-DST instant. For a skipped civil time there is no matching |
1439 | | // instant, so `tm_isdst != 0` returns the DST instant, and `tm_isdst == 0` |
1440 | | // returns the non-DST instant, that would have matched if the transition never |
1441 | | // happened. |
1442 | | ABSL_ATTRIBUTE_PURE_FUNCTION Time FromTM(const struct tm& tm, TimeZone tz); |
1443 | | |
1444 | | // ToTM() |
1445 | | // |
1446 | | // Converts the given `absl::Time` to a struct tm using the given time zone. |
1447 | | // See ctime(3) for a description of the values of the tm fields. |
1448 | | ABSL_ATTRIBUTE_PURE_FUNCTION struct tm ToTM(Time t, TimeZone tz); |
1449 | | |
1450 | | // RFC3339_full |
1451 | | // RFC3339_sec |
1452 | | // |
1453 | | // FormatTime()/ParseTime() format specifiers for RFC3339 date/time strings, |
1454 | | // with trailing zeros trimmed or with fractional seconds omitted altogether. |
1455 | | // |
1456 | | // Note that RFC3339_sec[] matches an ISO 8601 extended format for date and |
1457 | | // time with UTC offset. Also note the use of "%Y": RFC3339 mandates that |
1458 | | // years have exactly four digits, but we allow them to take their natural |
1459 | | // width. |
1460 | | ABSL_DLL extern const char RFC3339_full[]; // %Y-%m-%d%ET%H:%M:%E*S%Ez |
1461 | | ABSL_DLL extern const char RFC3339_sec[]; // %Y-%m-%d%ET%H:%M:%S%Ez |
1462 | | |
1463 | | // RFC1123_full |
1464 | | // RFC1123_no_wday |
1465 | | // |
1466 | | // FormatTime()/ParseTime() format specifiers for RFC1123 date/time strings. |
1467 | | ABSL_DLL extern const char RFC1123_full[]; // %a, %d %b %E4Y %H:%M:%S %z |
1468 | | ABSL_DLL extern const char RFC1123_no_wday[]; // %d %b %E4Y %H:%M:%S %z |
1469 | | |
1470 | | // FormatTime() |
1471 | | // |
1472 | | // Formats the given `absl::Time` in the `absl::TimeZone` according to the |
1473 | | // provided format string. Uses strftime()-like formatting options, with |
1474 | | // the following extensions: |
1475 | | // |
1476 | | // - %Ez - RFC3339-compatible numeric UTC offset (+hh:mm or -hh:mm) |
1477 | | // - %E*z - Full-resolution numeric UTC offset (+hh:mm:ss or -hh:mm:ss) |
1478 | | // - %E#S - Seconds with # digits of fractional precision |
1479 | | // - %E*S - Seconds with full fractional precision (a literal '*') |
1480 | | // - %E#f - Fractional seconds with # digits of precision |
1481 | | // - %E*f - Fractional seconds with full precision (a literal '*') |
1482 | | // - %E4Y - Four-character years (-999 ... -001, 0000, 0001 ... 9999) |
1483 | | // - %ET - The RFC3339 "date-time" separator "T" |
1484 | | // |
1485 | | // Note that %E0S behaves like %S, and %E0f produces no characters. In |
1486 | | // contrast %E*f always produces at least one digit, which may be '0'. |
1487 | | // |
1488 | | // Note that %Y produces as many characters as it takes to fully render the |
1489 | | // year. A year outside of [-999:9999] when formatted with %E4Y will produce |
1490 | | // more than four characters, just like %Y. |
1491 | | // |
1492 | | // We recommend that format strings include the UTC offset (%z, %Ez, or %E*z) |
1493 | | // so that the result uniquely identifies a time instant. |
1494 | | // |
1495 | | // Example: |
1496 | | // |
1497 | | // absl::CivilSecond cs(2013, 1, 2, 3, 4, 5); |
1498 | | // absl::Time t = absl::FromCivil(cs, lax); |
1499 | | // std::string f = absl::FormatTime("%H:%M:%S", t, lax); // "03:04:05" |
1500 | | // f = absl::FormatTime("%H:%M:%E3S", t, lax); // "03:04:05.000" |
1501 | | // |
1502 | | // Note: If the given `absl::Time` is `absl::InfiniteFuture()`, the returned |
1503 | | // string will be exactly "infinite-future". If the given `absl::Time` is |
1504 | | // `absl::InfinitePast()`, the returned string will be exactly "infinite-past". |
1505 | | // In both cases the given format string and `absl::TimeZone` are ignored. |
1506 | | // |
1507 | | ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(absl::string_view format, |
1508 | | Time t, TimeZone tz); |
1509 | | |
1510 | | // Convenience functions that format the given time using the RFC3339_full |
1511 | | // format. The first overload uses the provided TimeZone, while the second |
1512 | | // uses LocalTimeZone(). |
1513 | | ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(Time t, TimeZone tz); |
1514 | | ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(Time t); |
1515 | | |
1516 | | // Output stream operator. |
1517 | 0 | inline std::ostream& operator<<(std::ostream& os, Time t) { |
1518 | 0 | return os << FormatTime(t); |
1519 | 0 | } |
1520 | | |
1521 | | // Support for StrFormat(), StrCat() etc. |
1522 | | template <typename Sink> |
1523 | | void AbslStringify(Sink& sink, Time t) { |
1524 | | sink.Append(FormatTime(t)); |
1525 | | } |
1526 | | |
1527 | | // ParseTime() |
1528 | | // |
1529 | | // Parses an input string according to the provided format string and |
1530 | | // returns the corresponding `absl::Time`. Uses strftime()-like formatting |
1531 | | // options, with the same extensions as FormatTime(), but with the |
1532 | | // exceptions that %E#S is interpreted as %E*S, and %E#f as %E*f. %Ez |
1533 | | // and %E*z also accept the same inputs, which (along with %z) includes |
1534 | | // 'z' and 'Z' as synonyms for +00:00. %ET accepts either 'T' or 't'. |
1535 | | // |
1536 | | // %Y consumes as many numeric characters as it can, so the matching data |
1537 | | // should always be terminated with a non-numeric. %E4Y always consumes |
1538 | | // exactly four characters, including any sign. |
1539 | | // |
1540 | | // Unspecified fields are taken from the default date and time of ... |
1541 | | // |
1542 | | // "1970-01-01 00:00:00.0 +0000" |
1543 | | // |
1544 | | // For example, parsing a string of "15:45" (%H:%M) will return an absl::Time |
1545 | | // that represents "1970-01-01 15:45:00.0 +0000". |
1546 | | // |
1547 | | // Note that since ParseTime() returns time instants, it makes the most sense |
1548 | | // to parse fully-specified date/time strings that include a UTC offset (%z, |
1549 | | // %Ez, or %E*z). |
1550 | | // |
1551 | | // Note also that `absl::ParseTime()` only heeds the fields year, month, day, |
1552 | | // hour, minute, (fractional) second, and UTC offset. Other fields, like |
1553 | | // weekday (%a or %A), while parsed for syntactic validity, are ignored |
1554 | | // in the conversion. |
1555 | | // |
1556 | | // Date and time fields that are out-of-range will be treated as errors |
1557 | | // rather than normalizing them like `absl::CivilSecond` does. For example, |
1558 | | // it is an error to parse the date "Oct 32, 2013" because 32 is out of range. |
1559 | | // |
1560 | | // A leap second of ":60" is normalized to ":00" of the following minute |
1561 | | // with fractional seconds discarded. The following table shows how the |
1562 | | // given seconds and subseconds will be parsed: |
1563 | | // |
1564 | | // "59.x" -> 59.x // exact |
1565 | | // "60.x" -> 00.0 // normalized |
1566 | | // "00.x" -> 00.x // exact |
1567 | | // |
1568 | | // Errors are indicated by returning false and assigning an error message |
1569 | | // to the "err" out param if it is non-null. |
1570 | | // |
1571 | | // Note: If the input string is exactly "infinite-future", the returned |
1572 | | // `absl::Time` will be `absl::InfiniteFuture()` and `true` will be returned. |
1573 | | // If the input string is "infinite-past", the returned `absl::Time` will be |
1574 | | // `absl::InfinitePast()` and `true` will be returned. |
1575 | | // |
1576 | | bool ParseTime(absl::string_view format, absl::string_view input, Time* time, |
1577 | | std::string* err); |
1578 | | |
1579 | | // Like ParseTime() above, but if the format string does not contain a UTC |
1580 | | // offset specification (%z/%Ez/%E*z) then the input is interpreted in the |
1581 | | // given TimeZone. This means that the input, by itself, does not identify a |
1582 | | // unique instant. Being time-zone dependent, it also admits the possibility |
1583 | | // of ambiguity or non-existence, in which case the "pre" time (as defined |
1584 | | // by TimeZone::TimeInfo) is returned. For these reasons we recommend that |
1585 | | // all date/time strings include a UTC offset so they're context independent. |
1586 | | bool ParseTime(absl::string_view format, absl::string_view input, TimeZone tz, |
1587 | | Time* time, std::string* err); |
1588 | | |
1589 | | // ============================================================================ |
1590 | | // Implementation Details Follow |
1591 | | // ============================================================================ |
1592 | | |
1593 | | namespace time_internal { |
1594 | | |
1595 | | // Creates a Duration with a given representation. |
1596 | | // REQUIRES: hi,lo is a valid representation of a Duration as specified |
1597 | | // in time/duration.cc. |
1598 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi, |
1599 | 0 | uint32_t lo = 0) { |
1600 | 0 | return Duration(hi, lo); |
1601 | 0 | } |
1602 | | |
1603 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi, |
1604 | 0 | int64_t lo) { |
1605 | 0 | return MakeDuration(hi, static_cast<uint32_t>(lo)); |
1606 | 0 | } |
1607 | | |
1608 | | // Make a Duration value from a floating-point number, as long as that number |
1609 | | // is in the range [ 0 .. numeric_limits<int64_t>::max ), that is, as long as |
1610 | | // it's positive and can be converted to int64_t without risk of UB. |
1611 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration MakePosDoubleDuration(double n) { |
1612 | 0 | const int64_t int_secs = static_cast<int64_t>(n); |
1613 | 0 | const uint32_t ticks = static_cast<uint32_t>( |
1614 | 0 | std::round((n - static_cast<double>(int_secs)) * kTicksPerSecond)); |
1615 | 0 | return ticks < kTicksPerSecond |
1616 | 0 | ? MakeDuration(int_secs, ticks) |
1617 | 0 | : MakeDuration(int_secs + 1, ticks - kTicksPerSecond); |
1618 | 0 | } |
1619 | | |
1620 | | // Creates a normalized Duration from an almost-normalized (sec,ticks) |
1621 | | // pair. sec may be positive or negative. ticks must be in the range |
1622 | | // -kTicksPerSecond < *ticks < kTicksPerSecond. If ticks is negative it |
1623 | | // will be normalized to a positive value in the resulting Duration. |
1624 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeNormalizedDuration( |
1625 | 0 | int64_t sec, int64_t ticks) { |
1626 | 0 | return (ticks < 0) ? MakeDuration(sec - 1, ticks + kTicksPerSecond) |
1627 | 0 | : MakeDuration(sec, ticks); |
1628 | 0 | } |
1629 | | |
1630 | | // Provide access to the Duration representation. |
1631 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t GetRepHi(Duration d) { |
1632 | 0 | return d.rep_hi_.Get(); |
1633 | 0 | } |
1634 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr uint32_t GetRepLo(Duration d) { |
1635 | 0 | return d.rep_lo_; |
1636 | 0 | } |
1637 | | |
1638 | | // Returns true iff d is positive or negative infinity. |
1639 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool IsInfiniteDuration(Duration d) { |
1640 | 0 | return GetRepLo(d) == ~uint32_t{0}; |
1641 | 0 | } |
1642 | | |
1643 | | // Returns an infinite Duration with the opposite sign. |
1644 | | // REQUIRES: IsInfiniteDuration(d) |
1645 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration OppositeInfinity(Duration d) { |
1646 | 0 | return GetRepHi(d) < 0 |
1647 | 0 | ? MakeDuration((std::numeric_limits<int64_t>::max)(), ~uint32_t{0}) |
1648 | 0 | : MakeDuration((std::numeric_limits<int64_t>::min)(), |
1649 | 0 | ~uint32_t{0}); |
1650 | 0 | } |
1651 | | |
1652 | | // Returns (-n)-1 (equivalently -(n+1)) without avoidable overflow. |
1653 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t NegateAndSubtractOne( |
1654 | 0 | int64_t n) { |
1655 | | // Note: Good compilers will optimize this expression to ~n when using |
1656 | | // a two's-complement representation (which is required for int64_t). |
1657 | 0 | return (n < 0) ? -(n + 1) : (-n) - 1; |
1658 | 0 | } |
1659 | | |
1660 | | // Map between a Time and a Duration since the Unix epoch. Note that these |
1661 | | // functions depend on the above mentioned choice of the Unix epoch for the |
1662 | | // Time representation (and both need to be Time friends). Without this |
1663 | | // knowledge, we would need to add-in/subtract-out UnixEpoch() respectively. |
1664 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixDuration(Duration d) { |
1665 | 0 | return Time(d); |
1666 | 0 | } |
1667 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ToUnixDuration(Time t) { |
1668 | 0 | return t.rep_; |
1669 | 0 | } |
1670 | | |
1671 | | template <std::intmax_t N> |
1672 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, |
1673 | 0 | std::ratio<1, N>) { |
1674 | 0 | static_assert(0 < N && N <= 1000 * 1000 * 1000, "Unsupported ratio"); |
1675 | | // Subsecond ratios cannot overflow. |
1676 | 0 | return MakeNormalizedDuration( |
1677 | 0 | v / N, v % N * kTicksPerNanosecond * 1000 * 1000 * 1000 / N); |
1678 | 0 | } Unexecuted instantiation: absl::Duration absl::time_internal::FromInt64<1000000000l>(long, std::__1::ratio<1l, 1000000000l>) Unexecuted instantiation: absl::Duration absl::time_internal::FromInt64<1000000l>(long, std::__1::ratio<1l, 1000000l>) Unexecuted instantiation: absl::Duration absl::time_internal::FromInt64<1000l>(long, std::__1::ratio<1l, 1000l>) Unexecuted instantiation: absl::Duration absl::time_internal::FromInt64<1l>(long, std::__1::ratio<1l, 1l>) |
1679 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, |
1680 | 0 | std::ratio<60>) { |
1681 | 0 | return (v <= (std::numeric_limits<int64_t>::max)() / 60 && |
1682 | 0 | v >= (std::numeric_limits<int64_t>::min)() / 60) |
1683 | 0 | ? MakeDuration(v * 60) |
1684 | 0 | : v > 0 ? InfiniteDuration() : -InfiniteDuration(); |
1685 | 0 | } |
1686 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, |
1687 | 0 | std::ratio<3600>) { |
1688 | 0 | return (v <= (std::numeric_limits<int64_t>::max)() / 3600 && |
1689 | 0 | v >= (std::numeric_limits<int64_t>::min)() / 3600) |
1690 | 0 | ? MakeDuration(v * 3600) |
1691 | 0 | : v > 0 ? InfiniteDuration() : -InfiniteDuration(); |
1692 | 0 | } |
1693 | | |
1694 | | // IsValidRep64<T>(0) is true if the expression `int64_t{std::declval<T>()}` is |
1695 | | // valid. That is, if a T can be assigned to an int64_t without narrowing. |
1696 | | template <typename T> |
1697 | 0 | constexpr auto IsValidRep64(int) -> decltype(int64_t{std::declval<T>()} == 0) { |
1698 | 0 | return true; |
1699 | 0 | } Unexecuted instantiation: _ZN4absl13time_internal12IsValidRep64IxEEDTeqtllclsr3stdE7declvalIT_EEELi0EEi Unexecuted instantiation: _ZN4absl13time_internal12IsValidRep64IlEEDTeqtllclsr3stdE7declvalIT_EEELi0EEi |
1700 | | template <typename T> |
1701 | | constexpr auto IsValidRep64(char) -> bool { |
1702 | | return false; |
1703 | | } |
1704 | | |
1705 | | // Converts a std::chrono::duration to an absl::Duration. |
1706 | | template <typename Rep, typename Period> |
1707 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
1708 | 0 | const std::chrono::duration<Rep, Period>& d) { |
1709 | 0 | static_assert(IsValidRep64<Rep>(0), "duration::rep is invalid"); |
1710 | 0 | return FromInt64(int64_t{d.count()}, Period{}); |
1711 | 0 | } Unexecuted instantiation: absl::Duration absl::time_internal::FromChrono<long long, std::__1::ratio<1l, 1000000000l> >(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000000l> > const&) Unexecuted instantiation: absl::Duration absl::time_internal::FromChrono<long long, std::__1::ratio<1l, 1000000l> >(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000l> > const&) Unexecuted instantiation: absl::Duration absl::time_internal::FromChrono<long long, std::__1::ratio<1l, 1000l> >(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> > const&) Unexecuted instantiation: absl::Duration absl::time_internal::FromChrono<long long, std::__1::ratio<1l, 1l> >(std::__1::chrono::duration<long long, std::__1::ratio<1l, 1l> > const&) Unexecuted instantiation: absl::Duration absl::time_internal::FromChrono<long, std::__1::ratio<60l, 1l> >(std::__1::chrono::duration<long, std::__1::ratio<60l, 1l> > const&) Unexecuted instantiation: absl::Duration absl::time_internal::FromChrono<long, std::__1::ratio<3600l, 1l> >(std::__1::chrono::duration<long, std::__1::ratio<3600l, 1l> > const&) |
1712 | | |
1713 | | template <typename Ratio> |
1714 | | ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64(Duration d, Ratio) { |
1715 | | // Note: This may be used on MSVC, which may have a system_clock period of |
1716 | | // std::ratio<1, 10 * 1000 * 1000> |
1717 | | return ToInt64Seconds(d * Ratio::den / Ratio::num); |
1718 | | } |
1719 | | // Fastpath implementations for the 6 common duration units. |
1720 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::nano) { |
1721 | 0 | return ToInt64Nanoseconds(d); |
1722 | 0 | } |
1723 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::micro) { |
1724 | 0 | return ToInt64Microseconds(d); |
1725 | 0 | } |
1726 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::milli) { |
1727 | 0 | return ToInt64Milliseconds(d); |
1728 | 0 | } |
1729 | | ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, |
1730 | 0 | std::ratio<1>) { |
1731 | 0 | return ToInt64Seconds(d); |
1732 | 0 | } |
1733 | | ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, |
1734 | 0 | std::ratio<60>) { |
1735 | 0 | return ToInt64Minutes(d); |
1736 | 0 | } |
1737 | | ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, |
1738 | 0 | std::ratio<3600>) { |
1739 | 0 | return ToInt64Hours(d); |
1740 | 0 | } |
1741 | | |
1742 | | // Converts an absl::Duration to a chrono duration of type T. |
1743 | | template <typename T> |
1744 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION T ToChronoDuration(Duration d) { |
1745 | 0 | using Rep = typename T::rep; |
1746 | 0 | using Period = typename T::period; |
1747 | 0 | static_assert(IsValidRep64<Rep>(0), "duration::rep is invalid"); |
1748 | 0 | if (time_internal::IsInfiniteDuration(d)) |
1749 | 0 | return d < ZeroDuration() ? (T::min)() : (T::max)(); |
1750 | 0 | const auto v = ToInt64(d, Period{}); |
1751 | 0 | if (v > (std::numeric_limits<Rep>::max)()) return (T::max)(); |
1752 | 0 | if (v < (std::numeric_limits<Rep>::min)()) return (T::min)(); |
1753 | 0 | return T{v}; |
1754 | 0 | } Unexecuted instantiation: std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000000l> > absl::time_internal::ToChronoDuration<std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000000l> > >(absl::Duration) Unexecuted instantiation: std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000l> > absl::time_internal::ToChronoDuration<std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000000l> > >(absl::Duration) Unexecuted instantiation: std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> > absl::time_internal::ToChronoDuration<std::__1::chrono::duration<long long, std::__1::ratio<1l, 1000l> > >(absl::Duration) Unexecuted instantiation: std::__1::chrono::duration<long long, std::__1::ratio<1l, 1l> > absl::time_internal::ToChronoDuration<std::__1::chrono::duration<long long, std::__1::ratio<1l, 1l> > >(absl::Duration) Unexecuted instantiation: std::__1::chrono::duration<long, std::__1::ratio<60l, 1l> > absl::time_internal::ToChronoDuration<std::__1::chrono::duration<long, std::__1::ratio<60l, 1l> > >(absl::Duration) Unexecuted instantiation: std::__1::chrono::duration<long, std::__1::ratio<3600l, 1l> > absl::time_internal::ToChronoDuration<std::__1::chrono::duration<long, std::__1::ratio<3600l, 1l> > >(absl::Duration) |
1755 | | |
1756 | | } // namespace time_internal |
1757 | | |
1758 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Duration lhs, |
1759 | 0 | Duration rhs) { |
1760 | 0 | return time_internal::GetRepHi(lhs) != time_internal::GetRepHi(rhs) |
1761 | 0 | ? time_internal::GetRepHi(lhs) < time_internal::GetRepHi(rhs) |
1762 | 0 | : time_internal::GetRepHi(lhs) == (std::numeric_limits<int64_t>::min)() |
1763 | 0 | ? time_internal::GetRepLo(lhs) + 1 < |
1764 | 0 | time_internal::GetRepLo(rhs) + 1 |
1765 | 0 | : time_internal::GetRepLo(lhs) < time_internal::GetRepLo(rhs); |
1766 | 0 | } |
1767 | | |
1768 | | #ifdef ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON |
1769 | | |
1770 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr std::strong_ordering operator<=>( |
1771 | | Duration lhs, Duration rhs) { |
1772 | | const int64_t lhs_hi = time_internal::GetRepHi(lhs); |
1773 | | const int64_t rhs_hi = time_internal::GetRepHi(rhs); |
1774 | | if (auto c = lhs_hi <=> rhs_hi; c != std::strong_ordering::equal) { |
1775 | | return c; |
1776 | | } |
1777 | | const uint32_t lhs_lo = time_internal::GetRepLo(lhs); |
1778 | | const uint32_t rhs_lo = time_internal::GetRepLo(rhs); |
1779 | | return (lhs_hi == (std::numeric_limits<int64_t>::min)()) |
1780 | | ? (lhs_lo + 1) <=> (rhs_lo + 1) |
1781 | | : lhs_lo <=> rhs_lo; |
1782 | | } |
1783 | | |
1784 | | #endif // ABSL_INTERNAL_TIME_HAS_THREE_WAY_COMPARISON |
1785 | | |
1786 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Duration lhs, |
1787 | 0 | Duration rhs) { |
1788 | 0 | return time_internal::GetRepHi(lhs) == time_internal::GetRepHi(rhs) && |
1789 | 0 | time_internal::GetRepLo(lhs) == time_internal::GetRepLo(rhs); |
1790 | 0 | } |
1791 | | |
1792 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration operator-(Duration d) { |
1793 | | // This is a little interesting because of the special cases. |
1794 | | // |
1795 | | // If rep_lo_ is zero, we have it easy; it's safe to negate rep_hi_, we're |
1796 | | // dealing with an integral number of seconds, and the only special case is |
1797 | | // the maximum negative finite duration, which can't be negated. |
1798 | | // |
1799 | | // Infinities stay infinite, and just change direction. |
1800 | | // |
1801 | | // Finally we're in the case where rep_lo_ is non-zero, and we can borrow |
1802 | | // a second's worth of ticks and avoid overflow (as negating int64_t-min + 1 |
1803 | | // is safe). |
1804 | 0 | return time_internal::GetRepLo(d) == 0 |
1805 | 0 | ? time_internal::GetRepHi(d) == |
1806 | 0 | (std::numeric_limits<int64_t>::min)() |
1807 | 0 | ? InfiniteDuration() |
1808 | 0 | : time_internal::MakeDuration(-time_internal::GetRepHi(d)) |
1809 | 0 | : time_internal::IsInfiniteDuration(d) |
1810 | 0 | ? time_internal::OppositeInfinity(d) |
1811 | 0 | : time_internal::MakeDuration( |
1812 | 0 | time_internal::NegateAndSubtractOne( |
1813 | 0 | time_internal::GetRepHi(d)), |
1814 | 0 | time_internal::kTicksPerSecond - |
1815 | 0 | time_internal::GetRepLo(d)); |
1816 | 0 | } |
1817 | | |
1818 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration InfiniteDuration() { |
1819 | 0 | return time_internal::MakeDuration((std::numeric_limits<int64_t>::max)(), |
1820 | 0 | ~uint32_t{0}); |
1821 | 0 | } |
1822 | | |
1823 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
1824 | 0 | const std::chrono::nanoseconds& d) { |
1825 | 0 | return time_internal::FromChrono(d); |
1826 | 0 | } |
1827 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
1828 | 0 | const std::chrono::microseconds& d) { |
1829 | 0 | return time_internal::FromChrono(d); |
1830 | 0 | } |
1831 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
1832 | 0 | const std::chrono::milliseconds& d) { |
1833 | 0 | return time_internal::FromChrono(d); |
1834 | 0 | } |
1835 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
1836 | 0 | const std::chrono::seconds& d) { |
1837 | 0 | return time_internal::FromChrono(d); |
1838 | 0 | } |
1839 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
1840 | 0 | const std::chrono::minutes& d) { |
1841 | 0 | return time_internal::FromChrono(d); |
1842 | 0 | } |
1843 | | ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( |
1844 | 0 | const std::chrono::hours& d) { |
1845 | 0 | return time_internal::FromChrono(d); |
1846 | 0 | } |
1847 | | |
1848 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixNanos(int64_t ns) { |
1849 | 0 | return time_internal::FromUnixDuration(Nanoseconds(ns)); |
1850 | 0 | } |
1851 | | |
1852 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMicros(int64_t us) { |
1853 | 0 | return time_internal::FromUnixDuration(Microseconds(us)); |
1854 | 0 | } |
1855 | | |
1856 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMillis(int64_t ms) { |
1857 | 0 | return time_internal::FromUnixDuration(Milliseconds(ms)); |
1858 | 0 | } |
1859 | | |
1860 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixSeconds(int64_t s) { |
1861 | 0 | return time_internal::FromUnixDuration(Seconds(s)); |
1862 | 0 | } |
1863 | | |
1864 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromTimeT(time_t t) { |
1865 | 0 | return time_internal::FromUnixDuration(Seconds(t)); |
1866 | 0 | } |
1867 | | |
1868 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Nanoseconds(Duration d) { |
1869 | 0 | if (time_internal::GetRepHi(d) >= 0 && |
1870 | 0 | time_internal::GetRepHi(d) >> 33 == 0) { |
1871 | 0 | return (time_internal::GetRepHi(d) * 1000 * 1000 * 1000) + |
1872 | 0 | (time_internal::GetRepLo(d) / time_internal::kTicksPerNanosecond); |
1873 | 0 | } else { |
1874 | 0 | return d / Nanoseconds(1); |
1875 | 0 | } |
1876 | 0 | } |
1877 | | |
1878 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Microseconds( |
1879 | 0 | Duration d) { |
1880 | 0 | if (time_internal::GetRepHi(d) >= 0 && |
1881 | 0 | time_internal::GetRepHi(d) >> 43 == 0) { |
1882 | 0 | return (time_internal::GetRepHi(d) * 1000 * 1000) + |
1883 | 0 | (time_internal::GetRepLo(d) / |
1884 | 0 | (time_internal::kTicksPerNanosecond * 1000)); |
1885 | 0 | } else { |
1886 | 0 | return d / Microseconds(1); |
1887 | 0 | } |
1888 | 0 | } |
1889 | | |
1890 | | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Milliseconds( |
1891 | 0 | Duration d) { |
1892 | 0 | if (time_internal::GetRepHi(d) >= 0 && |
1893 | 0 | time_internal::GetRepHi(d) >> 53 == 0) { |
1894 | 0 | return (time_internal::GetRepHi(d) * 1000) + |
1895 | 0 | (time_internal::GetRepLo(d) / |
1896 | 0 | (time_internal::kTicksPerNanosecond * 1000 * 1000)); |
1897 | 0 | } else { |
1898 | 0 | return d / Milliseconds(1); |
1899 | 0 | } |
1900 | 0 | } |
1901 | | |
1902 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Seconds(Duration d) { |
1903 | 0 | int64_t hi = time_internal::GetRepHi(d); |
1904 | 0 | if (time_internal::IsInfiniteDuration(d)) return hi; |
1905 | 0 | if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; |
1906 | 0 | return hi; |
1907 | 0 | } |
1908 | | |
1909 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Minutes(Duration d) { |
1910 | 0 | int64_t hi = time_internal::GetRepHi(d); |
1911 | 0 | if (time_internal::IsInfiniteDuration(d)) return hi; |
1912 | 0 | if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; |
1913 | 0 | return hi / 60; |
1914 | 0 | } |
1915 | | |
1916 | 0 | ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t ToInt64Hours(Duration d) { |
1917 | 0 | int64_t hi = time_internal::GetRepHi(d); |
1918 | 0 | if (time_internal::IsInfiniteDuration(d)) return hi; |
1919 | 0 | if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; |
1920 | 0 | return hi / (60 * 60); |
1921 | 0 | } |
1922 | | |
1923 | | ABSL_NAMESPACE_END |
1924 | | } // namespace absl |
1925 | | |
1926 | | #endif // ABSL_TIME_TIME_H_ |