use time::{Month, PrimitiveDateTime, UtcOffset, Weekday}; use crate::error::TryFromPartial; use crate::partial::{PartDate, PartOffsetDateTime, PartTime, Partial}; /// An [`PartPrimitiveDateTime`] struct represents an incomplete [`PrimitiveDateTime`] struct. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub struct PartPrimitiveDateTime { date: PartDate, time: PartTime, } impl PartPrimitiveDateTime { /// Create a new [`PartPrimitiveDateTime`] from the provided [`PartDate`] and [`PartTime`]. pub fn new(date: PartDate, time: PartTime) -> Self { Self { date, time } } } impl PartPrimitiveDateTime { /// Returns the date. pub fn date(self) -> PartDate { self.date } /// Returns the year. pub fn year(self) -> Option { self.date.year() } /// Returns the month. pub fn month(self) -> Option { self.date.month() } /// Returns the weekday. pub fn weekday(self) -> Option { self.date.weekday() } /// Returns the day of the month. pub fn day(self) -> Option { self.date.day() } /// Returns the time. pub fn time(self) -> PartTime { self.time } /// Returns the clock hour. pub fn hour(self) -> Option { self.time.hour() } /// Returns the minute within the hour. pub fn minute(self) -> Option { self.time.minute() } /// Returns the second within the minute. pub fn second(self) -> Option { self.time.second() } /// Returns the milliseconds within the second. pub fn millisecond(self) -> Option { self.time.millisecond() } /// Returns the microseconds within the second. pub fn microsecond(self) -> Option { self.time.microsecond() } /// Returns the nanoseconds within the second. pub fn nanosecond(self) -> Option { self.time.nanosecond() } } impl PartPrimitiveDateTime { /// Replaces the date. pub fn replace_date(self, date: PartDate) -> Result { Ok(date.with_time(self.time)) } /// Replaces the year. pub fn replace_year(self, year: Option) -> Result { self.replace_date(self.date.replace_year(year)?) } /// Replaces the month of the year. pub fn replace_month(self, month: Option) -> Result { self.replace_date(self.date.replace_month(month)?) } /// Replaces the day of the month. pub fn replace_day(self, day: Option) -> Result { self.replace_date(self.date.replace_day(day)?) } /// Returns the time. pub fn replace_time(self, time: PartTime) -> Result { Ok(self.date.with_time(time)) } /// Returns the clock hour. pub fn replace_hour(self, hour: Option) -> Result { self.replace_time(self.time.replace_hour(hour)?) } /// Returns the minute within the hour. pub fn replace_minute(self, minute: Option) -> Result { self.replace_time(self.time.replace_minute(minute)?) } /// Returns the second within the minute. pub fn replace_second(self, second: Option) -> Result { self.replace_time(self.time.replace_second(second)?) } /// Returns the milliseconds within the second. pub fn replace_millisecond(self, millisecond: Option) -> Result { self.replace_time(self.time.replace_millisecond(millisecond)?) } /// Returns the microseconds within the second. pub fn replace_microsecond(self, microsecond: Option) -> Result { self.replace_time(self.time.replace_microsecond(microsecond)?) } /// Returns the nanoseconds within the second. pub fn replace_nanosecond(self, nanosecond: Option) -> Result { self.replace_time(self.time.replace_nanosecond(nanosecond)?) } } impl PartPrimitiveDateTime { /// Assuming that the existing `InPrimitiveDateTime` represents a moment /// in the provided `UtcOffset`, returns an `InOffsetDateTime`. pub fn assume_offset(self, offset: Option) -> PartOffsetDateTime { PartOffsetDateTime::new(self, offset) } /// Assuming that the existing `InPrimitiveDateTime` represents a moment /// in UTC, returns an `InOffsetDateTime`. pub fn assume_utc(self) -> PartOffsetDateTime { self.assume_offset(Some(UtcOffset::UTC)) } } impl Partial for PartPrimitiveDateTime { type Complete = PrimitiveDateTime; fn from_complete(complete: Self::Complete) -> Self { let d = PartDate::from_complete(complete.date()); let t = PartTime::from_complete(complete.time()); Self::new(d, t) } fn into_complete(self) -> Result { let d = self.date.into_complete()?; let t = self.time.into_complete()?; Ok(Self::Complete::new(d, t)) } fn with_fallback(self, fallback: Self::Complete) -> Result { let d = self.date.with_fallback(fallback.date())?; let t = self.time.with_fallback(fallback.time())?; Ok(Self::new(d, t)) } } impl From for PartPrimitiveDateTime { fn from(datetime: PrimitiveDateTime) -> Self { Self::from_complete(datetime) } } impl TryFrom for PrimitiveDateTime { type Error = TryFromPartial; fn try_from(datetime: PartPrimitiveDateTime) -> Result { datetime.into_complete() } }