blob: cf850bb9b7d97b31fede5515f1464de46e6b4775 [file] [log] [blame]
mark a. foltzc28ca402018-07-19 16:11:32 -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
5#ifndef PLATFORM_API_TIME_H_
6#define PLATFORM_API_TIME_H_
7
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -07008#include <chrono>
mark a. foltzc28ca402018-07-19 16:11:32 -07009
10namespace openscreen {
11namespace platform {
12
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070013// For proper behavior of the OpenScreen library, the Clock implementation must
14// tick at least 10000 times per second.
15using kRequiredClockResolution = std::ratio<1, 10000>;
16
17// A monotonic clock that meets all the C++14 requirements of a TrivialClock,
18// for use with the chrono library. The default platform implementation bases
19// this on std::chrono::steady_clock or std::chrono::high_resolution_clock, but
20// a custom implementation may use a different source of time (e.g., an
21// embedder's time library, a simulated time source, or a mock).
22class Clock {
btolsch5292c942018-07-26 00:06:22 -070023 public:
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070024 // TrivialClock named requirements: std::chrono templates can/may use these.
25 using duration = std::chrono::microseconds;
26 using rep = duration::rep;
27 using period = duration::period;
28 using time_point = std::chrono::time_point<Clock, duration>;
29 static constexpr bool is_steady = true;
mark a. foltzc28ca402018-07-19 16:11:32 -070030
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070031 static time_point now() noexcept;
btolsch5292c942018-07-26 00:06:22 -070032
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070033 // In <chrono>, a Clock is just some type properties plus a static now()
34 // function. So, there's nothing to instantiate here.
35 Clock() = delete;
36 ~Clock() = delete;
btolschc11dc342019-01-16 16:37:34 -080037
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070038 // "Trivially copyable" is necessary for use in std::atomic<>.
39 static_assert(std::is_trivially_copyable<duration>(),
40 "duration is not trivially copyable");
41 static_assert(std::is_trivially_copyable<time_point>(),
42 "time_point is not trivially copyable");
btolsch5292c942018-07-26 00:06:22 -070043};
44
Yuri Wiitalaeb8eee72019-03-26 15:52:43 -070045// Convenience, for injecting Clocks into classes. Note: The 'noexcept' keyword
46// is dropped here to avoid a well-known Clang compiler warning (about an
47// upcoming C++20 ABI change).
48using ClockNowFunctionPtr = Clock::time_point (*)();
49
50// Returns the number of seconds since UNIX epoch (1 Jan 1970, midnight)
51// according to the wall clock, which is subject to adjustments (e.g., via
52// NTP). Note that this is NOT the same time source as Clock::now() above, and
53// is NOT guaranteed to be monotonically non-decreasing.
54std::chrono::seconds GetWallTimeSinceUnixEpoch() noexcept;
mark a. foltzc28ca402018-07-19 16:11:32 -070055
56} // namespace platform
57} // namespace openscreen
58
btolscha21e8ed2018-08-30 15:13:48 -070059#endif // PLATFORM_API_TIME_H_