From 17bf5c0564cae4d1aabf6c4d8c5d18892c9e57e4 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Wed, 6 Mar 2024 12:47:10 +0100 Subject: [PATCH] fix: parse dates as ISO --- src/sync_csv.rs | 8 ++++---- src/utils.rs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/sync_csv.rs b/src/sync_csv.rs index 5cdec2c..6232771 100644 --- a/src/sync_csv.rs +++ b/src/sync_csv.rs @@ -4,7 +4,7 @@ use crate::{ }; use anyhow::Result; -use crate::utils::{normalize_str, parse_datetime_american, parse_normalize_phone}; +use crate::utils::{normalize_str, parse_date_iso, parse_normalize_phone}; use crate::sync_paheko::{GeneralizedAnswer, sync_paheko}; use email_address::EmailAddress; use chrono::prelude::Datelike; @@ -42,7 +42,7 @@ pub async fn sync_csv( // raw row record directly from CSV #[derive(Debug, serde::Deserialize)] struct AnswerRecord { - #[serde(rename = "Date (MM/JJ/AAAA)")] + #[serde(rename = "Date")] date: String, #[serde(rename = "Email acheteur")] email: String, @@ -122,11 +122,11 @@ pub async fn sync_csv( country: "fr".to_string(), job: process_csv_value(parsed_record.job), birth_year: process_csv_value(parsed_record.birth_date) - .and_then(|raw_date| parse_datetime_american(&raw_date)) + .and_then(|raw_date| parse_date_iso(&raw_date)) .map(|d| d.year() as u32), inception_time: process_csv_value(parsed_record.date) .map(|s| - parse_datetime_american(&s).expect("Record must have a valid date") + parse_date_iso(&s).expect("Record must have a valid date") ) .expect("Record must have a date"), reference: format!("BP/{}", process_csv_value(parsed_record.reference).expect("Row must have reference")), // BP as Bulletin Papier diff --git a/src/utils.rs b/src/utils.rs index 9651adc..9caf90e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -23,6 +23,14 @@ pub fn generate_id() -> Id { } +pub fn parse_date_iso(inp: &str) -> Option> { + let date = NaiveDate::parse_from_str(inp, "%Y-%m-%d").ok()?; + Some(DateTime::::from_naive_utc_and_offset( + date.and_hms_opt(0, 0, 0).unwrap(), + Utc + )) +} + pub fn parse_datetime_american(inp: &str) -> Option> { let date = NaiveDate::parse_from_str(inp, "%m/%d/%Y").ok()?; Some(DateTime::::from_naive_utc_and_offset( @@ -31,6 +39,14 @@ pub fn parse_datetime_american(inp: &str) -> Option> { )) } +pub fn parse_datetime_french(inp: &str) -> Option> { + let date = NaiveDate::parse_from_str(inp, "%d/%m/%Y").ok()?; + Some(DateTime::::from_naive_utc_and_offset( + date.and_hms_opt(0, 0, 0).unwrap(), + Utc + )) +} + pub fn parse_normalize_phone(inp: String) -> Option { let parsed = match phonenumber::parse( Some(phonenumber::country::Id::FR), inp