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