1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
15 #include "pw_chrono/system_clock.h"
17 namespace pw::chrono {
20 class RealSystemClock final : public VirtualSystemClock {
22 SystemClock::time_point now() final { return SystemClock::now(); }
24 RealSystemClock real_system_clock;
28 VirtualSystemClock& VirtualSystemClock::RealClock() {
29 return real_system_clock;
32 } // namespace pw::chrono
34 extern "C" pw_chrono_SystemClock_TimePoint pw_chrono_SystemClock_Now() {
36 .duration_since_epoch = {
37 .ticks = pw::chrono::SystemClock::now().time_since_epoch().count()}};
40 extern "C" pw_chrono_SystemClock_Duration pw_chrono_SystemClock_TimeElapsed(
41 pw_chrono_SystemClock_TimePoint last_time,
42 pw_chrono_SystemClock_TimePoint current_time) {
43 return {.ticks = current_time.duration_since_epoch.ticks -
44 last_time.duration_since_epoch.ticks};
47 extern "C" pw_chrono_SystemClock_Nanoseconds
48 pw_chrono_SystemClock_DurationToNsFloor(
49 pw_chrono_SystemClock_Duration duration) {
50 return std::chrono::floor<std::chrono::nanoseconds>(
51 pw::chrono::SystemClock::duration(duration.ticks))