From c1a4092e3ce97ed1f7342042a04d6cec32446bc5 Mon Sep 17 00:00:00 2001 From: jude Date: Sun, 30 Aug 2020 21:08:08 +0100 Subject: [PATCH] working on time parser --- src/commands/moderation_cmds.rs | 2 +- src/models.rs | 9 +++++++++ src/time_parser.rs | 36 ++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/commands/moderation_cmds.rs b/src/commands/moderation_cmds.rs index 1bdc745..955677c 100644 --- a/src/commands/moderation_cmds.rs +++ b/src/commands/moderation_cmds.rs @@ -149,7 +149,7 @@ async fn pause(ctx: &Context, msg: &Message, args: String) -> CommandResult { let pool = ctx.data.read().await .get::().cloned().expect("Could not get SQLPool from data"); - let channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).await.unwrap(), + let channel = ChannelData::from_channel(msg.channel(&ctx).await.unwrap(), pool.clone()).await.unwrap(); channel.commit_changes(pool).await; diff --git a/src/models.rs b/src/models.rs index a74e6a2..6e8a14e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -47,6 +47,15 @@ SELECT id, guild, name, prefix FROM guilds WHERE guild = ? .await?) } } + + pub async fn commit_changes(&self, pool: MySqlPool) { + sqlx::query!( + " +UPDATE guilds SET name = ?, prefix = ? WHERE id = ? + ", self.name, self.prefix, self.id) + .execute(&pool) + .await.unwrap(); + } } pub struct ChannelData { diff --git a/src/time_parser.rs b/src/time_parser.rs index 6ca16a5..db59949 100644 --- a/src/time_parser.rs +++ b/src/time_parser.rs @@ -4,6 +4,8 @@ use std::time::{ }; use chrono_tz::Tz; +use chrono::offset::Utc; +use chrono::{Timelike, Datelike, TimeZone}; enum ParseType { Explicit, @@ -42,17 +44,18 @@ impl TimeParser { } pub fn timestamp(&self) -> i32 { - + 0 } pub fn displacement(&self) -> i32 { - + 0 } fn process(&self) -> i32 { match self.parse_type { ParseType::Explicit => { - self.process_explicit() + // TODO remove unwrap from here + self.process_explicit().unwrap() }, ParseType::Displacement => { @@ -61,50 +64,51 @@ impl TimeParser { .duration_since(UNIX_EPOCH) .expect("Time calculated as going backwards. Very bad"); - since_epoch.as_secs() + self.process_displacement() + since_epoch.as_secs() as i32 + self.process_displacement() }, } } - fn process_explicit(&self) -> i32 { + fn process_explicit(&self) -> Result> { + let dt = self.timezone.datetime_from_str(self.time_string.as_str(), "%d/%m/%Y-%H:%M:%S")?; - 0 + Ok(dt.timestamp() as i32) } fn process_displacement(&self) -> i32 { let mut current_buffer = "0".to_string(); - let mut seconds = 0; - let mut minutes = 0; - let mut hours = 0; - let mut days = 0; + let mut seconds = 0 as i32; + let mut minutes = 0 as i32; + let mut hours = 0 as i32; + let mut days = 0 as i32; for character in self.time_string.chars() { match character { 's' => { - seconds = current_buffer.parse::().unwrap(); + seconds = current_buffer.parse::().unwrap(); current_buffer = String::from("0"); }, 'm' => { - minutes = current_buffer.parse::().unwrap(); + minutes = current_buffer.parse::().unwrap(); current_buffer = String::from("0"); }, 'h' => { - hours = current_buffer.parse::().unwrap(); + hours = current_buffer.parse::().unwrap(); current_buffer = String::from("0"); }, 'd' => { - days = current_buffer.parse::().unwrap(); + days = current_buffer.parse::().unwrap(); current_buffer = String::from("0"); }, c => { if c.is_digit(10) { - current_buffer += c.as_str(); + current_buffer += &c.to_string(); } else { // raise exception @@ -113,7 +117,7 @@ impl TimeParser { } } - let full = seconds + (minutes * 60) + (hours * 3600) + (days * 86400) + current_buffer.parse::() * + let full = seconds + (minutes * 60) + (hours * 3600) + (days * 86400) + current_buffer.parse::().unwrap() * if self.inverted { -1 } else { 1 }; full