mod common;
mod error;
pub mod hour_cycle;
mod item;
pub mod reference;
pub mod runtime;
use crate::fields;
pub use error::PatternError;
pub use hour_cycle::CoarseHourCycle;
use icu_provider::prelude::*;
pub use item::{GenericPatternItem, PatternItem};
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, yoke::Yokeable, zerofrom::ZeroFrom,
)]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_datetime::pattern),
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[non_exhaustive]
pub enum TimeGranularity {
None,
Hours,
Minutes,
Seconds,
Nanoseconds,
}
impl Default for TimeGranularity {
fn default() -> Self {
Self::None
}
}
impl TimeGranularity {
pub fn is_top_of_hour(self, minute: u8, second: u8, nanosecond: u32) -> bool {
match self {
Self::None | Self::Hours => true,
Self::Minutes => minute == 0,
Self::Seconds => minute + second == 0,
Self::Nanoseconds => minute as u32 + second as u32 + nanosecond == 0,
}
}
#[inline]
pub(crate) fn from_ordinal(ordinal: u8) -> TimeGranularity {
use TimeGranularity::*;
match ordinal {
1 => Hours,
2 => Minutes,
3 => Seconds,
4 => Nanoseconds,
_ => None,
}
}
#[inline]
pub(crate) const fn ordinal(self) -> u8 {
use TimeGranularity::*;
match self {
None => 0,
Hours => 1,
Minutes => 2,
Seconds => 3,
Nanoseconds => 4,
}
}
}
impl From<&PatternItem> for TimeGranularity {
fn from(item: &PatternItem) -> Self {
match item {
PatternItem::Field(field) => match field.symbol {
fields::FieldSymbol::Hour(_) => Self::Hours,
fields::FieldSymbol::Minute => Self::Minutes,
fields::FieldSymbol::Second(s) => match s {
fields::Second::FractionalSecond => Self::Nanoseconds,
fields::Second::Millisecond | fields::Second::Second => Self::Seconds,
},
_ => Self::None,
},
_ => Self::None,
}
}
}