Use timezones wherever possible.

Replace uses of NaiveDateTime with DateTime<Utc>. Use timezones in postman to update days correctly. Use chrono::Months to update months rather than using MySQL query.
This commit is contained in:
jude
2022-11-22 20:41:07 +00:00
parent 08e4c6cb57
commit aa74a7f9a3
7 changed files with 92 additions and 120 deletions

View File

@ -1,10 +1,6 @@
use std::{
collections::HashSet,
string::ToString,
time::{SystemTime, UNIX_EPOCH},
};
use std::{collections::HashSet, string::ToString};
use chrono::NaiveDateTime;
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono_tz::Tz;
use num_integer::Integer;
use poise::{
@ -60,18 +56,27 @@ pub async fn pause(
let parsed = natural_parser(&until, &timezone.to_string()).await;
if let Some(timestamp) = parsed {
let dt = NaiveDateTime::from_timestamp(timestamp, 0);
match NaiveDateTime::from_timestamp_opt(timestamp, 0) {
Some(dt) => {
channel.paused = true;
channel.paused_until = Some(dt);
channel.paused = true;
channel.paused_until = Some(dt);
channel.commit_changes(&ctx.data().database).await;
channel.commit_changes(&ctx.data().database).await;
ctx.say(format!(
"Reminders in this channel have been silenced until **<t:{}:D>**",
timestamp
))
.await?;
}
ctx.say(format!(
"Reminders in this channel have been silenced until **<t:{}:D>**",
timestamp
))
.await?;
None => {
ctx.say(format!(
"Time processed could not be interpreted as `DateTime`. Please write the time as clearly as possible",
))
.await?;
}
}
} else {
ctx.say(
"Time could not be processed. Please write the time as clearly as possible",
@ -432,11 +437,8 @@ pub fn show_delete_page(reminders: &[Reminder], page: usize, timezone: Tz) -> Cr
reply
}
fn time_difference(start_time: NaiveDateTime) -> String {
let unix_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64;
let now = NaiveDateTime::from_timestamp(unix_time, 0);
let delta = (now - start_time).num_seconds();
fn time_difference(start_time: DateTime<Utc>) -> String {
let delta = (Utc::now() - start_time).num_seconds();
let (minutes, seconds) = delta.div_rem(&60);
let (hours, minutes) = minutes.div_rem(&60);

View File

@ -175,17 +175,15 @@ impl<'a> MultiReminderBuilder<'a> {
}
pub fn time<T: Into<i64>>(mut self, time: T) -> Self {
self.utc_time = NaiveDateTime::from_timestamp(time.into(), 0);
if let Some(utc_time) = NaiveDateTime::from_timestamp_opt(time.into(), 0) {
self.utc_time = utc_time;
}
self
}
pub fn expires<T: Into<i64>>(mut self, time: Option<T>) -> Self {
if let Some(t) = time {
self.expires = Some(NaiveDateTime::from_timestamp(t.into(), 0));
} else {
self.expires = None;
}
self.expires = time.map(|t| NaiveDateTime::from_timestamp_opt(t.into(), 0)).flatten();
self
}

View File

@ -6,7 +6,7 @@ pub mod look_flags;
use std::hash::{Hash, Hasher};
use chrono::{NaiveDateTime, TimeZone};
use chrono::{DateTime, NaiveDateTime, Utc};
use chrono_tz::Tz;
use poise::serenity_prelude::{
model::id::{ChannelId, GuildId, UserId},
@ -24,7 +24,7 @@ pub struct Reminder {
pub id: u32,
pub uid: String,
pub channel: u64,
pub utc_time: NaiveDateTime,
pub utc_time: DateTime<Utc>,
pub interval_seconds: Option<u32>,
pub interval_months: Option<u32>,
pub expires: Option<NaiveDateTime>,
@ -310,16 +310,15 @@ WHERE
count + 1,
self.display_content(),
self.channel,
timezone.timestamp(self.utc_time.timestamp(), 0).format("%Y-%m-%d %H:%M:%S")
self.utc_time.with_timezone(timezone).format("%Y-%m-%d %H:%M:%S")
)
}
pub fn display(&self, flags: &LookFlags, timezone: &Tz) -> String {
let time_display = match flags.time_display {
TimeDisplayType::Absolute => timezone
.timestamp(self.utc_time.timestamp(), 0)
.format("%Y-%m-%d %H:%M:%S")
.to_string(),
TimeDisplayType::Absolute => {
self.utc_time.with_timezone(timezone).format("%Y-%m-%d %H:%M:%S").to_string()
}
TimeDisplayType::Relative => format!("<t:{}:R>", self.utc_time.timestamp()),
};

View File

@ -1,9 +1,9 @@
use chrono::NaiveDateTime;
use chrono::{DateTime, Utc};
use sqlx::MySqlPool;
pub struct Timer {
pub name: String,
pub start_time: NaiveDateTime,
pub start_time: DateTime<Utc>,
pub owner: u64,
}