blob: f4a3e85f292348b9f990d96089069a0aa37dbc5e [file] [log] [blame]
btolsch5292c942018-07-26 00:06:22 -07001// 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
btolsch9d6900c2018-05-30 18:22:53 -07005#include "platform/api/time.h"
6
Jordan Baylesc9201dd2020-06-02 15:51:31 -07007#include <chrono>
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -07008#include <ctime>
9#include <ratio>
10
Jordan Baylesc9201dd2020-06-02 15:51:31 -070011#include "util/chrono_helpers.h"
Jordan Baylesa44d04a2020-05-06 16:59:39 -070012#include "util/osp_logging.h"
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070013
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070014using std::chrono::high_resolution_clock;
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070015using std::chrono::steady_clock;
16using std::chrono::system_clock;
btolsch9d6900c2018-05-30 18:22:53 -070017
18namespace openscreen {
btolsch9d6900c2018-05-30 18:22:53 -070019
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070020Clock::time_point Clock::now() noexcept {
21 constexpr bool can_use_steady_clock =
22 std::ratio_less_equal<steady_clock::period,
Yuri Wiitalaf162a612019-11-22 22:46:04 -080023 Clock::kRequiredResolution>::value;
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070024 constexpr bool can_use_high_resolution_clock =
25 std::ratio_less_equal<high_resolution_clock::period,
Yuri Wiitalaf162a612019-11-22 22:46:04 -080026 Clock::kRequiredResolution>::value &&
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070027 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 Baylesc9201dd2020-06-02 15:51:31 -070041 Clock::to_duration(steady_clock::now().time_since_epoch()));
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070042 }
Jordan Baylesc9201dd2020-06-02 15:51:31 -070043 return Clock::time_point(
44 Clock::to_duration(high_resolution_clock::now().time_since_epoch()));
btolsch5292c942018-07-26 00:06:22 -070045}
46
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070047std::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 Baylesc9201dd2020-06-02 15:51:31 -070060 to_seconds(365 * hours(24)).count();
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070061 OSP_DCHECK_LE(since_epoch, a_year_before_overflow);
62 }
63
64 return std::chrono::seconds(since_epoch);
btolsch9d6900c2018-05-30 18:22:53 -070065}
66
btolsch9d6900c2018-05-30 18:22:53 -070067} // namespace openscreen