blob: 14a2f286aec0090c38b20488f4a9754b6a62b122 [file] [log] [blame]
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -07001// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#include "pw_chrono/system_clock.h"
16
17namespace pw::chrono {
18namespace {
19
20class RealSystemClock final : public VirtualSystemClock {
21 public:
22 SystemClock::time_point now() final { return SystemClock::now(); }
23};
24RealSystemClock real_system_clock;
25
26} // namespace
27
28VirtualSystemClock& VirtualSystemClock::RealClock() {
29 return real_system_clock;
30}
31
32} // namespace pw::chrono
33
34extern "C" pw_chrono_SystemClock_TimePoint pw_chrono_SystemClock_Now() {
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080035 return {
36 .duration_since_epoch = {
37 .ticks = pw::chrono::SystemClock::now().time_since_epoch().count()}};
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -070038}
39
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080040extern "C" pw_chrono_SystemClock_Duration pw_chrono_SystemClock_TimeElapsed(
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -070041 pw_chrono_SystemClock_TimePoint last_time,
42 pw_chrono_SystemClock_TimePoint current_time) {
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080043 return {.ticks = current_time.duration_since_epoch.ticks -
44 last_time.duration_since_epoch.ticks};
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -070045}
46
47extern "C" pw_chrono_SystemClock_Nanoseconds
Ewout van Bekkum830d5d12021-01-28 18:58:03 -080048pw_chrono_SystemClock_DurationToNsFloor(
49 pw_chrono_SystemClock_Duration duration) {
50 return std::chrono::floor<std::chrono::nanoseconds>(
51 pw::chrono::SystemClock::duration(duration.ticks))
Ewout van Bekkum3c61ae92020-10-30 15:52:25 -070052 .count();
53}