Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for width-aware noon and midnight options for DayPeriod #444

Merged
merged 17 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/datetime/src/fields/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,15 @@ impl From<Weekday> for FieldSymbol {
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum DayPeriod {
AmPm,
NoonMidnight,
}

impl TryFrom<u8> for DayPeriod {
type Error = SymbolError;
fn try_from(b: u8) -> Result<Self, Self::Error> {
match b {
b'a' => Ok(Self::AmPm),
b'b' => Ok(Self::NoonMidnight),
b => Err(SymbolError::Unknown(b)),
}
}
Expand Down
39 changes: 28 additions & 11 deletions components/datetime/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/master/LICENSE ).

use crate::date::{self, DateTimeType};
use crate::error::DateTimeFormatError;
use crate::fields::{self, FieldLength, FieldSymbol};
use crate::pattern::{Pattern, PatternItem};
use crate::provider;
use crate::provider::helpers::DateTimeDates;
use crate::{error::DateTimeFormatError, pattern::TimeGranularity};
use std::fmt;
use writeable::Writeable;

Expand Down Expand Up @@ -96,6 +97,20 @@ fn get_day_of_week(year: usize, month: date::Month, day: date::Day) -> date::Wee
date::WeekDay::new_unchecked(result as u8)
}

/// Returns `true` if the most granular time being displayed will align with
/// the top of the hour, otherwise returns `false`.
/// e.g. `12:00:00` is at the top of the hour for hours, minutes, and seconds.
/// e.g. `12:00:05` is only at the top of the hour if the seconds are not displayed.
fn is_top_of_hour<T: DateTimeType>(pattern: &Pattern, date_time: &T) -> bool {
match pattern.most_granular_time() {
None | Some(TimeGranularity::Hours) => true,
Some(TimeGranularity::Minutes) => u8::from(date_time.minute()) == 0,
Some(TimeGranularity::Seconds) => {
u8::from(date_time.minute()) + u8::from(date_time.second()) == 0
}
}
}

