From 56cf021f4a1c2584074c2bd9abef0308ce5003e6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 1 Jun 2020 09:38:00 +0200 Subject: [PATCH] Parse mysql dates --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/helpers/database.rs | 17 +++++++++++++++++ src/helpers/user_helper.rs | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9dd1ce9..f0470c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -509,6 +509,7 @@ version = "0.1.0" dependencies = [ "actix-rt", "actix-web", + "chrono", "encoding_rs", "futures", "mailchecker", diff --git a/Cargo.toml b/Cargo.toml index c31279f..9d83c5b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ encoding_rs = "0.8.23" percent-encoding = "2.1.0" mailchecker = "3.3.6" sha1 = "0.6.0" -rand = "0.7.3" \ No newline at end of file +rand = "0.7.3" +chrono = "0.4.11" \ No newline at end of file diff --git a/src/helpers/database.rs b/src/helpers/database.rs index dcb296c..16c50dd 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -9,6 +9,7 @@ use mysql::prelude::Queryable; use crate::data::config::DatabaseConfig; use crate::data::error::{ExecError, ResultBoxError}; use std::collections::HashMap; +use chrono::{Utc, TimeZone}; /// Database access helper /// @@ -213,6 +214,22 @@ impl<'a> RowResult<'a> { pub fn get_legacy_bool(&self, name: &str) -> ResultBoxError { Ok(self.get_int64(name)? == 1) } + + /// Get a MYSQL date as a timestamp + pub fn get_date_as_time(&self, name: &str) -> ResultBoxError { + let value = self.row.get_opt(self.find_col(name)?); + let value : Value = value.ok_or(ExecError(format!("Could not find date field {} !", name)))??; + + // Check if it is a date + if let Value::Date(year, month, day, hour, minutes, seconds, mic_secs) = value { + let dt = Utc.ymd(year as i32, month as u32, day as u32) + .and_hms_micro(hour as u32, minutes as u32, seconds as u32, mic_secs); + + return Ok(dt.timestamp() as u64); + } + + Err(ExecError::boxed_string(format!("Field {} could not be resolved as a date !", name))) + } } diff --git a/src/helpers/user_helper.rs b/src/helpers/user_helper.rs index 7c793df..f492c35 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -57,7 +57,7 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError { public_note: res.get_optional_str("public_note")?, block_comments_on_his_page: res.get_legacy_bool("bloquecommentaire")?, allow_posts_from_friends: res.get_legacy_bool("autoriser_post_amis")?, - account_creation_time: 10,//TODO : parse date + account_creation_time: res.get_date_as_time("date_creation")? }) }) }