From e79fa9875d2122e607fb1150dafa99d703ed11b8 Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Mon, 25 Nov 2024 13:05:35 +0000 Subject: Transform categories --- rust/schist_models/src/date_utc.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'rust/schist_models/src') diff --git a/rust/schist_models/src/date_utc.rs b/rust/schist_models/src/date_utc.rs index c801585..d3a1d2e 100644 --- a/rust/schist_models/src/date_utc.rs +++ b/rust/schist_models/src/date_utc.rs @@ -69,6 +69,15 @@ impl DateUtc { Ok(Self { chrono_datetime_utc }) } + pub fn sub_days(&self, days: u64) -> Result { + let chrono_datetime_utc = self + .chrono_datetime_utc + .checked_sub_days(chrono::Days::new(days)) + .with_context(|| format!("failed to subtract {} days from {}", days, self)) + .unwrap(); + Ok(Self { chrono_datetime_utc }) + } + fn is_day_str(str: &str) -> bool { if str.len() != 10 { return false; @@ -151,9 +160,14 @@ impl Dateable for DateUtc { impl From for DateUtc { fn from(datetime_utc: DatetimeUtc) -> Self { + Self::from(&datetime_utc) + } +} + +impl From<&DatetimeUtc> for DateUtc { + fn from(datetime_utc: &DatetimeUtc) -> Self { Self { chrono_datetime_utc: datetime_utc - .borrow() .to_string() .parse::>().unwrap() .with_time(NaiveTime::from_hms_opt(0, 0, 0).unwrap()).unwrap(), -- cgit v1.2.3