icu_datetime/provider/pattern/
mod.rs1mod common;
14mod error;
15mod hour_cycle;
16mod item;
17pub mod reference;
18pub mod runtime;
19
20use crate::provider::fields;
21pub use error::PatternError;
22#[cfg(feature = "datagen")]
23pub(crate) use hour_cycle::naively_apply_preferences;
24pub use hour_cycle::CoarseHourCycle;
25use icu_provider::prelude::*;
26pub use item::{GenericPatternItem, PatternItem};
27
28#[derive(
31    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, yoke::Yokeable, zerofrom::ZeroFrom,
32)]
33#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
34#[cfg_attr(feature = "datagen", databake(path = icu_datetime::provider::pattern))]
35#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
36#[non_exhaustive]
37pub enum TimeGranularity {
38    None,
40    Hours,
42    Minutes,
44    Seconds,
46    Nanoseconds,
48}
49
50impl Default for TimeGranularity {
51    fn default() -> Self {
52        Self::None
53    }
54}
55
56impl TimeGranularity {
57    pub fn is_top_of_hour(self, minute: u8, second: u8, subsecond: u32) -> bool {
62        match self {
63            Self::None | Self::Hours => true,
64            Self::Minutes => minute == 0,
65            Self::Seconds => minute == 0 && second == 0,
66            Self::Nanoseconds => minute == 0 && second == 0 && subsecond == 0,
67        }
68    }
69
70    #[inline]
71    pub(crate) fn from_ordinal(ordinal: u8) -> TimeGranularity {
72        use TimeGranularity::*;
73        match ordinal {
74            1 => Hours,
75            2 => Minutes,
76            3 => Seconds,
77            4 => Nanoseconds,
78            _ => None,
79        }
80    }
81
82    #[inline]
83    pub(crate) const fn ordinal(self) -> u8 {
84        use TimeGranularity::*;
85        match self {
86            None => 0,
87            Hours => 1,
88            Minutes => 2,
89            Seconds => 3,
90            Nanoseconds => 4,
91        }
92    }
93}
94
95impl From<PatternItem> for TimeGranularity {
96    fn from(item: PatternItem) -> Self {
99        match item {
100            PatternItem::Field(field) => match field.symbol {
101                fields::FieldSymbol::Hour(_) => Self::Hours,
102                fields::FieldSymbol::Minute => Self::Minutes,
103                fields::FieldSymbol::Second(_) => Self::Seconds,
104                fields::FieldSymbol::DecimalSecond(_) => Self::Nanoseconds,
105                _ => Self::None,
106            },
107            _ => Self::None,
108        }
109    }
110}