annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/time.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 // Copyright (c) 2014 Google Inc. (contributed by Remy Blank <rblank@google.com>)
jpayne@69 2 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
jpayne@69 3 // Licensed under the MIT License:
jpayne@69 4 //
jpayne@69 5 // Permission is hereby granted, free of charge, to any person obtaining a copy
jpayne@69 6 // of this software and associated documentation files (the "Software"), to deal
jpayne@69 7 // in the Software without restriction, including without limitation the rights
jpayne@69 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jpayne@69 9 // copies of the Software, and to permit persons to whom the Software is
jpayne@69 10 // furnished to do so, subject to the following conditions:
jpayne@69 11 //
jpayne@69 12 // The above copyright notice and this permission notice shall be included in
jpayne@69 13 // all copies or substantial portions of the Software.
jpayne@69 14 //
jpayne@69 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jpayne@69 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jpayne@69 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jpayne@69 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jpayne@69 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jpayne@69 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jpayne@69 21 // THE SOFTWARE.
jpayne@69 22
jpayne@69 23 #pragma once
jpayne@69 24
jpayne@69 25 #include "units.h"
jpayne@69 26 #include <inttypes.h>
jpayne@69 27 #include "string.h"
jpayne@69 28
jpayne@69 29 KJ_BEGIN_HEADER
jpayne@69 30
jpayne@69 31 namespace kj {
jpayne@69 32 namespace _ { // private
jpayne@69 33
jpayne@69 34 class NanosecondLabel;
jpayne@69 35 class TimeLabel;
jpayne@69 36 class DateLabel;
jpayne@69 37
jpayne@69 38 static constexpr size_t TIME_STR_LEN = sizeof(int64_t) * 3 + 8;
jpayne@69 39 // Maximum length of a stringified time. 3 digits per byte of integer, plus 8 digits to cover
jpayne@69 40 // negative sign, decimal point, unit, NUL terminator, and anything else that might sneak in.
jpayne@69 41
jpayne@69 42 } // namespace _ (private)
jpayne@69 43
jpayne@69 44 using Duration = Quantity<int64_t, _::NanosecondLabel>;
jpayne@69 45 // A time value, in nanoseconds.
jpayne@69 46
jpayne@69 47 constexpr Duration NANOSECONDS = unit<Duration>();
jpayne@69 48 constexpr Duration MICROSECONDS = 1000 * NANOSECONDS;
jpayne@69 49 constexpr Duration MILLISECONDS = 1000 * MICROSECONDS;
jpayne@69 50 constexpr Duration SECONDS = 1000 * MILLISECONDS;
jpayne@69 51 constexpr Duration MINUTES = 60 * SECONDS;
jpayne@69 52 constexpr Duration HOURS = 60 * MINUTES;
jpayne@69 53 constexpr Duration DAYS = 24 * HOURS;
jpayne@69 54
jpayne@69 55 using TimePoint = Absolute<Duration, _::TimeLabel>;
jpayne@69 56 // An absolute time measured by some particular instance of `Timer` or `MonotonicClock`. `Time`s
jpayne@69 57 // from two different `Timer`s or `MonotonicClock`s may be measured from different origins and so
jpayne@69 58 // are not necessarily compatible.
jpayne@69 59
jpayne@69 60 using Date = Absolute<Duration, _::DateLabel>;
jpayne@69 61 // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC).
jpayne@69 62
jpayne@69 63 CappedArray<char, _::TIME_STR_LEN> KJ_STRINGIFY(TimePoint);
jpayne@69 64 CappedArray<char, _::TIME_STR_LEN> KJ_STRINGIFY(Date);
jpayne@69 65 CappedArray<char, _::TIME_STR_LEN> KJ_STRINGIFY(Duration);
jpayne@69 66
jpayne@69 67 constexpr Date UNIX_EPOCH = origin<Date>();
jpayne@69 68 // The `Date` representing Jan 1, 1970 00:00:00 UTC.
jpayne@69 69
jpayne@69 70 class Clock {
jpayne@69 71 // Interface to read the current date and time.
jpayne@69 72 public:
jpayne@69 73 virtual Date now() const = 0;
jpayne@69 74 };
jpayne@69 75
jpayne@69 76 class MonotonicClock {
jpayne@69 77 // Interface to read time in a way that increases as real-world time increases, independent of
jpayne@69 78 // any manual changes to the calendar date/time. Such a clock never "goes backwards" even if the
jpayne@69 79 // system administrator changes the calendar time or suspends the system. However, this clock's
jpayne@69 80 // time points are only meaningful in comparison to other time points from the same clock, and
jpayne@69 81 // cannot be used to determine the current calendar date.
jpayne@69 82
jpayne@69 83 public:
jpayne@69 84 virtual TimePoint now() const = 0;
jpayne@69 85 };
jpayne@69 86
jpayne@69 87 const Clock& nullClock();
jpayne@69 88 // A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about
jpayne@69 89 // time.
jpayne@69 90
jpayne@69 91 const Clock& systemCoarseCalendarClock();
jpayne@69 92 const Clock& systemPreciseCalendarClock();
jpayne@69 93 // A clock that reads the real system time.
jpayne@69 94 //
jpayne@69 95 // In well-designed code, this should only be called by the top-level dependency injector. All
jpayne@69 96 // other modules should request that the caller provide a Clock so that alternate clock
jpayne@69 97 // implementations can be injected for testing, simulation, reproducibility, and other purposes.
jpayne@69 98 //
jpayne@69 99 // The "coarse" version has precision around 1-10ms, while the "precise" version has precision
jpayne@69 100 // better than 1us. The "precise" version may be slightly slower, though on modern hardware and
jpayne@69 101 // a reasonable operating system the difference is usually negligible.
jpayne@69 102 //
jpayne@69 103 // Note: On Windows prior to Windows 8, there is no precise calendar clock; the "precise" clock
jpayne@69 104 // will be no more precise than the "coarse" clock in this case.
jpayne@69 105
jpayne@69 106 const MonotonicClock& systemCoarseMonotonicClock();
jpayne@69 107 const MonotonicClock& systemPreciseMonotonicClock();
jpayne@69 108 // A MonotonicClock that reads the real system time.
jpayne@69 109 //
jpayne@69 110 // In well-designed code, this should only be called by the top-level dependency injector. All
jpayne@69 111 // other modules should request that the caller provide a Clock so that alternate clock
jpayne@69 112 // implementations can be injected for testing, simulation, reproducibility, and other purposes.
jpayne@69 113 //
jpayne@69 114 // The "coarse" version has precision around 1-10ms, while the "precise" version has precision
jpayne@69 115 // better than 1us. The "precise" version may be slightly slower, though on modern hardware and
jpayne@69 116 // a reasonable operating system the difference is usually negligible.
jpayne@69 117 } // namespace kj
jpayne@69 118
jpayne@69 119 KJ_END_HEADER