blob: 7f843db719237ed70eba4da2beb3e2c896e49b9f [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
Yuri Wiitala0fda1ab2019-11-20 11:43:36 -080010#include "util/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 {
20namespace platform {
21
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070022Clock::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()));
btolsch5292c942018-07-26 00:06:22 -070047}
48
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070049std::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);
btolsch9d6900c2018-05-30 18:22:53 -070067}
68
69} // namespace platform
70} // namespace openscreen