-
Notifications
You must be signed in to change notification settings - Fork 596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove some chrono #933
Remove some chrono #933
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,51 +1,45 @@ | ||||||
use chrono::{DateTime, Duration, Utc}; | ||||||
use std::time::Duration as StdDuration; | ||||||
use tokio::time::delay_for; | ||||||
use tokio::time::{delay_until, Duration, Instant}; | ||||||
|
||||||
#[derive(Debug)] | ||||||
pub struct Timer { | ||||||
due: DateTime<Utc>, | ||||||
due: Instant, | ||||||
duration: Duration, | ||||||
} | ||||||
|
||||||
impl Timer { | ||||||
/// construct timer, initially set and due `duration_in_ms` in the future | ||||||
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, | ||||||
} | ||||||
} | ||||||
|
||||||
/// block until next due time resetting afterwards | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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 and resets the timer if due | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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; } | ||||||
/// reset timer to be 1 duration after previous due time | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
fn increment(&mut self) { | ||||||
self.due += self.duration | ||||||
} | ||||||
|
||||||
/// reset timer to be 1 duration from **now** | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
pub fn reset(&mut self) { | ||||||
self.due = Instant::now() + self.duration; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc-strings should be written as proper sentences. They should start with an uppercase letter, and end with a full stop.