| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
//! Structured datetime pattern types for datagen and the data provider. |
| 6 |
//! |
| 7 |
//! <div class="stab unstable"> |
| 8 |
//! 🚧 This code is considered unstable; it may change at any time, in breaking or non-breaking ways, |
| 9 |
//! including in SemVer minor releases. While the serde representation of data structs is guaranteed |
| 10 |
//! to be stable, their Rust representation might not be. Use with caution. |
| 11 |
//! </div> |
| 12 |
|
| 13 |
mod common; |
| 14 |
mod error; |
| 15 |
mod hour_cycle; |
| 16 |
mod item; |
| 17 |
pub mod reference; |
| 18 |
pub mod runtime; |
| 19 |
|
| 20 |
use crate::provider::fields; |
| 21 |
pub use error::PatternError; |
| 22 |
pub use hour_cycle::CoarseHourCycle; |
| 23 |
#[cfg(feature = "datagen")] |
| 24 |
pub(crate) use hour_cycle::naively_apply_hour_cycle; |
| 25 |
use icu_provider::prelude::*; |
| 26 |
pub use item::{GenericPatternItem, PatternItem}; |
| 27 |
|
| 28 |
/// The granularity of time represented in a [`Pattern`](runtime::Pattern). |
| 29 |
/// Ordered from least granular to most granular for comparison. |
| 30 |
#[derive( |
| 31 |
Default, 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] |
| 37 |
pub enum TimeGranularity { |
| 38 |
/// No time fields are in the pattern, or the smallest time unit is 23/24-hour style hours (e.g., `H` or `k`). |
| 39 |
/// |
| 40 |
/// When formatting with [`TimePrecision::MinuteOptional`](crate::options::TimePrecision::MinuteOptional), |
| 41 |
/// zero minutes are retained for 23/24-hour style hours (formatting `15:00:00` as `15:00`). |
| 42 |
#[default] |
| 43 |
None = 0, |
| 44 |
/// Smallest time unit is 12-hour style hours (e.g., `h` or `K`). |
| 45 |
/// |
| 46 |
/// When formatting with [`TimePrecision::MinuteOptional`](crate::options::TimePrecision::MinuteOptional), |
| 47 |
/// zero minutes are omitted for 12-hour style hours (formatting `15:00:00` as `3 PM`). |
| 48 |
Hours = 1, |
| 49 |
/// Smallest time unit = minutes. |
| 50 |
Minutes = 2, |
| 51 |
/// Smallest time unit = seconds. |
| 52 |
Seconds = 3, |
| 53 |
/// Smallest time unit = Nanoseconds. |
| 54 |
Nanoseconds = 4, |
| 55 |
} |
| 56 |
|
| 57 |
impl TimeGranularity { |
| 58 |
/// Returns [`true`] if the most granular time being displayed will align with |
| 59 |
/// the top of the hour, otherwise returns [`false`]. |
| 60 |
/// e.g. `12:00:00` is at the top of the hour for any display granularity. |
| 61 |
/// e.g. `12:00:05` is only at the top of the hour if the seconds are not displayed. |
| 62 |
pub fn is_top_of_hour(self, minute: u8, second: u8, subsecond: u32) -> bool { |
| 63 |
match self { |
| 64 |
Self::None | Self::Hours => true, |
| 65 |
Self::Minutes => minute == 0, |
| 66 |
Self::Seconds => minute == 0 && second == 0, |
| 67 |
Self::Nanoseconds => minute == 0 && second == 0 && subsecond == 0, |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
pub(crate) fn coarse_hour_cycle(self) -> Option<CoarseHourCycle> { |
| 72 |
match self { |
| 73 |
Self::None => Some(CoarseHourCycle::H23), |
| 74 |
Self::Hours => Some(CoarseHourCycle::H11H12), |
| 75 |
_ => None, |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#[inline] |
| 80 |
pub(crate) fn from_ordinal(ordinal: u8) -> TimeGranularity { |
| 81 |
use TimeGranularity::*; |
| 82 |
match ordinal { |
| 83 |
0 => None, |
| 84 |
1 => Hours, |
| 85 |
2 => Minutes, |
| 86 |
3 => Seconds, |
| 87 |
4 => Nanoseconds, |
| 88 |
_ => None, |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
#[inline] |
| 93 |
pub(crate) const fn ordinal(self) -> u8 { |
| 94 |
use TimeGranularity::*; |
| 95 |
match self { |
| 96 |
None => 0, |
| 97 |
Hours => 1, |
| 98 |
Minutes => 2, |
| 99 |
Seconds => 3, |
| 100 |
Nanoseconds => 4, |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
impl From<PatternItem> for TimeGranularity { |
| 106 |
/// Retrieves the granularity of time represented by a [`PatternItem`]. |
| 107 |
/// If the [`PatternItem`] is not time-related, returns [`TimeGranularity::None`]. |
| 108 |
fn from(item: PatternItem) -> Self { |
| 109 |
match item { |
| 110 |
PatternItem::Field(field) => match field.symbol { |
| 111 |
fields::FieldSymbol::Hour(fields::Hour::H23) => Self::None, |
| 112 |
fields::FieldSymbol::Hour(fields::Hour::H11 | fields::Hour::H12) => Self::Hours, |
| 113 |
fields::FieldSymbol::Minute => Self::Minutes, |
| 114 |
fields::FieldSymbol::Second(_) => Self::Seconds, |
| 115 |
fields::FieldSymbol::DecimalSecond(_) => Self::Nanoseconds, |
| 116 |
_ => Self::None, |
| 117 |
}, |
| 118 |
_ => Self::None, |
| 119 |
} |
| 120 |
} |
| 121 |
} |