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