• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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 
17 namespace pw::chrono {
18 namespace {
19 
20 class RealSystemClock final : public VirtualSystemClock {
21  public:
now()22   SystemClock::time_point now() final { return SystemClock::now(); }
23 };
24 RealSystemClock real_system_clock;
25 
26 }  // namespace
27 
RealClock()28 VirtualSystemClock& VirtualSystemClock::RealClock() {
29   return real_system_clock;
30 }
31 
32 }  // namespace pw::chrono
33 
pw_chrono_SystemClock_Now()34 extern "C" pw_chrono_SystemClock_TimePoint pw_chrono_SystemClock_Now() {
35   return {
36       .duration_since_epoch = {
37           .ticks = pw::chrono::SystemClock::now().time_since_epoch().count()}};
38 }
39 
pw_chrono_SystemClock_TimeElapsed(pw_chrono_SystemClock_TimePoint last_time,pw_chrono_SystemClock_TimePoint current_time)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};
45 }
46 
47 extern "C" pw_chrono_SystemClock_Nanoseconds
pw_chrono_SystemClock_DurationToNsFloor(pw_chrono_SystemClock_Duration duration)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))
52       .count();
53 }
54