aboutsummaryrefslogtreecommitdiff
path: root/src/internal/timer.rs
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-06-04 20:32:25 -0700
committerZeyla Hellyer <[email protected]>2017-06-06 10:12:13 -0700
commit990e611a56f37f64fbce74fbc487c7dcc4aa4e28 (patch)
tree399c69583bd256d31668affc227f235386814552 /src/internal/timer.rs
parentAdd CurrentUser::face(), User::face() (diff)
downloadserenity-990e611a56f37f64fbce74fbc487c7dcc4aa4e28.tar.xz
serenity-990e611a56f37f64fbce74fbc487c7dcc4aa4e28.zip
Use chrono for struct timestamp fields
Chrono is easier to use than timestamped strings, so they should be automatically deserialized and available for the user, instead of having the user deserialize the strings themselves. These fields have been changed to use a type of `DateTime<FixedOffset>`: - `ChannelPinsUpdateEvent.last_pin_timestamp` - `Group.last_pin_timestamp` - `Guild.joined_at` - `GuildChannel.last_pin_timestamp` - `Invite.created_at` - `Member.joined_at` - `Message.edited_timestamp - `Message.timestamp` - `MessageUpdateEvent.edited_timestamp` - `MessageUpdateEvent.timestamp` - `PrivateChannel.last_pin_timestamp` `Member.joined_at` is now also an `Option`. Previously, if a Guild Member Update was received for a member not in the cache, a new Member would be instantiated with a default String value. This is incorrect behaviour, and has now been replaced with being set to `None` in that case. Id methods' `created_at()` method now return a `chrono::NaiveDateTime` instead of a `time::Timespec`, and `User::created_at` has been updated to reflect that. Additionally, drop `time` as a direct dependency and use chrono for internals.
Diffstat (limited to 'src/internal/timer.rs')
-rw-r--r--src/internal/timer.rs25
1 files changed, 16 insertions, 9 deletions
diff --git a/src/internal/timer.rs b/src/internal/timer.rs
index cc846b3..677ece1 100644
--- a/src/internal/timer.rs
+++ b/src/internal/timer.rs
@@ -1,9 +1,9 @@
+use chrono::{DateTime, Duration, UTC};
use std::thread;
use std::time::Duration as StdDuration;
-use time::{self, Duration, Timespec};
pub struct Timer {
- due: Timespec,
+ due: DateTime<UTC>,
duration: Duration,
}
@@ -12,25 +12,32 @@ impl Timer {
let duration = Duration::milliseconds(duration_in_ms as i64);
Timer {
- due: time::get_time() + duration,
+ due: UTC::now() + duration,
duration: duration,
}
}
pub fn await(&mut self) {
- let diff = self.due - time::get_time();
+ let due_time = (self.due.timestamp() * 1000) + self.due.timestamp_subsec_millis() as i64;
+ let now_time = {
+ let now = UTC::now();
- if diff > time::Duration::zero() {
- let amount = diff.num_milliseconds() as u64;
+ (now.timestamp() * 1000) + now.timestamp_subsec_millis() as i64
+ };
- thread::sleep(StdDuration::from_millis(amount));
+ if due_time > now_time {
+ let sleep_time = due_time - now_time;
+
+ if sleep_time > 0 {
+ thread::sleep(StdDuration::from_millis(sleep_time as u64));
+ }
}
self.due = self.due + self.duration;
}
pub fn check(&mut self) -> bool {
- if time::get_time() >= self.due {
+ if UTC::now() >= self.due {
self.due = self.due + self.duration;
true
@@ -40,6 +47,6 @@ impl Timer {
}
pub fn reset(&mut self) {
- self.due = time::get_time() + self.duration;
+ self.due = UTC::now() + self.duration;
}
}