jpayne@69: // Copyright (c) 2014 Google Inc. (contributed by Remy Blank ) jpayne@69: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors jpayne@69: // Licensed under the MIT License: jpayne@69: // jpayne@69: // Permission is hereby granted, free of charge, to any person obtaining a copy jpayne@69: // of this software and associated documentation files (the "Software"), to deal jpayne@69: // in the Software without restriction, including without limitation the rights jpayne@69: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell jpayne@69: // copies of the Software, and to permit persons to whom the Software is jpayne@69: // furnished to do so, subject to the following conditions: jpayne@69: // jpayne@69: // The above copyright notice and this permission notice shall be included in jpayne@69: // all copies or substantial portions of the Software. jpayne@69: // jpayne@69: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jpayne@69: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jpayne@69: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jpayne@69: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jpayne@69: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, jpayne@69: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN jpayne@69: // THE SOFTWARE. jpayne@69: jpayne@69: #pragma once jpayne@69: jpayne@69: #include "units.h" jpayne@69: #include jpayne@69: #include "string.h" jpayne@69: jpayne@69: KJ_BEGIN_HEADER jpayne@69: jpayne@69: namespace kj { jpayne@69: namespace _ { // private jpayne@69: jpayne@69: class NanosecondLabel; jpayne@69: class TimeLabel; jpayne@69: class DateLabel; jpayne@69: jpayne@69: static constexpr size_t TIME_STR_LEN = sizeof(int64_t) * 3 + 8; jpayne@69: // Maximum length of a stringified time. 3 digits per byte of integer, plus 8 digits to cover jpayne@69: // negative sign, decimal point, unit, NUL terminator, and anything else that might sneak in. jpayne@69: jpayne@69: } // namespace _ (private) jpayne@69: jpayne@69: using Duration = Quantity; jpayne@69: // A time value, in nanoseconds. jpayne@69: jpayne@69: constexpr Duration NANOSECONDS = unit(); jpayne@69: constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; jpayne@69: constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; jpayne@69: constexpr Duration SECONDS = 1000 * MILLISECONDS; jpayne@69: constexpr Duration MINUTES = 60 * SECONDS; jpayne@69: constexpr Duration HOURS = 60 * MINUTES; jpayne@69: constexpr Duration DAYS = 24 * HOURS; jpayne@69: jpayne@69: using TimePoint = Absolute; jpayne@69: // An absolute time measured by some particular instance of `Timer` or `MonotonicClock`. `Time`s jpayne@69: // from two different `Timer`s or `MonotonicClock`s may be measured from different origins and so jpayne@69: // are not necessarily compatible. jpayne@69: jpayne@69: using Date = Absolute; jpayne@69: // A point in real-world time, measured relative to the Unix epoch (Jan 1, 1970 00:00:00 UTC). jpayne@69: jpayne@69: CappedArray KJ_STRINGIFY(TimePoint); jpayne@69: CappedArray KJ_STRINGIFY(Date); jpayne@69: CappedArray KJ_STRINGIFY(Duration); jpayne@69: jpayne@69: constexpr Date UNIX_EPOCH = origin(); jpayne@69: // The `Date` representing Jan 1, 1970 00:00:00 UTC. jpayne@69: jpayne@69: class Clock { jpayne@69: // Interface to read the current date and time. jpayne@69: public: jpayne@69: virtual Date now() const = 0; jpayne@69: }; jpayne@69: jpayne@69: class MonotonicClock { jpayne@69: // Interface to read time in a way that increases as real-world time increases, independent of jpayne@69: // any manual changes to the calendar date/time. Such a clock never "goes backwards" even if the jpayne@69: // system administrator changes the calendar time or suspends the system. However, this clock's jpayne@69: // time points are only meaningful in comparison to other time points from the same clock, and jpayne@69: // cannot be used to determine the current calendar date. jpayne@69: jpayne@69: public: jpayne@69: virtual TimePoint now() const = 0; jpayne@69: }; jpayne@69: jpayne@69: const Clock& nullClock(); jpayne@69: // A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about jpayne@69: // time. jpayne@69: jpayne@69: const Clock& systemCoarseCalendarClock(); jpayne@69: const Clock& systemPreciseCalendarClock(); jpayne@69: // A clock that reads the real system time. jpayne@69: // jpayne@69: // In well-designed code, this should only be called by the top-level dependency injector. All jpayne@69: // other modules should request that the caller provide a Clock so that alternate clock jpayne@69: // implementations can be injected for testing, simulation, reproducibility, and other purposes. jpayne@69: // jpayne@69: // The "coarse" version has precision around 1-10ms, while the "precise" version has precision jpayne@69: // better than 1us. The "precise" version may be slightly slower, though on modern hardware and jpayne@69: // a reasonable operating system the difference is usually negligible. jpayne@69: // jpayne@69: // Note: On Windows prior to Windows 8, there is no precise calendar clock; the "precise" clock jpayne@69: // will be no more precise than the "coarse" clock in this case. jpayne@69: jpayne@69: const MonotonicClock& systemCoarseMonotonicClock(); jpayne@69: const MonotonicClock& systemPreciseMonotonicClock(); jpayne@69: // A MonotonicClock that reads the real system time. jpayne@69: // jpayne@69: // In well-designed code, this should only be called by the top-level dependency injector. All jpayne@69: // other modules should request that the caller provide a Clock so that alternate clock jpayne@69: // implementations can be injected for testing, simulation, reproducibility, and other purposes. jpayne@69: // jpayne@69: // The "coarse" version has precision around 1-10ms, while the "precise" version has precision jpayne@69: // better than 1us. The "precise" version may be slightly slower, though on modern hardware and jpayne@69: // a reasonable operating system the difference is usually negligible. jpayne@69: } // namespace kj jpayne@69: jpayne@69: KJ_END_HEADER