From 7acd8ff26d94690927a0c23910762f6bc1f4249b Mon Sep 17 00:00:00 2001 From: Ariel Richtman <10679234+arichtman@users.noreply.github.com> Date: Fri, 8 Nov 2024 09:53:03 +1000 Subject: [PATCH] feat: add method for getting NaiveDate from DatePerhapsTime Refs: #118 --- src/components/date_time.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/components/date_time.rs b/src/components/date_time.rs index 0ecb569..04defb2 100644 --- a/src/components/date_time.rs +++ b/src/components/date_time.rs @@ -266,6 +266,16 @@ impl DatePerhapsTime { Self::Date(date) => naive_date_to_property(*date, key), } } + /// Discards time, assumes UTC, and returns an owned instance of a pure date + pub fn date_naive(&self) -> NaiveDate { + use crate::DatePerhapsTime::*; + match self { + Date(date) => date.to_owned(), + DateTime(CalendarDateTime::Floating(date_time)) => date_time.date(), + DateTime(CalendarDateTime::Utc(date_time)) => date_time.date_naive(), + DateTime(CalendarDateTime::WithTimezone { date_time, tzid: _ }) => date_time.date(), + } + } } /// TODO: make public or delete @@ -281,6 +291,17 @@ pub fn with_timezone( .into() } +impl From for NaiveDate { + fn from(dt: DatePerhapsTime) -> Self { + match dt { + DatePerhapsTime::Date(date) => date, + DatePerhapsTime::DateTime(CalendarDateTime::Floating(date_time)) => date_time.date(), + DatePerhapsTime::DateTime(CalendarDateTime::Utc(date_time)) => date_time.date_naive(), + DatePerhapsTime::DateTime(CalendarDateTime::WithTimezone { date_time, tzid: _ }) => date_time.date(), + } + } +} + impl From for DatePerhapsTime { fn from(dt: CalendarDateTime) -> Self { Self::DateTime(dt)