From 42b78c6e0e9f1a134fda20b306a5194051425cb5 Mon Sep 17 00:00:00 2001 From: Mihail Malo Date: Thu, 20 Aug 2020 16:11:00 +0300 Subject: [PATCH] Remove usage of chrono in internals (#933) --- src/internal/timer.rs | 51 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/internal/timer.rs b/src/internal/timer.rs index 5099e7d4190..e84243e4f1f 100644 --- a/src/internal/timer.rs +++ b/src/internal/timer.rs @@ -1,51 +1,50 @@ -use chrono::{DateTime, Duration, Utc}; -use std::time::Duration as StdDuration; -use tokio::time::delay_for; +use tokio::time::{delay_until, Duration, Instant}; +/// A reusable timer that keeps track of its duration/interval/period. #[derive(Debug)] pub struct Timer { - due: DateTime, + due: Instant, duration: Duration, } impl Timer { + /// Constructs a `Timer`, initially armed to expire in `duration_in_ms` from the current instant. pub fn new(duration_in_ms: u64) -> Timer { - let duration = Duration::milliseconds(duration_in_ms as i64); + let duration = Duration::from_millis(duration_in_ms); Timer { - due: Utc::now() + duration, + due: Instant::now() + duration, duration, } } + /// Blocks until the due time. + /// The timer wil be reset afterwards, `duration` later. pub async fn hold(&mut self) { - let due_time = (self.due.timestamp() * 1000) + i64::from(self.due.timestamp_subsec_millis()); - let now_time = { - let now = Utc::now(); - - (now.timestamp() * 1000) + i64::from(now.timestamp_subsec_millis()) - }; - - if due_time > now_time { - let sleep_time = due_time - now_time; - - if sleep_time > 0 { - delay_for(StdDuration::from_millis(sleep_time as u64)).await; - } - } - - self.due = self.due + self.duration; + delay_until(self.due).await; + self.increment(); } + /// Returns true if the timer is expired (current instant past `due` time). + /// Resets the timer `duration` later if it is. pub fn check(&mut self) -> bool { - if Utc::now() >= self.due { - self.due = self.due + self.duration; - + if Instant::now() >= self.due { + self.increment(); true } else { false } } - pub fn reset(&mut self) { self.due = Utc::now() + self.duration; } + /// Resets the timer to expire 1 `duration` later than it was **previously set to expire**. + /// Does not depend on the actual current time. + fn increment(&mut self) { + self.due += self.duration + } + + /// Resets timer to expire 1 `duration` from the **current instant**. + /// This has the same effect as constructing a new `Timer` with the same `duration`. + pub fn reset(&mut self) { + self.due = Instant::now() + self.duration; + } }