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 // The implementation of the absl::Duration class, which is declared in
16 // //absl/time.h. This class behaves like a numeric type; it has no public
17 // methods and is used only through the operators defined here.
18 //
19 // Implementation notes:
20 //
21 // An absl::Duration is represented as
22 //
23 // rep_hi_ : (int64_t) Whole seconds
24 // rep_lo_ : (uint32_t) Fractions of a second
25 //
26 // The seconds value (rep_hi_) may be positive or negative as appropriate.
27 // The fractional seconds (rep_lo_) is always a positive offset from rep_hi_.
28 // The API for Duration guarantees at least nanosecond resolution, which
29 // means rep_lo_ could have a max value of 1B - 1 if it stored nanoseconds.
30 // However, to utilize more of the available 32 bits of space in rep_lo_,
31 // we instead store quarters of a nanosecond in rep_lo_ resulting in a max
32 // value of 4B - 1. This allows us to correctly handle calculations like
33 // 0.5 nanos + 0.5 nanos = 1 nano. The following example shows the actual
34 // Duration rep using quarters of a nanosecond.
35 //
36 // 2.5 sec = {rep_hi_=2, rep_lo_=2000000000} // lo = 4 * 500000000
37 // -2.5 sec = {rep_hi_=-3, rep_lo_=2000000000}
38 //
39 // Infinite durations are represented as Durations with the rep_lo_ field set
40 // to all 1s.
41 //
42 // +InfiniteDuration:
43 // rep_hi_ : kint64max
44 // rep_lo_ : ~0U
45 //
46 // -InfiniteDuration:
47 // rep_hi_ : kint64min
48 // rep_lo_ : ~0U
49 //
50 // Arithmetic overflows/underflows to +/- infinity and saturates.
51
52 #if defined(_MSC_VER)
53 #include <winsock2.h> // for timeval
54 #endif
55
56 #include <algorithm>
57 #include <cassert>
58 #include <cctype>
59 #include <cerrno>
60 #include <cmath>
61 #include <cstdint>
62 #include <cstdlib>
63 #include <cstring>
64 #include <ctime>
65 #include <functional>
66 #include <limits>
67 #include <string>
68
69 #include "absl/base/casts.h"
70 #include "absl/base/macros.h"
71 #include "absl/numeric/int128.h"
72 #include "absl/strings/string_view.h"
73 #include "absl/strings/strip.h"
74 #include "absl/time/time.h"
75
76 namespace absl {
77 ABSL_NAMESPACE_BEGIN
78
79 namespace {
80
81 using time_internal::kTicksPerNanosecond;
82 using time_internal::kTicksPerSecond;
83
84 constexpr int64_t kint64max = std::numeric_limits<int64_t>::max();
85 constexpr int64_t kint64min = std::numeric_limits<int64_t>::min();
86
87 // Can't use std::isinfinite() because it doesn't exist on windows.
IsFinite(double d)88 inline bool IsFinite(double d) {
89 if (std::isnan(d)) return false;
90 return d != std::numeric_limits<double>::infinity() &&
91 d != -std::numeric_limits<double>::infinity();
92 }
93
IsValidDivisor(double d)94 inline bool IsValidDivisor(double d) {
95 if (std::isnan(d)) return false;
96 return d != 0.0;
97 }
98
99 // Can't use std::round() because it is only available in C++11.
100 // Note that we ignore the possibility of floating-point over/underflow.
101 template <typename Double>
Round(Double d)102 inline double Round(Double d) {
103 return d < 0 ? std::ceil(d - 0.5) : std::floor(d + 0.5);
104 }
105
106 // *sec may be positive or negative. *ticks must be in the range
107 // -kTicksPerSecond < *ticks < kTicksPerSecond. If *ticks is negative it
108 // will be normalized to a positive value by adjusting *sec accordingly.
NormalizeTicks(int64_t * sec,int64_t * ticks)109 inline void NormalizeTicks(int64_t* sec, int64_t* ticks) {
110 if (*ticks < 0) {
111 --*sec;
112 *ticks += kTicksPerSecond;
113 }
114 }
115
116 // Makes a uint128 from the absolute value of the given scalar.
MakeU128(int64_t a)117 inline uint128 MakeU128(int64_t a) {
118 uint128 u128 = 0;
119 if (a < 0) {
120 ++u128;
121 ++a; // Makes it safe to negate 'a'
122 a = -a;
123 }
124 u128 += static_cast<uint64_t>(a);
125 return u128;
126 }
127
128 // Makes a uint128 count of ticks out of the absolute value of the Duration.
MakeU128Ticks(Duration d)129 inline uint128 MakeU128Ticks(Duration d) {
130 int64_t rep_hi = time_internal::GetRepHi(d);
131 uint32_t rep_lo = time_internal::GetRepLo(d);
132 if (rep_hi < 0) {
133 ++rep_hi;
134 rep_hi = -rep_hi;
135 rep_lo = kTicksPerSecond - rep_lo;
136 }
137 uint128 u128 = static_cast<uint64_t>(rep_hi);
138 u128 *= static_cast<uint64_t>(kTicksPerSecond);
139 u128 += rep_lo;
140 return u128;
141 }
142
143 // Breaks a uint128 of ticks into a Duration.
MakeDurationFromU128(uint128 u128,bool is_neg)144 inline Duration MakeDurationFromU128(uint128 u128, bool is_neg) {
145 int64_t rep_hi;
146 uint32_t rep_lo;
147 const uint64_t h64 = Uint128High64(u128);
148 const uint64_t l64 = Uint128Low64(u128);
149 if (h64 == 0) { // fastpath
150 const uint64_t hi = l64 / kTicksPerSecond;
151 rep_hi = static_cast<int64_t>(hi);
152 rep_lo = static_cast<uint32_t>(l64 - hi * kTicksPerSecond);
153 } else {
154 // kMaxRepHi64 is the high 64 bits of (2^63 * kTicksPerSecond).
155 // Any positive tick count whose high 64 bits are >= kMaxRepHi64
156 // is not representable as a Duration. A negative tick count can
157 // have its high 64 bits == kMaxRepHi64 but only when the low 64
158 // bits are all zero, otherwise it is not representable either.
159 const uint64_t kMaxRepHi64 = 0x77359400UL;
160 if (h64 >= kMaxRepHi64) {
161 if (is_neg && h64 == kMaxRepHi64 && l64 == 0) {
162 // Avoid trying to represent -kint64min below.
163 return time_internal::MakeDuration(kint64min);
164 }
165 return is_neg ? -InfiniteDuration() : InfiniteDuration();
166 }
167 const uint128 kTicksPerSecond128 = static_cast<uint64_t>(kTicksPerSecond);
168 const uint128 hi = u128 / kTicksPerSecond128;
169 rep_hi = static_cast<int64_t>(Uint128Low64(hi));
170 rep_lo =
171 static_cast<uint32_t>(Uint128Low64(u128 - hi * kTicksPerSecond128));
172 }
173 if (is_neg) {
174 rep_hi = -rep_hi;
175 if (rep_lo != 0) {
176 --rep_hi;
177 rep_lo = kTicksPerSecond - rep_lo;
178 }
179 }
180 return time_internal::MakeDuration(rep_hi, rep_lo);
181 }
182
183 // Convert between int64_t and uint64_t, preserving representation. This
184 // allows us to do arithmetic in the unsigned domain, where overflow has
185 // well-defined behavior. See operator+=() and operator-=().
186 //
187 // C99 7.20.1.1.1, as referenced by C++11 18.4.1.2, says, "The typedef
188 // name intN_t designates a signed integer type with width N, no padding
189 // bits, and a two's complement representation." So, we can convert to
190 // and from the corresponding uint64_t value using a bit cast.
EncodeTwosComp(int64_t v)191 inline uint64_t EncodeTwosComp(int64_t v) {
192 return absl::bit_cast<uint64_t>(v);
193 }
DecodeTwosComp(uint64_t v)194 inline int64_t DecodeTwosComp(uint64_t v) { return absl::bit_cast<int64_t>(v); }
195
196 // Note: The overflow detection in this function is done using greater/less *or
197 // equal* because kint64max/min is too large to be represented exactly in a
198 // double (which only has 53 bits of precision). In order to avoid assigning to
199 // rep->hi a double value that is too large for an int64_t (and therefore is
200 // undefined), we must consider computations that equal kint64max/min as a
201 // double as overflow cases.
SafeAddRepHi(double a_hi,double b_hi,Duration * d)202 inline bool SafeAddRepHi(double a_hi, double b_hi, Duration* d) {
203 double c = a_hi + b_hi;
204 if (c >= static_cast<double>(kint64max)) {
205 *d = InfiniteDuration();
206 return false;
207 }
208 if (c <= static_cast<double>(kint64min)) {
209 *d = -InfiniteDuration();
210 return false;
211 }
212 *d = time_internal::MakeDuration(c, time_internal::GetRepLo(*d));
213 return true;
214 }
215
216 // A functor that's similar to std::multiplies<T>, except this returns the max
217 // T value instead of overflowing. This is only defined for uint128.
218 template <typename Ignored>
219 struct SafeMultiply {
operator ()absl::__anonf5f0c9bc0111::SafeMultiply220 uint128 operator()(uint128 a, uint128 b) const {
221 // b hi is always zero because it originated as an int64_t.
222 assert(Uint128High64(b) == 0);
223 // Fastpath to avoid the expensive overflow check with division.
224 if (Uint128High64(a) == 0) {
225 return (((Uint128Low64(a) | Uint128Low64(b)) >> 32) == 0)
226 ? static_cast<uint128>(Uint128Low64(a) * Uint128Low64(b))
227 : a * b;
228 }
229 return b == 0 ? b : (a > kuint128max / b) ? kuint128max : a * b;
230 }
231 };
232
233 // Scales (i.e., multiplies or divides, depending on the Operation template)
234 // the Duration d by the int64_t r.
235 template <template <typename> class Operation>
ScaleFixed(Duration d,int64_t r)236 inline Duration ScaleFixed(Duration d, int64_t r) {
237 const uint128 a = MakeU128Ticks(d);
238 const uint128 b = MakeU128(r);
239 const uint128 q = Operation<uint128>()(a, b);
240 const bool is_neg = (time_internal::GetRepHi(d) < 0) != (r < 0);
241 return MakeDurationFromU128(q, is_neg);
242 }
243
244 // Scales (i.e., multiplies or divides, depending on the Operation template)
245 // the Duration d by the double r.
246 template <template <typename> class Operation>
ScaleDouble(Duration d,double r)247 inline Duration ScaleDouble(Duration d, double r) {
248 Operation<double> op;
249 double hi_doub = op(time_internal::GetRepHi(d), r);
250 double lo_doub = op(time_internal::GetRepLo(d), r);
251
252 double hi_int = 0;
253 double hi_frac = std::modf(hi_doub, &hi_int);
254
255 // Moves hi's fractional bits to lo.
256 lo_doub /= kTicksPerSecond;
257 lo_doub += hi_frac;
258
259 double lo_int = 0;
260 double lo_frac = std::modf(lo_doub, &lo_int);
261
262 // Rolls lo into hi if necessary.
263 int64_t lo64 = Round(lo_frac * kTicksPerSecond);
264
265 Duration ans;
266 if (!SafeAddRepHi(hi_int, lo_int, &ans)) return ans;
267 int64_t hi64 = time_internal::GetRepHi(ans);
268 if (!SafeAddRepHi(hi64, lo64 / kTicksPerSecond, &ans)) return ans;
269 hi64 = time_internal::GetRepHi(ans);
270 lo64 %= kTicksPerSecond;
271 NormalizeTicks(&hi64, &lo64);
272 return time_internal::MakeDuration(hi64, lo64);
273 }
274
275 // Tries to divide num by den as fast as possible by looking for common, easy
276 // cases. If the division was done, the quotient is in *q and the remainder is
277 // in *rem and true will be returned.
IDivFastPath(const Duration num,const Duration den,int64_t * q,Duration * rem)278 inline bool IDivFastPath(const Duration num, const Duration den, int64_t* q,
279 Duration* rem) {
280 // Bail if num or den is an infinity.
281 if (time_internal::IsInfiniteDuration(num) ||
282 time_internal::IsInfiniteDuration(den))
283 return false;
284
285 int64_t num_hi = time_internal::GetRepHi(num);
286 uint32_t num_lo = time_internal::GetRepLo(num);
287 int64_t den_hi = time_internal::GetRepHi(den);
288 uint32_t den_lo = time_internal::GetRepLo(den);
289
290 if (den_hi == 0 && den_lo == kTicksPerNanosecond) {
291 // Dividing by 1ns
292 if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000000000) {
293 *q = num_hi * 1000000000 + num_lo / kTicksPerNanosecond;
294 *rem = time_internal::MakeDuration(0, num_lo % den_lo);
295 return true;
296 }
297 } else if (den_hi == 0 && den_lo == 100 * kTicksPerNanosecond) {
298 // Dividing by 100ns (common when converting to Universal time)
299 if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 10000000) {
300 *q = num_hi * 10000000 + num_lo / (100 * kTicksPerNanosecond);
301 *rem = time_internal::MakeDuration(0, num_lo % den_lo);
302 return true;
303 }
304 } else if (den_hi == 0 && den_lo == 1000 * kTicksPerNanosecond) {
305 // Dividing by 1us
306 if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000000) {
307 *q = num_hi * 1000000 + num_lo / (1000 * kTicksPerNanosecond);
308 *rem = time_internal::MakeDuration(0, num_lo % den_lo);
309 return true;
310 }
311 } else if (den_hi == 0 && den_lo == 1000000 * kTicksPerNanosecond) {
312 // Dividing by 1ms
313 if (num_hi >= 0 && num_hi < (kint64max - kTicksPerSecond) / 1000) {
314 *q = num_hi * 1000 + num_lo / (1000000 * kTicksPerNanosecond);
315 *rem = time_internal::MakeDuration(0, num_lo % den_lo);
316 return true;
317 }
318 } else if (den_hi > 0 && den_lo == 0) {
319 // Dividing by positive multiple of 1s
320 if (num_hi >= 0) {
321 if (den_hi == 1) {
322 *q = num_hi;
323 *rem = time_internal::MakeDuration(0, num_lo);
324 return true;
325 }
326 *q = num_hi / den_hi;
327 *rem = time_internal::MakeDuration(num_hi % den_hi, num_lo);
328 return true;
329 }
330 if (num_lo != 0) {
331 num_hi += 1;
332 }
333 int64_t quotient = num_hi / den_hi;
334 int64_t rem_sec = num_hi % den_hi;
335 if (rem_sec > 0) {
336 rem_sec -= den_hi;
337 quotient += 1;
338 }
339 if (num_lo != 0) {
340 rem_sec -= 1;
341 }
342 *q = quotient;
343 *rem = time_internal::MakeDuration(rem_sec, num_lo);
344 return true;
345 }
346
347 return false;
348 }
349
350 } // namespace
351
352 namespace time_internal {
353
354 // The 'satq' argument indicates whether the quotient should saturate at the
355 // bounds of int64_t. If it does saturate, the difference will spill over to
356 // the remainder. If it does not saturate, the remainder remain accurate,
357 // but the returned quotient will over/underflow int64_t and should not be used.
IDivDuration(bool satq,const Duration num,const Duration den,Duration * rem)358 int64_t IDivDuration(bool satq, const Duration num, const Duration den,
359 Duration* rem) {
360 int64_t q = 0;
361 if (IDivFastPath(num, den, &q, rem)) {
362 return q;
363 }
364
365 const bool num_neg = num < ZeroDuration();
366 const bool den_neg = den < ZeroDuration();
367 const bool quotient_neg = num_neg != den_neg;
368
369 if (time_internal::IsInfiniteDuration(num) || den == ZeroDuration()) {
370 *rem = num_neg ? -InfiniteDuration() : InfiniteDuration();
371 return quotient_neg ? kint64min : kint64max;
372 }
373 if (time_internal::IsInfiniteDuration(den)) {
374 *rem = num;
375 return 0;
376 }
377
378 const uint128 a = MakeU128Ticks(num);
379 const uint128 b = MakeU128Ticks(den);
380 uint128 quotient128 = a / b;
381
382 if (satq) {
383 // Limits the quotient to the range of int64_t.
384 if (quotient128 > uint128(static_cast<uint64_t>(kint64max))) {
385 quotient128 = quotient_neg ? uint128(static_cast<uint64_t>(kint64min))
386 : uint128(static_cast<uint64_t>(kint64max));
387 }
388 }
389
390 const uint128 remainder128 = a - quotient128 * b;
391 *rem = MakeDurationFromU128(remainder128, num_neg);
392
393 if (!quotient_neg || quotient128 == 0) {
394 return Uint128Low64(quotient128) & kint64max;
395 }
396 // The quotient needs to be negated, but we need to carefully handle
397 // quotient128s with the top bit on.
398 return -static_cast<int64_t>(Uint128Low64(quotient128 - 1) & kint64max) - 1;
399 }
400
401 } // namespace time_internal
402
403 //
404 // Additive operators.
405 //
406
operator +=(Duration rhs)407 Duration& Duration::operator+=(Duration rhs) {
408 if (time_internal::IsInfiniteDuration(*this)) return *this;
409 if (time_internal::IsInfiniteDuration(rhs)) return *this = rhs;
410 const int64_t orig_rep_hi = rep_hi_;
411 rep_hi_ =
412 DecodeTwosComp(EncodeTwosComp(rep_hi_) + EncodeTwosComp(rhs.rep_hi_));
413 if (rep_lo_ >= kTicksPerSecond - rhs.rep_lo_) {
414 rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_) + 1);
415 rep_lo_ -= kTicksPerSecond;
416 }
417 rep_lo_ += rhs.rep_lo_;
418 if (rhs.rep_hi_ < 0 ? rep_hi_ > orig_rep_hi : rep_hi_ < orig_rep_hi) {
419 return *this = rhs.rep_hi_ < 0 ? -InfiniteDuration() : InfiniteDuration();
420 }
421 return *this;
422 }
423
operator -=(Duration rhs)424 Duration& Duration::operator-=(Duration rhs) {
425 if (time_internal::IsInfiniteDuration(*this)) return *this;
426 if (time_internal::IsInfiniteDuration(rhs)) {
427 return *this = rhs.rep_hi_ >= 0 ? -InfiniteDuration() : InfiniteDuration();
428 }
429 const int64_t orig_rep_hi = rep_hi_;
430 rep_hi_ =
431 DecodeTwosComp(EncodeTwosComp(rep_hi_) - EncodeTwosComp(rhs.rep_hi_));
432 if (rep_lo_ < rhs.rep_lo_) {
433 rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_) - 1);
434 rep_lo_ += kTicksPerSecond;
435 }
436 rep_lo_ -= rhs.rep_lo_;
437 if (rhs.rep_hi_ < 0 ? rep_hi_ < orig_rep_hi : rep_hi_ > orig_rep_hi) {
438 return *this = rhs.rep_hi_ >= 0 ? -InfiniteDuration() : InfiniteDuration();
439 }
440 return *this;
441 }
442
443 //
444 // Multiplicative operators.
445 //
446
operator *=(int64_t r)447 Duration& Duration::operator*=(int64_t r) {
448 if (time_internal::IsInfiniteDuration(*this)) {
449 const bool is_neg = (r < 0) != (rep_hi_ < 0);
450 return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
451 }
452 return *this = ScaleFixed<SafeMultiply>(*this, r);
453 }
454
operator *=(double r)455 Duration& Duration::operator*=(double r) {
456 if (time_internal::IsInfiniteDuration(*this) || !IsFinite(r)) {
457 const bool is_neg = (std::signbit(r) != 0) != (rep_hi_ < 0);
458 return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
459 }
460 return *this = ScaleDouble<std::multiplies>(*this, r);
461 }
462
operator /=(int64_t r)463 Duration& Duration::operator/=(int64_t r) {
464 if (time_internal::IsInfiniteDuration(*this) || r == 0) {
465 const bool is_neg = (r < 0) != (rep_hi_ < 0);
466 return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
467 }
468 return *this = ScaleFixed<std::divides>(*this, r);
469 }
470
operator /=(double r)471 Duration& Duration::operator/=(double r) {
472 if (time_internal::IsInfiniteDuration(*this) || !IsValidDivisor(r)) {
473 const bool is_neg = (std::signbit(r) != 0) != (rep_hi_ < 0);
474 return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
475 }
476 return *this = ScaleDouble<std::divides>(*this, r);
477 }
478
operator %=(Duration rhs)479 Duration& Duration::operator%=(Duration rhs) {
480 time_internal::IDivDuration(false, *this, rhs, this);
481 return *this;
482 }
483
FDivDuration(Duration num,Duration den)484 double FDivDuration(Duration num, Duration den) {
485 // Arithmetic with infinity is sticky.
486 if (time_internal::IsInfiniteDuration(num) || den == ZeroDuration()) {
487 return (num < ZeroDuration()) == (den < ZeroDuration())
488 ? std::numeric_limits<double>::infinity()
489 : -std::numeric_limits<double>::infinity();
490 }
491 if (time_internal::IsInfiniteDuration(den)) return 0.0;
492
493 double a =
494 static_cast<double>(time_internal::GetRepHi(num)) * kTicksPerSecond +
495 time_internal::GetRepLo(num);
496 double b =
497 static_cast<double>(time_internal::GetRepHi(den)) * kTicksPerSecond +
498 time_internal::GetRepLo(den);
499 return a / b;
500 }
501
502 //
503 // Trunc/Floor/Ceil.
504 //
505
Trunc(Duration d,Duration unit)506 Duration Trunc(Duration d, Duration unit) {
507 return d - (d % unit);
508 }
509
Floor(const Duration d,const Duration unit)510 Duration Floor(const Duration d, const Duration unit) {
511 const absl::Duration td = Trunc(d, unit);
512 return td <= d ? td : td - AbsDuration(unit);
513 }
514
Ceil(const Duration d,const Duration unit)515 Duration Ceil(const Duration d, const Duration unit) {
516 const absl::Duration td = Trunc(d, unit);
517 return td >= d ? td : td + AbsDuration(unit);
518 }
519
520 //
521 // Factory functions.
522 //
523
DurationFromTimespec(timespec ts)524 Duration DurationFromTimespec(timespec ts) {
525 if (static_cast<uint64_t>(ts.tv_nsec) < 1000 * 1000 * 1000) {
526 int64_t ticks = ts.tv_nsec * kTicksPerNanosecond;
527 return time_internal::MakeDuration(ts.tv_sec, ticks);
528 }
529 return Seconds(ts.tv_sec) + Nanoseconds(ts.tv_nsec);
530 }
531
DurationFromTimeval(timeval tv)532 Duration DurationFromTimeval(timeval tv) {
533 if (static_cast<uint64_t>(tv.tv_usec) < 1000 * 1000) {
534 int64_t ticks = tv.tv_usec * 1000 * kTicksPerNanosecond;
535 return time_internal::MakeDuration(tv.tv_sec, ticks);
536 }
537 return Seconds(tv.tv_sec) + Microseconds(tv.tv_usec);
538 }
539
540 //
541 // Conversion to other duration types.
542 //
543
ToInt64Nanoseconds(Duration d)544 int64_t ToInt64Nanoseconds(Duration d) {
545 if (time_internal::GetRepHi(d) >= 0 &&
546 time_internal::GetRepHi(d) >> 33 == 0) {
547 return (time_internal::GetRepHi(d) * 1000 * 1000 * 1000) +
548 (time_internal::GetRepLo(d) / kTicksPerNanosecond);
549 }
550 return d / Nanoseconds(1);
551 }
ToInt64Microseconds(Duration d)552 int64_t ToInt64Microseconds(Duration d) {
553 if (time_internal::GetRepHi(d) >= 0 &&
554 time_internal::GetRepHi(d) >> 43 == 0) {
555 return (time_internal::GetRepHi(d) * 1000 * 1000) +
556 (time_internal::GetRepLo(d) / (kTicksPerNanosecond * 1000));
557 }
558 return d / Microseconds(1);
559 }
ToInt64Milliseconds(Duration d)560 int64_t ToInt64Milliseconds(Duration d) {
561 if (time_internal::GetRepHi(d) >= 0 &&
562 time_internal::GetRepHi(d) >> 53 == 0) {
563 return (time_internal::GetRepHi(d) * 1000) +
564 (time_internal::GetRepLo(d) / (kTicksPerNanosecond * 1000 * 1000));
565 }
566 return d / Milliseconds(1);
567 }
ToInt64Seconds(Duration d)568 int64_t ToInt64Seconds(Duration d) {
569 int64_t hi = time_internal::GetRepHi(d);
570 if (time_internal::IsInfiniteDuration(d)) return hi;
571 if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
572 return hi;
573 }
ToInt64Minutes(Duration d)574 int64_t ToInt64Minutes(Duration d) {
575 int64_t hi = time_internal::GetRepHi(d);
576 if (time_internal::IsInfiniteDuration(d)) return hi;
577 if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
578 return hi / 60;
579 }
ToInt64Hours(Duration d)580 int64_t ToInt64Hours(Duration d) {
581 int64_t hi = time_internal::GetRepHi(d);
582 if (time_internal::IsInfiniteDuration(d)) return hi;
583 if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi;
584 return hi / (60 * 60);
585 }
586
ToDoubleNanoseconds(Duration d)587 double ToDoubleNanoseconds(Duration d) {
588 return FDivDuration(d, Nanoseconds(1));
589 }
ToDoubleMicroseconds(Duration d)590 double ToDoubleMicroseconds(Duration d) {
591 return FDivDuration(d, Microseconds(1));
592 }
ToDoubleMilliseconds(Duration d)593 double ToDoubleMilliseconds(Duration d) {
594 return FDivDuration(d, Milliseconds(1));
595 }
ToDoubleSeconds(Duration d)596 double ToDoubleSeconds(Duration d) {
597 return FDivDuration(d, Seconds(1));
598 }
ToDoubleMinutes(Duration d)599 double ToDoubleMinutes(Duration d) {
600 return FDivDuration(d, Minutes(1));
601 }
ToDoubleHours(Duration d)602 double ToDoubleHours(Duration d) {
603 return FDivDuration(d, Hours(1));
604 }
605
ToTimespec(Duration d)606 timespec ToTimespec(Duration d) {
607 timespec ts;
608 if (!time_internal::IsInfiniteDuration(d)) {
609 int64_t rep_hi = time_internal::GetRepHi(d);
610 uint32_t rep_lo = time_internal::GetRepLo(d);
611 if (rep_hi < 0) {
612 // Tweak the fields so that unsigned division of rep_lo
613 // maps to truncation (towards zero) for the timespec.
614 rep_lo += kTicksPerNanosecond - 1;
615 if (rep_lo >= kTicksPerSecond) {
616 rep_hi += 1;
617 rep_lo -= kTicksPerSecond;
618 }
619 }
620 ts.tv_sec = rep_hi;
621 if (ts.tv_sec == rep_hi) { // no time_t narrowing
622 ts.tv_nsec = rep_lo / kTicksPerNanosecond;
623 return ts;
624 }
625 }
626 if (d >= ZeroDuration()) {
627 ts.tv_sec = std::numeric_limits<time_t>::max();
628 ts.tv_nsec = 1000 * 1000 * 1000 - 1;
629 } else {
630 ts.tv_sec = std::numeric_limits<time_t>::min();
631 ts.tv_nsec = 0;
632 }
633 return ts;
634 }
635
ToTimeval(Duration d)636 timeval ToTimeval(Duration d) {
637 timeval tv;
638 timespec ts = ToTimespec(d);
639 if (ts.tv_sec < 0) {
640 // Tweak the fields so that positive division of tv_nsec
641 // maps to truncation (towards zero) for the timeval.
642 ts.tv_nsec += 1000 - 1;
643 if (ts.tv_nsec >= 1000 * 1000 * 1000) {
644 ts.tv_sec += 1;
645 ts.tv_nsec -= 1000 * 1000 * 1000;
646 }
647 }
648 tv.tv_sec = ts.tv_sec;
649 if (tv.tv_sec != ts.tv_sec) { // narrowing
650 if (ts.tv_sec < 0) {
651 tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>::min();
652 tv.tv_usec = 0;
653 } else {
654 tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>::max();
655 tv.tv_usec = 1000 * 1000 - 1;
656 }
657 return tv;
658 }
659 tv.tv_usec = static_cast<int>(ts.tv_nsec / 1000); // suseconds_t
660 return tv;
661 }
662
ToChronoNanoseconds(Duration d)663 std::chrono::nanoseconds ToChronoNanoseconds(Duration d) {
664 return time_internal::ToChronoDuration<std::chrono::nanoseconds>(d);
665 }
ToChronoMicroseconds(Duration d)666 std::chrono::microseconds ToChronoMicroseconds(Duration d) {
667 return time_internal::ToChronoDuration<std::chrono::microseconds>(d);
668 }
ToChronoMilliseconds(Duration d)669 std::chrono::milliseconds ToChronoMilliseconds(Duration d) {
670 return time_internal::ToChronoDuration<std::chrono::milliseconds>(d);
671 }
ToChronoSeconds(Duration d)672 std::chrono::seconds ToChronoSeconds(Duration d) {
673 return time_internal::ToChronoDuration<std::chrono::seconds>(d);
674 }
ToChronoMinutes(Duration d)675 std::chrono::minutes ToChronoMinutes(Duration d) {
676 return time_internal::ToChronoDuration<std::chrono::minutes>(d);
677 }
ToChronoHours(Duration d)678 std::chrono::hours ToChronoHours(Duration d) {
679 return time_internal::ToChronoDuration<std::chrono::hours>(d);
680 }
681
682 //
683 // To/From string formatting.
684 //
685
686 namespace {
687
688 // Formats a positive 64-bit integer in the given field width. Note that
689 // it is up to the caller of Format64() to ensure that there is sufficient
690 // space before ep to hold the conversion.
Format64(char * ep,int width,int64_t v)691 char* Format64(char* ep, int width, int64_t v) {
692 do {
693 --width;
694 *--ep = '0' + (v % 10); // contiguous digits
695 } while (v /= 10);
696 while (--width >= 0) *--ep = '0'; // zero pad
697 return ep;
698 }
699
700 // Helpers for FormatDuration() that format 'n' and append it to 'out'
701 // followed by the given 'unit'. If 'n' formats to "0", nothing is
702 // appended (not even the unit).
703
704 // A type that encapsulates how to display a value of a particular unit. For
705 // values that are displayed with fractional parts, the precision indicates
706 // where to round the value. The precision varies with the display unit because
707 // a Duration can hold only quarters of a nanosecond, so displaying information
708 // beyond that is just noise.
709 //
710 // For example, a microsecond value of 42.00025xxxxx should not display beyond 5
711 // fractional digits, because it is in the noise of what a Duration can
712 // represent.
713 struct DisplayUnit {
714 absl::string_view abbr;
715 int prec;
716 double pow10;
717 };
718 ABSL_CONST_INIT const DisplayUnit kDisplayNano = {"ns", 2, 1e2};
719 ABSL_CONST_INIT const DisplayUnit kDisplayMicro = {"us", 5, 1e5};
720 ABSL_CONST_INIT const DisplayUnit kDisplayMilli = {"ms", 8, 1e8};
721 ABSL_CONST_INIT const DisplayUnit kDisplaySec = {"s", 11, 1e11};
722 ABSL_CONST_INIT const DisplayUnit kDisplayMin = {"m", -1, 0.0}; // prec ignored
723 ABSL_CONST_INIT const DisplayUnit kDisplayHour = {"h", -1,
724 0.0}; // prec ignored
725
AppendNumberUnit(std::string * out,int64_t n,DisplayUnit unit)726 void AppendNumberUnit(std::string* out, int64_t n, DisplayUnit unit) {
727 char buf[sizeof("2562047788015216")]; // hours in max duration
728 char* const ep = buf + sizeof(buf);
729 char* bp = Format64(ep, 0, n);
730 if (*bp != '0' || bp + 1 != ep) {
731 out->append(bp, ep - bp);
732 out->append(unit.abbr.data(), unit.abbr.size());
733 }
734 }
735
736 // Note: unit.prec is limited to double's digits10 value (typically 15) so it
737 // always fits in buf[].
AppendNumberUnit(std::string * out,double n,DisplayUnit unit)738 void AppendNumberUnit(std::string* out, double n, DisplayUnit unit) {
739 constexpr int kBufferSize = std::numeric_limits<double>::digits10;
740 const int prec = std::min(kBufferSize, unit.prec);
741 char buf[kBufferSize]; // also large enough to hold integer part
742 char* ep = buf + sizeof(buf);
743 double d = 0;
744 int64_t frac_part = Round(std::modf(n, &d) * unit.pow10);
745 int64_t int_part = d;
746 if (int_part != 0 || frac_part != 0) {
747 char* bp = Format64(ep, 0, int_part); // always < 1000
748 out->append(bp, ep - bp);
749 if (frac_part != 0) {
750 out->push_back('.');
751 bp = Format64(ep, prec, frac_part);
752 while (ep[-1] == '0') --ep;
753 out->append(bp, ep - bp);
754 }
755 out->append(unit.abbr.data(), unit.abbr.size());
756 }
757 }
758
759 } // namespace
760
761 // From Go's doc at https://golang.org/pkg/time/#Duration.String
762 // [FormatDuration] returns a string representing the duration in the
763 // form "72h3m0.5s". Leading zero units are omitted. As a special
764 // case, durations less than one second format use a smaller unit
765 // (milli-, micro-, or nanoseconds) to ensure that the leading digit
766 // is non-zero.
767 // Unlike Go, we format the zero duration as 0, with no unit.
FormatDuration(Duration d)768 std::string FormatDuration(Duration d) {
769 const Duration min_duration = Seconds(kint64min);
770 if (d == min_duration) {
771 // Avoid needing to negate kint64min by directly returning what the
772 // following code should produce in that case.
773 return "-2562047788015215h30m8s";
774 }
775 std::string s;
776 if (d < ZeroDuration()) {
777 s.append("-");
778 d = -d;
779 }
780 if (d == InfiniteDuration()) {
781 s.append("inf");
782 } else if (d < Seconds(1)) {
783 // Special case for durations with a magnitude < 1 second. The duration
784 // is printed as a fraction of a single unit, e.g., "1.2ms".
785 if (d < Microseconds(1)) {
786 AppendNumberUnit(&s, FDivDuration(d, Nanoseconds(1)), kDisplayNano);
787 } else if (d < Milliseconds(1)) {
788 AppendNumberUnit(&s, FDivDuration(d, Microseconds(1)), kDisplayMicro);
789 } else {
790 AppendNumberUnit(&s, FDivDuration(d, Milliseconds(1)), kDisplayMilli);
791 }
792 } else {
793 AppendNumberUnit(&s, IDivDuration(d, Hours(1), &d), kDisplayHour);
794 AppendNumberUnit(&s, IDivDuration(d, Minutes(1), &d), kDisplayMin);
795 AppendNumberUnit(&s, FDivDuration(d, Seconds(1)), kDisplaySec);
796 }
797 if (s.empty() || s == "-") {
798 s = "0";
799 }
800 return s;
801 }
802
803 namespace {
804
805 // A helper for ParseDuration() that parses a leading number from the given
806 // string and stores the result in *int_part/*frac_part/*frac_scale. The
807 // given string pointer is modified to point to the first unconsumed char.
ConsumeDurationNumber(const char ** dpp,const char * ep,int64_t * int_part,int64_t * frac_part,int64_t * frac_scale)808 bool ConsumeDurationNumber(const char** dpp, const char* ep, int64_t* int_part,
809 int64_t* frac_part, int64_t* frac_scale) {
810 *int_part = 0;
811 *frac_part = 0;
812 *frac_scale = 1; // invariant: *frac_part < *frac_scale
813 const char* start = *dpp;
814 for (; *dpp != ep; *dpp += 1) {
815 const int d = **dpp - '0'; // contiguous digits
816 if (d < 0 || 10 <= d) break;
817
818 if (*int_part > kint64max / 10) return false;
819 *int_part *= 10;
820 if (*int_part > kint64max - d) return false;
821 *int_part += d;
822 }
823 const bool int_part_empty = (*dpp == start);
824 if (*dpp == ep || **dpp != '.') return !int_part_empty;
825
826 for (*dpp += 1; *dpp != ep; *dpp += 1) {
827 const int d = **dpp - '0'; // contiguous digits
828 if (d < 0 || 10 <= d) break;
829 if (*frac_scale <= kint64max / 10) {
830 *frac_part *= 10;
831 *frac_part += d;
832 *frac_scale *= 10;
833 }
834 }
835 return !int_part_empty || *frac_scale != 1;
836 }
837
838 // A helper for ParseDuration() that parses a leading unit designator (e.g.,
839 // ns, us, ms, s, m, h) from the given string and stores the resulting unit
840 // in "*unit". The given string pointer is modified to point to the first
841 // unconsumed char.
ConsumeDurationUnit(const char ** start,const char * end,Duration * unit)842 bool ConsumeDurationUnit(const char** start, const char* end, Duration* unit) {
843 size_t size = end - *start;
844 switch (size) {
845 case 0:
846 return false;
847 default:
848 switch (**start) {
849 case 'n':
850 if (*(*start + 1) == 's') {
851 *start += 2;
852 *unit = Nanoseconds(1);
853 return true;
854 }
855 break;
856 case 'u':
857 if (*(*start + 1) == 's') {
858 *start += 2;
859 *unit = Microseconds(1);
860 return true;
861 }
862 break;
863 case 'm':
864 if (*(*start + 1) == 's') {
865 *start += 2;
866 *unit = Milliseconds(1);
867 return true;
868 }
869 break;
870 default:
871 break;
872 }
873 ABSL_FALLTHROUGH_INTENDED;
874 case 1:
875 switch (**start) {
876 case 's':
877 *unit = Seconds(1);
878 *start += 1;
879 return true;
880 case 'm':
881 *unit = Minutes(1);
882 *start += 1;
883 return true;
884 case 'h':
885 *unit = Hours(1);
886 *start += 1;
887 return true;
888 default:
889 return false;
890 }
891 }
892 }
893
894 } // namespace
895
896 // From Go's doc at https://golang.org/pkg/time/#ParseDuration
897 // [ParseDuration] parses a duration string. A duration string is
898 // a possibly signed sequence of decimal numbers, each with optional
899 // fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m".
900 // Valid time units are "ns", "us" "ms", "s", "m", "h".
ParseDuration(absl::string_view dur_sv,Duration * d)901 bool ParseDuration(absl::string_view dur_sv, Duration* d) {
902 int sign = 1;
903 if (absl::ConsumePrefix(&dur_sv, "-")) {
904 sign = -1;
905 } else {
906 absl::ConsumePrefix(&dur_sv, "+");
907 }
908 if (dur_sv.empty()) return false;
909
910 // Special case for a string of "0".
911 if (dur_sv == "0") {
912 *d = ZeroDuration();
913 return true;
914 }
915
916 if (dur_sv == "inf") {
917 *d = sign * InfiniteDuration();
918 return true;
919 }
920
921 const char* start = dur_sv.data();
922 const char* end = start + dur_sv.size();
923
924 Duration dur;
925 while (start != end) {
926 int64_t int_part;
927 int64_t frac_part;
928 int64_t frac_scale;
929 Duration unit;
930 if (!ConsumeDurationNumber(&start, end, &int_part, &frac_part,
931 &frac_scale) ||
932 !ConsumeDurationUnit(&start, end, &unit)) {
933 return false;
934 }
935 if (int_part != 0) dur += sign * int_part * unit;
936 if (frac_part != 0) dur += sign * frac_part * unit / frac_scale;
937 }
938 *d = dur;
939 return true;
940 }
941
AbslParseFlag(absl::string_view text,Duration * dst,std::string *)942 bool AbslParseFlag(absl::string_view text, Duration* dst, std::string*) {
943 return ParseDuration(text, dst);
944 }
945
AbslUnparseFlag(Duration d)946 std::string AbslUnparseFlag(Duration d) { return FormatDuration(d); }
ParseFlag(const std::string & text,Duration * dst,std::string *)947 bool ParseFlag(const std::string& text, Duration* dst, std::string* ) {
948 return ParseDuration(text, dst);
949 }
950
UnparseFlag(Duration d)951 std::string UnparseFlag(Duration d) { return FormatDuration(d); }
952
953 ABSL_NAMESPACE_END
954 } // namespace absl
955