xref: /aosp_15_r20/external/openscreen/platform/api/time.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1*3f982cf4SFabien Sanglard // Copyright 2018 The Chromium Authors. All rights reserved.
2*3f982cf4SFabien Sanglard // Use of this source code is governed by a BSD-style license that can be
3*3f982cf4SFabien Sanglard // found in the LICENSE file.
4*3f982cf4SFabien Sanglard 
5*3f982cf4SFabien Sanglard #ifndef PLATFORM_API_TIME_H_
6*3f982cf4SFabien Sanglard #define PLATFORM_API_TIME_H_
7*3f982cf4SFabien Sanglard 
8*3f982cf4SFabien Sanglard #include <chrono>
9*3f982cf4SFabien Sanglard 
10*3f982cf4SFabien Sanglard #include "platform/base/trivial_clock_traits.h"
11*3f982cf4SFabien Sanglard 
12*3f982cf4SFabien Sanglard namespace openscreen {
13*3f982cf4SFabien Sanglard 
14*3f982cf4SFabien Sanglard // The "reasonably high-resolution" source of monotonic time from the embedder,
15*3f982cf4SFabien Sanglard // exhibiting the traits described in TrivialClockTraits. This class is not
16*3f982cf4SFabien Sanglard // instantiated. It only contains a static now() function.
17*3f982cf4SFabien Sanglard //
18*3f982cf4SFabien Sanglard // For example, the default platform implementation bases this on
19*3f982cf4SFabien Sanglard // std::chrono::steady_clock or std::chrono::high_resolution_clock, but an
20*3f982cf4SFabien Sanglard // embedder may choose to use a different source of time (e.g., the embedder's
21*3f982cf4SFabien Sanglard // time library, a simulated time source, or a mock).
22*3f982cf4SFabien Sanglard class Clock : public TrivialClockTraits {
23*3f982cf4SFabien Sanglard  public:
24*3f982cf4SFabien Sanglard   // Returns the current time.
25*3f982cf4SFabien Sanglard   static time_point now() noexcept;
26*3f982cf4SFabien Sanglard };
27*3f982cf4SFabien Sanglard 
28*3f982cf4SFabien Sanglard // Returns the number of seconds since UNIX epoch (1 Jan 1970, midnight)
29*3f982cf4SFabien Sanglard // according to the wall clock, which is subject to adjustments (e.g., via NTP).
30*3f982cf4SFabien Sanglard // Note that this is NOT necessarily the same time source as Clock::now() above,
31*3f982cf4SFabien Sanglard // and is NOT guaranteed to be monotonically non-decreasing; it is "calendar
32*3f982cf4SFabien Sanglard // time."
33*3f982cf4SFabien Sanglard std::chrono::seconds GetWallTimeSinceUnixEpoch() noexcept;
34*3f982cf4SFabien Sanglard 
35*3f982cf4SFabien Sanglard }  // namespace openscreen
36*3f982cf4SFabien Sanglard 
37*3f982cf4SFabien Sanglard #endif  // PLATFORM_API_TIME_H_
38