| 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 | #if defined(__GNUC__) && !KJ_HEADER_WARNINGS |
| 26 | #pragma GCC system_header |
| 27 | #endif |
| 28 | |
| 29 | #include "units.h" |
| 30 | #include <inttypes.h> |
| 31 | |
| 32 | namespace kj { |
| 33 | namespace _ { // private |
| 34 | |
| 35 | class NanosecondLabel; |
| 36 | class TimeLabel; |
| 37 | class DateLabel; |
| 38 | |
| 39 | } // namespace _ (private) |
| 40 | |
| 41 | using Duration = Quantity<int64_t, _::NanosecondLabel>; |
| 42 | // A time value, in nanoseconds. |
| 43 | |
| 44 | constexpr Duration NANOSECONDS = unit<Duration>(); |
| 45 | constexpr Duration MICROSECONDS = 1000 * NANOSECONDS; |
| 46 | constexpr Duration MILLISECONDS = 1000 * MICROSECONDS; |
| 47 | constexpr Duration SECONDS = 1000 * MILLISECONDS; |
| 48 | constexpr Duration MINUTES = 60 * SECONDS; |
| 49 | constexpr Duration HOURS = 60 * MINUTES; |
| 50 | constexpr Duration DAYS = 24 * HOURS; |
| 51 | |
| 52 | using TimePoint = Absolute<Duration, _::TimeLabel>; |
| 53 | // An absolute time measured by some particular instance of `Timer`. `Time`s from two different |
| 54 | // `Timer`s may be measured from different origins and so 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 | constexpr Date UNIX_EPOCH = origin<Date>(); |
| 60 | // The `Date` representing Jan 1, 1970 00:00:00 UTC. |
| 61 | |
| 62 | class Clock { |
| 63 | // Interface to read the current date and time. |
| 64 | public: |
| 65 | virtual Date now() const = 0; |
| 66 | }; |
| 67 | |
| 68 | const Clock& nullClock(); |
| 69 | // A clock which always returns UNIX_EPOCH as the current time. Useful when you don't care about |
| 70 | // time. |
| 71 | |
| 72 | } // namespace kj |
| 73 | |