working on time parser

This commit is contained in:
jude 2020-08-30 21:08:08 +01:00
parent d097f36313
commit c1a4092e3c
3 changed files with 30 additions and 17 deletions

View File

@ -149,7 +149,7 @@ async fn pause(ctx: &Context, msg: &Message, args: String) -> CommandResult {
let pool = ctx.data.read().await
.get::<SQLPool>().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;

View File

@ -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 {

View File

@ -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<i32, Box<dyn std::error::Error>> {
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::<u32>().unwrap();
seconds = current_buffer.parse::<i32>().unwrap();
current_buffer = String::from("0");
},
'm' => {
minutes = current_buffer.parse::<u32>().unwrap();
minutes = current_buffer.parse::<i32>().unwrap();
current_buffer = String::from("0");
},
'h' => {
hours = current_buffer.parse::<u32>().unwrap();
hours = current_buffer.parse::<i32>().unwrap();
current_buffer = String::from("0");
},
'd' => {
days = current_buffer.parse::<u32>().unwrap();
days = current_buffer.parse::<i32>().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::<u32>() *
let full = seconds + (minutes * 60) + (hours * 3600) + (days * 86400) + current_buffer.parse::<i32>().unwrap() *
if self.inverted { -1 } else { 1 };
full