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

@ -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,
}