Skip to content

Commit

Permalink
feat: add method for getting NaiveDate from DatePerhapsTime
Browse files Browse the repository at this point in the history
  • Loading branch information
arichtman committed Nov 17, 2024
1 parent 1aa79db commit 7acd8ff
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/components/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -281,6 +291,17 @@ pub fn with_timezone<T: chrono::TimeZone + chrono_tz::OffsetName>(
.into()
}

impl From<DatePerhapsTime> 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<CalendarDateTime> for DatePerhapsTime {
fn from(dt: CalendarDateTime) -> Self {
Self::DateTime(dt)
Expand Down

0 comments on commit 7acd8ff

Please sign in to comment.