use crate::r4::types::{Date, DateTime, Instant, Time};
pub use crate::temporal::{DateParts, DatePrecision, TimeParts};
impl Date {
#[must_use]
pub fn parse_parts(&self) -> Option<DateParts> {
DateParts::parse(&self.0)
}
}
impl DateTime {
#[must_use]
pub fn date_parts(&self) -> Option<DateParts> {
let date = self.0.split(['T', 't']).next().unwrap_or(&self.0);
DateParts::parse(date)
}
}
impl Instant {
#[must_use]
pub fn date_parts(&self) -> Option<DateParts> {
let date = self.0.split(['T', 't']).next().unwrap_or(&self.0);
DateParts::parse(date)
}
}
impl Time {
#[must_use]
pub fn parse_parts(&self) -> Option<TimeParts> {
TimeParts::parse(&self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_date_precisions() {
assert_eq!(
Date("2024".into()).parse_parts().unwrap(),
DateParts { year: 2024, month: None, day: None }
);
assert_eq!(
Date("2024-03-25".into()).parse_parts().unwrap().precision(),
DatePrecision::Day
);
}
#[test]
fn datetime_and_instant_date_part() {
let dt = DateTime("2015-02-07T13:28:17-05:00".into());
assert_eq!(dt.date_parts().unwrap().precision(), DatePrecision::Day);
let inst = Instant("2015-02-07T13:28:17.239+02:00".into());
assert_eq!(inst.date_parts().unwrap().day, Some(7));
}
#[test]
fn parse_time() {
let t = Time("13:28:17".into()).parse_parts().unwrap();
assert_eq!((t.hour, t.minute, t.second), (13, 28, 17));
assert!(Time("25:00:00".into()).parse_parts().is_none());
}
}