#[cfg(feature = "jiff")]
mod instant;
mod parse;
mod range;
use core::fmt;
#[cfg(feature = "jiff")]
use jiff::Timestamp;
use crate::escape::{EscapeError, unescape};
use crate::prefix::{Prefix, PrefixError};
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum DateError {
#[error(transparent)]
Escape(#[from] EscapeError),
#[error(transparent)]
Prefix(#[from] PrefixError),
#[error(
"the value is not of the form yyyy, yyyy-mm, yyyy-mm-dd or yyyy-mm-ddThh:mm[:ss[.f]][Z|±hh:mm]"
)]
Form,
#[error("the value names a day the calendar does not have")]
Calendar,
#[error("the value names a leap second")]
LeapSecond,
#[error("the value is outside the representable instants")]
Unrepresentable,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DateValue {
prefix: Prefix,
date: SearchDate,
}
impl DateValue {
pub fn parse(text: &str) -> Result<Self, DateError> {
let plain = unescape(text)?;
let (prefix, rest) = Prefix::split(&plain)?;
Ok(Self {
prefix,
date: SearchDate::parse(rest)?,
})
}
#[must_use]
pub fn prefix(&self) -> Prefix {
self.prefix
}
#[must_use]
pub fn date(&self) -> &SearchDate {
&self.date
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Range {
low: SearchDate,
high: Option<SearchDate>,
}
impl Range {
#[must_use]
pub fn low(&self) -> &SearchDate {
&self.low
}
#[must_use]
pub fn high(&self) -> Option<&SearchDate> {
self.high.as_ref()
}
}
#[cfg(feature = "jiff")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Instants {
from: Timestamp,
until: Option<Timestamp>,
}
#[cfg(feature = "jiff")]
impl Instants {
#[must_use]
pub fn from(self) -> Timestamp {
self.from
}
#[must_use]
pub fn until(self) -> Option<Timestamp> {
self.until
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Precision {
Year,
Month,
Day,
Minute,
Second,
Fraction(usize),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Offset {
Utc,
Plus {
hours: u8,
minutes: u8,
},
Minus {
hours: u8,
minutes: u8,
},
}
impl Offset {
#[must_use]
pub fn seconds(self) -> i32 {
match self {
Self::Utc => 0,
Self::Plus { hours, minutes } => i32::from(hours) * 3600 + i32::from(minutes) * 60,
Self::Minus { hours, minutes } => -(i32::from(hours) * 3600 + i32::from(minutes) * 60),
}
}
}
impl fmt::Display for Offset {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Utc => f.write_str("Z"),
Self::Plus { hours, minutes } => write!(f, "+{hours:02}:{minutes:02}"),
Self::Minus { hours, minutes } => write!(f, "-{hours:02}:{minutes:02}"),
}
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SearchDate {
year: i16,
month: Option<i8>,
day: Option<i8>,
time: Option<Clock>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Clock {
hour: i8,
minute: i8,
second: Option<Second>,
offset: Option<Offset>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Second {
whole: i8,
fraction: Option<String>,
}
impl SearchDate {
#[must_use]
pub fn precision(&self) -> Precision {
match (&self.month, &self.day, &self.time) {
(None, _, _) => Precision::Year,
(Some(_), None, _) => Precision::Month,
(Some(_), Some(_), None) => Precision::Day,
(Some(_), Some(_), Some(clock)) => match &clock.second {
None => Precision::Minute,
Some(Second { fraction: None, .. }) => Precision::Second,
Some(Second {
fraction: Some(digits),
..
}) => Precision::Fraction(digits.len()),
},
}
}
#[cfg(feature = "jiff")]
#[must_use]
#[expect(
clippy::expect_used,
reason = "parse admits years 0001 to 9999 and checks the day against the Gregorian calendar, and next() steps within it, all of which jiff's Date holds"
)]
pub fn first_day(&self) -> jiff::civil::Date {
jiff::civil::Date::new(self.year, self.month.unwrap_or(1), self.day.unwrap_or(1))
.expect("a date read by parse or stepped by next should be a calendar date")
}
#[must_use]
pub fn offset(&self) -> Option<Offset> {
self.time.as_ref().and_then(|clock| clock.offset)
}
}
impl fmt::Display for SearchDate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:04}", self.year)?;
if let Some(month) = self.month {
write!(f, "-{month:02}")?;
}
if let Some(day) = self.day {
write!(f, "-{day:02}")?;
}
if let Some(clock) = &self.time {
write!(f, "T{:02}:{:02}", clock.hour, clock.minute)?;
if let Some(second) = &clock.second {
write!(f, ":{:02}", second.whole)?;
if let Some(digits) = &second.fraction {
write!(f, ".{digits}")?;
}
}
if let Some(offset) = clock.offset {
write!(f, "{offset}")?;
}
}
Ok(())
}
}
impl fmt::Debug for SearchDate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SearchDate")
.field("precision", &self.precision())
.field("offset", &self.offset())
.finish_non_exhaustive()
}
}