time.h (5000B)
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 } // namespace _ (private) 39 40 using Duration = Quantity<int64_t, _::NanosecondLabel>; 41 // A time value, in nanoseconds. 42 43 constexpr Duration NANOSECONDS = unit<Duration>(); 44 constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; 45 constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; 46 constexpr Duration SECONDS = 1000 * MILLISECONDS; 47 constexpr Duration MINUTES = 60 * SECONDS; 48 constexpr Duration HOURS = 60 * MINUTES; 49 constexpr Duration DAYS = 24 * HOURS; 50 51 using TimePoint = Absolute<Duration, _::TimeLabel>; 52 // An absolute time measured by some particular instance of `Timer` or `MonotonicClock`. `Time`s 53 // from two different `Timer`s or `MonotonicClock`s may be measured from different origins and so 54 // are not necessarily compatible. 55 56 using Date = Absolute<Duration, _::DateLabel>; 57 // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC). 58 59 CappedArray<char, sizeof(int64_t) * 3 + 2 + 4> KJ_STRINGIFY(TimePoint); 60 CappedArray<char, sizeof(int64_t) * 3 + 2 + 4> KJ_STRINGIFY(Date); 61 CappedArray<char, sizeof(int64_t) * 3 + 2 + 4> KJ_STRINGIFY(Duration); 62 63 constexpr Date UNIX_EPOCH = origin<Date>(); 64 // The `Date` representing Jan 1, 1970 00:00:00 UTC. 65 66 class Clock { 67 // Interface to read the current date and time. 68 public: 69 virtual Date now() const = 0; 70 }; 71 72 class MonotonicClock { 73 // Interface to read time in a way that increases as real-world time increases, independent of 74 // any manual changes to the calendar date/time. Such a clock never "goes backwards" even if the 75 // system administrator changes the calendar time or suspends the system. However, this clock's 76 // time points are only meaningful in comparison to other time points from the same clock, and 77 // cannot be used to determine the current calendar date. 78 79 public: 80 virtual TimePoint now() const = 0; 81 }; 82 83 const Clock& nullClock(); 84 // A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about 85 // time. 86 87 const Clock& systemCoarseCalendarClock(); 88 const Clock& systemPreciseCalendarClock(); 89 // A clock that reads the real system time. 90 // 91 // In well-designed code, this should only be called by the top-level dependency injector. All 92 // other modules should request that the caller provide a Clock so that alternate clock 93 // implementations can be injected for testing, simulation, reproducibility, and other purposes. 94 // 95 // The "coarse" version has precision around 1-10ms, while the "precise" version has precision 96 // better than 1us. The "precise" version may be slightly slower, though on modern hardware and 97 // a reasonable operating system the difference is usually negligible. 98 // 99 // Note: On Windows prior to Windows 8, there is no precise calendar clock; the "precise" clock 100 // will be no more precise than the "coarse" clock in this case. 101 102 const MonotonicClock& systemCoarseMonotonicClock(); 103 const MonotonicClock& systemPreciseMonotonicClock(); 104 // A MonotonicClock that reads the real system time. 105 // 106 // In well-designed code, this should only be called by the top-level dependency injector. All 107 // other modules should request that the caller provide a Clock so that alternate clock 108 // implementations can be injected for testing, simulation, reproducibility, and other purposes. 109 // 110 // The "coarse" version has precision around 1-10ms, while the "precise" version has precision 111 // better than 1us. The "precise" version may be slightly slower, though on modern hardware and 112 // a reasonable operating system the difference is usually negligible. 113 } // namespace kj 114 115 KJ_END_HEADER