Skip to content
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

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 20 additions & 26 deletions src/internal/timer.rs
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
Copy link
Member

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.

Suggested change
/// construct timer, initially set and due `duration_in_ms` in the future
/// Construct a `Timer`, which is initially set to expire in due `duration_in_ms` time 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,
}
}

/// block until next due time resetting afterwards
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// block until next due time resetting afterwards
/// Block until the due time. The timer will be reset afterwards.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// returns true and resets the timer if due
/// Returns a boolean indicating whether the timer has expired. the timer will also be reset if it has.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// reset timer to be 1 duration after previous due time
/// Resets the timer by incrementing the due time using the duration that was passed upon construction of the timer.

fn increment(&mut self) {
self.due += self.duration
}

/// reset timer to be 1 duration from **now**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// reset timer to be 1 duration from **now**
/// Resets the timer by restarting from the current instant and setting its due time to the duration that was passed upon construction. This has the same effect as constructing a new `Timer`.

pub fn reset(&mut self) {
self.due = Instant::now() + self.duration;
}
}