pub fn write_pattern<T, W>(
pattern: &crate::pattern::Pattern,
data: &provider::gregory::DatesV1,
Expand All @@ -106,7 +121,7 @@ where
T: DateTimeType,
W: fmt::Write + ?Sized,
{
for item in &pattern.0 {
for item in pattern.items() {
match item {
PatternItem::Field(field) => match field.symbol {
FieldSymbol::Year(..) => format_number(w, date_time.year(), field.length)?,
Expand Down Expand Up @@ -154,15 +169,17 @@ where
FieldSymbol::Second(..) => {
format_number(w, date_time.second().into(), field.length)?
}
FieldSymbol::DayPeriod(period) => match period {
fields::DayPeriod::AmPm => {
let symbol =
data.get_symbol_for_day_period(period, field.length, date_time.hour());
w.write_str(symbol)?
}
},
FieldSymbol::DayPeriod(period) => {
let symbol = data.get_symbol_for_day_period(
period,
field.length,
date_time.hour(),
is_top_of_hour(&pattern, date_time),
);
w.write_str(symbol)?
}
},
PatternItem::Literal(l) => w.write_str(l)?,
PatternItem::Literal(l) => w.write_str(&l)?,
}
}
Ok(())
Expand All @@ -173,7 +190,7 @@ mod tests {
use super::*;

#[test]
fn test_format_numer() {
fn test_format_number() {
let values = &[2, 20, 201, 2017, 20173];
let samples = &[
(FieldLength::One, ["2", "20", "201", "2017", "20173"]),
Expand Down
52 changes: 47 additions & 5 deletions components/datetime/src/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,67 @@ impl<'p> From<String> for PatternItem {
}
}

/// The granularity of time represented in a pattern item.
/// Ordered from least granular to most granular for comparsion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(super) enum TimeGranularity {
Hours,
Minutes,
Seconds,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct Pattern(pub Vec<PatternItem>);
pub struct Pattern {
items: Vec<PatternItem>,
time_granularity: Option<TimeGranularity>,
}

/// Retrieves the granularity of time represented by a `PatternItem`.
/// If the `PatternItem` is not time-related, returns `None`.
fn get_time_granularity(item: &PatternItem) -> Option<TimeGranularity> {
match item {
PatternItem::Field(field) => match field.symbol {
fields::FieldSymbol::Hour(_) => Some(TimeGranularity::Hours),
fields::FieldSymbol::Minute => Some(TimeGranularity::Minutes),
fields::FieldSymbol::Second(_) => Some(TimeGranularity::Seconds),
_ => None,
},
_ => None,
}
}

impl Pattern {
pub fn items(&self) -> &[PatternItem] {
&self.items
}

pub fn from_bytes(input: &str) -> Result<Self, Error> {
Parser::new(input).parse().map(Self)
Parser::new(input).parse().map(Pattern::from)
}

// TODO(#277): This should be turned into a utility for all ICU4X.
pub fn from_bytes_combination(input: &str, date: Self, time: Self) -> Result<Self, Error> {
Parser::new(input)
.parse_placeholders(vec![time, date])
.map(Self)
.map(Pattern::from)
}

pub(super) fn most_granular_time(&self) -> Option<TimeGranularity> {
self.time_granularity
}
}

impl From<Vec<PatternItem>> for Pattern {
fn from(items: Vec<PatternItem>) -> Self {
Self {
time_granularity: items.iter().flat_map(get_time_granularity).max(),
sffc marked this conversation as resolved.
Show resolved Hide resolved
items,
}
}
nordzilla marked this conversation as resolved.
Show resolved Hide resolved
}

impl FromIterator<PatternItem> for Pattern {
fn from_iter<I: IntoIterator<Item = PatternItem>>(iter: I) -> Self {
let items: Vec<PatternItem> = iter.into_iter().collect();
Self(items)
Self::from(iter.into_iter().collect::<Vec<_>>())
}
}
72 changes: 62 additions & 10 deletions components/datetime/src/pattern/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'p> Parser<'p> {
let replacement = replacements
.get_mut(idx as usize)
.ok_or(Error::UnknownSubstitution(ch))?;
result.append(&mut replacement.0);
result.extend_from_slice(replacement.items());
let ch = chars.next().ok_or(Error::UnclosedPlaceholder)?;
if ch != '}' {
return Err(Error::UnclosedPlaceholder);
Expand Down Expand Up @@ -328,6 +328,14 @@ mod tests {
(fields::DayPeriod::AmPm.into(), FieldLength::One).into(),
],
),
(
"hh''b",
vec![
(fields::Hour::H12.into(), FieldLength::TwoDigit).into(),
"'".into(),
(fields::DayPeriod::NoonMidnight.into(), FieldLength::One).into(),
],
),
(
"y'My'M",
vec![
Expand All @@ -353,6 +361,14 @@ mod tests {
(fields::DayPeriod::AmPm.into(), FieldLength::One).into(),
],
),
(
"hh 'o''clock' b",
vec![
(fields::Hour::H12.into(), FieldLength::TwoDigit).into(),
" o'clock ".into(),
(fields::DayPeriod::NoonMidnight.into(), FieldLength::One).into(),
],
),
(
"hh''a",
vec![
Expand All @@ -361,6 +377,14 @@ mod tests {
(fields::DayPeriod::AmPm.into(), FieldLength::One).into(),
],
),
(
"hh''b",
vec![
(fields::Hour::H12.into(), FieldLength::TwoDigit).into(),
"'".into(),
(fields::DayPeriod::NoonMidnight.into(), FieldLength::One).into(),
],
),
];

for (string, pattern) in samples {
Expand All @@ -385,25 +409,41 @@ mod tests {
#[test]
fn pattern_parse_placeholders() {
let samples = vec![
("{0}", vec![Pattern(vec!["ONE".into()])], vec!["ONE".into()]),
(
"{0}",
vec![Pattern::from(vec!["ONE".into()])],
vec!["ONE".into()],
),
(
"{0}{1}",
vec![Pattern(vec!["ONE".into()]), Pattern(vec!["TWO".into()])],
vec![
Pattern::from(vec!["ONE".into()]),
Pattern::from(vec!["TWO".into()]),
],
vec!["ONE".into(), "TWO".into()],
),
(
"{0} 'at' {1}",
vec![Pattern(vec!["ONE".into()]), Pattern(vec!["TWO".into()])],
vec![
Pattern::from(vec!["ONE".into()]),
Pattern::from(vec!["TWO".into()]),
],
vec!["ONE".into(), " at ".into(), "TWO".into()],
),
(
"{0}'at'{1}",
vec![Pattern(vec!["ONE".into()]), Pattern(vec!["TWO".into()])],
vec![
Pattern::from(vec!["ONE".into()]),
Pattern::from(vec!["TWO".into()]),
],
vec!["ONE".into(), "at".into(), "TWO".into()],
),
(
"'{0}' 'at' '{1}'",
vec![Pattern(vec!["ONE".into()]), Pattern(vec!["TWO".into()])],
vec![
Pattern::from(vec!["ONE".into()]),
Pattern::from(vec!["TWO".into()]),
],
vec!["{0} at {1}".into()],
),
];
Expand All @@ -421,10 +461,22 @@ mod tests {
("{0}", vec![], Error::UnknownSubstitution('0')),
("{a}", vec![], Error::UnknownSubstitution('a')),
("{", vec![], Error::UnclosedPlaceholder),
("{0", vec![Pattern(vec![])], Error::UnclosedPlaceholder),
("{01", vec![Pattern(vec![])], Error::UnclosedPlaceholder),
("{00}", vec![Pattern(vec![])], Error::UnclosedPlaceholder),
("'{00}", vec![Pattern(vec![])], Error::UnclosedLiteral),
(
"{0",
vec![Pattern::from(vec![])],
Error::UnclosedPlaceholder,
),
(
"{01",
vec![Pattern::from(vec![])],
Error::UnclosedPlaceholder,
),
(
"{00}",
vec![Pattern::from(vec![])],
Error::UnclosedPlaceholder,
),
("'{00}", vec![Pattern::from(vec![])], Error::UnclosedLiteral),
];

for (string, replacements, error) in broken {
Expand Down
23 changes: 11 additions & 12 deletions components/datetime/src/provider/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub trait DateTimeDates {
day_period: fields::DayPeriod,
length: fields::FieldLength,
hour: date::Hour,
is_top_of_hour: bool,
) -> &Cow<str>;
}

Expand Down Expand Up @@ -180,22 +181,20 @@ impl DateTimeDates for provider::gregory::DatesV1 {
day_period: fields::DayPeriod,
length: fields::FieldLength,
hour: date::Hour,
is_top_of_hour: bool,
) -> &Cow<str> {
let widths = match day_period {
fields::DayPeriod::AmPm => &self.symbols.day_periods.format,
};
use fields::{DayPeriod::NoonMidnight, FieldLength};
let widths = &self.symbols.day_periods.format;
let symbols = match length {
fields::FieldLength::Wide => &widths.wide,
fields::FieldLength::Narrow => &widths.narrow,
FieldLength::Wide => &widths.wide,
FieldLength::Narrow => &widths.narrow,
_ => &widths.abbreviated,
};

//TODO: Once we have more dayperiod types, we'll need to handle
// this logic in the right location.
if u8::from(hour) < 12 {
&symbols.am
} else {
&symbols.pm
match (day_period, u8::from(hour), is_top_of_hour) {
(NoonMidnight, 00, true) => symbols.midnight.as_ref().unwrap_or(&symbols.am),
(NoonMidnight, 12, true) => symbols.noon.as_ref().unwrap_or(&symbols.pm),
(_, hour, _) if hour < 12 => &symbols.am,
_ => &symbols.pm,
}
}
}
Loading