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)