• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zbraniecki / icu4x / 12020603084

23 Nov 2024 08:43PM UTC coverage: 75.71% (+0.2%) from 75.477%
12020603084

push

github

sffc
Touch Cargo.lock

55589 of 73424 relevant lines covered (75.71%)

644270.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/components/datetime/src/provider/pattern/mod.rs
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::fields;
21
pub use error::PatternError;
22
#[cfg(feature = "datagen")]
23
pub(crate) use hour_cycle::naively_apply_preferences;
24
pub use hour_cycle::CoarseHourCycle;
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
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, yoke::Yokeable, zerofrom::ZeroFrom,
1,046,897✔
32
)]
33
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
6✔
34
#[cfg_attr(feature = "datagen", databake(path = icu_datetime::provider::pattern))]
35
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
8✔
36
#[non_exhaustive]
37
pub enum TimeGranularity {
2✔
38
    /// No time is in the pattern.
39
    None,
×
40
    /// Smallest time unit = hours.
41
    Hours,
×
42
    /// Smallest time unit = minutes.
43
    Minutes,
×
44
    /// Smallest time unit = seconds.
45
    Seconds,
×
46
    /// Smallest time unit = nanoseconds.
47
    Nanoseconds,
×
48
}
49

50
impl Default for TimeGranularity {
51
    fn default() -> Self {
14✔
52
        Self::None
14✔
53
    }
14✔
54
}
55

56
impl TimeGranularity {
57
    /// Returns [`true`] if the most granular time being displayed will align with
58
    /// the top of the hour, otherwise returns [`false`].
59
    /// e.g. `12:00:00` is at the top of the hour for any display granularity.
60
    /// e.g. `12:00:05` is only at the top of the hour if the seconds are not displayed.
61
    pub fn is_top_of_hour(self, minute: u8, second: u8, nanosecond: u32) -> bool {
610✔
62
        match self {
610✔
63
            Self::None | Self::Hours => true,
301✔
64
            Self::Minutes => minute == 0,
164✔
65
            Self::Seconds => minute + second == 0,
143✔
66
            Self::Nanoseconds => minute as u32 + second as u32 + nanosecond == 0,
2✔
67
        }
68
    }
610✔
69

70
    #[inline]
71
    pub(crate) fn from_ordinal(ordinal: u8) -> TimeGranularity {
12,164✔
72
        use TimeGranularity::*;
73
        match ordinal {
12,164✔
74
            1 => Hours,
272✔
75
            2 => Minutes,
2,830✔
76
            3 => Seconds,
8,186✔
77
            4 => Nanoseconds,
1✔
78
            _ => None,
875✔
79
        }
80
    }
12,164✔
81

82
    #[inline]
83
    pub(crate) const fn ordinal(self) -> u8 {
235,967✔
84
        use TimeGranularity::*;
85
        match self {
235,967✔
86
            None => 0,
155,955✔
87
            Hours => 1,
11,508✔
88
            Minutes => 2,
26,087✔
89
            Seconds => 3,
42,409✔
90
            Nanoseconds => 4,
8✔
91
        }
92
    }
235,967✔
93
}
94

95
impl From<PatternItem> for TimeGranularity {
96
    /// Retrieves the granularity of time represented by a [`PatternItem`].
97
    /// If the [`PatternItem`] is not time-related, returns [`None`].
98
    fn from(item: PatternItem) -> Self {
1,281,673✔
99
        match item {
1,281,673✔
100
            PatternItem::Field(field) => match field.symbol {
697,583✔
101
                fields::FieldSymbol::Hour(_) => Self::Hours,
73,955✔
102
                fields::FieldSymbol::Minute => Self::Minutes,
68,219✔
103
                fields::FieldSymbol::Second(_) => Self::Seconds,
42,227✔
104
                fields::FieldSymbol::DecimalSecond(_) => Self::Nanoseconds,
28✔
105
                _ => Self::None,
513,154✔
106
            },
107
            _ => Self::None,
584,090✔
108
        }
109
    }
1,281,673✔
110
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc