btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 5 | #include "platform/api/time.h" |
| 6 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 7 | #include <ctime> |
| 8 | #include <ratio> |
| 9 | |
Yuri Wiitala | 0fda1ab | 2019-11-20 11:43:36 -0800 | [diff] [blame^] | 10 | #include "util/logging.h" |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 11 | |
| 12 | using std::chrono::duration_cast; |
| 13 | using std::chrono::high_resolution_clock; |
| 14 | using std::chrono::hours; |
| 15 | using std::chrono::seconds; |
| 16 | using std::chrono::steady_clock; |
| 17 | using std::chrono::system_clock; |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 18 | |
| 19 | namespace openscreen { |
| 20 | namespace platform { |
| 21 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 22 | Clock::time_point Clock::now() noexcept { |
| 23 | constexpr bool can_use_steady_clock = |
| 24 | std::ratio_less_equal<steady_clock::period, |
| 25 | kRequiredClockResolution>::value; |
| 26 | constexpr bool can_use_high_resolution_clock = |
| 27 | std::ratio_less_equal<high_resolution_clock::period, |
| 28 | kRequiredClockResolution>::value && |
| 29 | high_resolution_clock::is_steady; |
| 30 | static_assert(can_use_steady_clock || can_use_high_resolution_clock, |
| 31 | "no suitable default clock on this platform"); |
| 32 | |
| 33 | // Choose whether to use the steady_clock or the high_resolution_clock. The |
| 34 | // general assumption here is that steady_clock will be the lesser expensive |
| 35 | // to use. Only fall-back to high_resolution_clock if steady_clock does not |
| 36 | // meet the resolution requirement. |
| 37 | // |
| 38 | // Note: Most of the expression below should be reduced at compile-time (by |
| 39 | // any half-decent optimizing compiler), and so there won't be any branching |
| 40 | // or significant math actually taking place here. |
| 41 | if (can_use_steady_clock) { |
| 42 | return Clock::time_point( |
| 43 | duration_cast<Clock::duration>(steady_clock::now().time_since_epoch())); |
| 44 | } |
| 45 | return Clock::time_point(duration_cast<Clock::duration>( |
| 46 | high_resolution_clock::now().time_since_epoch())); |
btolsch | 5292c94 | 2018-07-26 00:06:22 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Yuri Wiitala | eb8eee7 | 2019-03-26 15:52:43 -0700 | [diff] [blame] | 49 | std::chrono::seconds GetWallTimeSinceUnixEpoch() noexcept { |
| 50 | // Note: Even though std::time_t is not necessarily "seconds since UNIX epoch" |
| 51 | // before C++20, it is almost universally implemented that way on all |
| 52 | // platforms. There is a unit test to confirm this behavior, so don't worry |
| 53 | // about it here. |
| 54 | const std::time_t since_epoch = system_clock::to_time_t(system_clock::now()); |
| 55 | |
| 56 | // std::time_t is unspecified by the spec. If it's only a 32-bit integer, it's |
| 57 | // possible that values will overflow in early 2038. Warn future developers a |
| 58 | // year ahead of time. |
| 59 | if (sizeof(std::time_t) <= 4) { |
| 60 | constexpr std::time_t a_year_before_overflow = |
| 61 | std::numeric_limits<std::time_t>::max() - |
| 62 | duration_cast<seconds>(365 * hours(24)).count(); |
| 63 | OSP_DCHECK_LE(since_epoch, a_year_before_overflow); |
| 64 | } |
| 65 | |
| 66 | return std::chrono::seconds(since_epoch); |
btolsch | 9d6900c | 2018-05-30 18:22:53 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace platform |
| 70 | } // namespace openscreen |