summaryrefslogtreecommitdiff
path: root/rust/schist_models/src/date_utc.rs
diff options
context:
space:
mode:
authorJoe Carstairs <me@joeac.net>2024-11-25 13:05:35 +0000
committerJoe Carstairs <me@joeac.net>2024-11-25 13:05:35 +0000
commite79fa9875d2122e607fb1150dafa99d703ed11b8 (patch)
tree2417a8b6cfa9b0ea284859b9d9b70f082520375f /rust/schist_models/src/date_utc.rs
parent5bd223ee9c6853502d509627678f696c6ea5fcdc (diff)
Transform categories
Diffstat (limited to 'rust/schist_models/src/date_utc.rs')
-rw-r--r--rust/schist_models/src/date_utc.rs16
1 files changed, 15 insertions, 1 deletions
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<Self> {
+ 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<DatetimeUtc> 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::<DateTime<Utc>>().unwrap()
.with_time(NaiveTime::from_hms_opt(0, 0, 0).unwrap()).unwrap(),