Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add type conversion DatePerhapsTime <-> NaiveDate #119

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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