Skip to content

Commit

Permalink
Merge pull request #119 from arichtman/naivedate-conversion
Browse files Browse the repository at this point in the history
feat: add type conversion DatePerhapsTime <-> NaiveDate
  • Loading branch information
hoodie authored Nov 30, 2024
2 parents 1aa79db + 7acd8ff commit 5a3d8b5
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 5a3d8b5

Please sign in to comment.