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