David Drysdale | 30196cf | 2023-12-02 19:24:15 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2022 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Monotonic clock implementation. |
| 17 | |
| 18 | use kmr_common::crypto; |
| 19 | use std::time::Instant; |
| 20 | |
| 21 | /// Monotonic clock. |
| 22 | pub struct StdClock { |
| 23 | start: Instant, |
| 24 | } |
| 25 | |
| 26 | impl StdClock { |
| 27 | /// Create new clock instance, holding time since construction. |
| 28 | pub fn new() -> Self { |
| 29 | Self { |
| 30 | start: Instant::now(), |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl crypto::MonotonicClock for StdClock { |
| 36 | fn now(&self) -> crypto::MillisecondsSinceEpoch { |
| 37 | let duration = self.start.elapsed(); |
| 38 | crypto::MillisecondsSinceEpoch(duration.as_millis().try_into().unwrap()) |
| 39 | } |
| 40 | } |