use crate::GregorianDate;
use crate::GregorianDateError;
use crate::ShahanshahiDate;
use crate::ShahanshahiDateError;
use ::time::{Date, Month};
impl GregorianDate {
pub fn try_from_time_date(date: Date) -> Result<Self, GregorianDateError> {
GregorianDate::try_new(date.year(), date.month() as u8, date.day())
}
pub fn to_time_date(self) -> Date {
let month = Month::try_from(self.month()).expect("GregorianDate month is always 1..=12");
Date::from_calendar_date(self.year(), month, self.day())
.expect("GregorianDate always maps to a valid time::Date")
}
}
impl ShahanshahiDate {
pub fn try_from_time_date(date: Date) -> Result<Self, ShahanshahiDateError> {
let g = GregorianDate::try_from_time_date(date)?;
Self::try_from_gregorian(g)
}
pub fn to_time_date(self) -> Date {
self.to_gregorian().to_time_date()
}
#[cfg(feature = "proleptic")]
pub fn try_from_time_date_proleptic(date: Date) -> Result<Self, ShahanshahiDateError> {
let g = GregorianDate::try_from_time_date(date)?;
Self::try_from_gregorian_proleptic(g)
}
}