Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 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: |
| 22 | SystemClock::time_point now() final { return SystemClock::now(); } |
| 23 | }; |
| 24 | RealSystemClock real_system_clock; |
| 25 | |
| 26 | } // namespace |
| 27 | |
| 28 | VirtualSystemClock& VirtualSystemClock::RealClock() { |
| 29 | return real_system_clock; |
| 30 | } |
| 31 | |
| 32 | } // namespace pw::chrono |
| 33 | |
| 34 | extern "C" pw_chrono_SystemClock_TimePoint pw_chrono_SystemClock_Now() { |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 35 | return { |
| 36 | .duration_since_epoch = { |
| 37 | .ticks = pw::chrono::SystemClock::now().time_since_epoch().count()}}; |
Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 40 | extern "C" pw_chrono_SystemClock_Duration pw_chrono_SystemClock_TimeElapsed( |
Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 41 | pw_chrono_SystemClock_TimePoint last_time, |
| 42 | pw_chrono_SystemClock_TimePoint current_time) { |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 43 | return {.ticks = current_time.duration_since_epoch.ticks - |
| 44 | last_time.duration_since_epoch.ticks}; |
Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | extern "C" pw_chrono_SystemClock_Nanoseconds |
Ewout van Bekkum | 830d5d1 | 2021-01-28 18:58:03 -0800 | [diff] [blame] | 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)) |
Ewout van Bekkum | 3c61ae9 | 2020-10-30 15:52:25 -0700 | [diff] [blame] | 52 | .count(); |
| 53 | } |