Skip to content

Commit 9c48ed5

Browse files
authored
Add hmss and with_time_hmss functions to field set API (#6473)
Part of #6242 This addresses feedback that setting a subsecond time precision was too verbose.
1 parent 402c006 commit 9c48ed5

26 files changed

+461
-356
lines changed

components/datetime/src/fieldsets.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ macro_rules! impl_time_precision_constructors {
180180
pub fn hms() -> Self {
181181
Self::for_length(Default::default()).with_time_precision(TimePrecision::Second)
182182
}
183+
#[doc = concat!("Creates a ", stringify!($type), " that formats hours, minutes, seconds, and subseconds with the default length.")]
184+
pub fn hmss(subsecond_digits: SubsecondDigits) -> Self {
185+
Self::for_length(Default::default()).with_time_precision(TimePrecision::Subsecond(subsecond_digits))
186+
}
183187
}
184188
};
185189
}
@@ -303,6 +307,17 @@ macro_rules! impl_date_to_time_helpers {
303307
}
304308
}
305309
/// Shorthand to associate this field set with [`TimePrecision::Minute`].
310+
///
311+
/// # Examples
312+
///
313+
/// ```
314+
#[doc = concat!("use icu::datetime::fieldsets::", stringify!($type), ";")]
315+
/// use icu::datetime::options::TimePrecision;
316+
///
317+
#[doc = concat!("let fs1 = ", stringify!($type), "::medium().with_time(TimePrecision::Minute);")]
318+
#[doc = concat!("let fs2 = ", stringify!($type), "::medium().with_time_hm();")]
319+
/// assert_eq!(fs1, fs2);
320+
/// ```
306321
pub fn with_time_hm(self) -> $type_time {
307322
$type_time {
308323
length: self.length,
@@ -312,6 +327,17 @@ macro_rules! impl_date_to_time_helpers {
312327
}
313328
}
314329
/// Shorthand to associate this field set with [`TimePrecision::Second`].
330+
///
331+
/// # Examples
332+
///
333+
/// ```
334+
#[doc = concat!("use icu::datetime::fieldsets::", stringify!($type), ";")]
335+
/// use icu::datetime::options::TimePrecision;
336+
///
337+
#[doc = concat!("let fs1 = ", stringify!($type), "::medium().with_time(TimePrecision::Second);")]
338+
#[doc = concat!("let fs2 = ", stringify!($type), "::medium().with_time_hms();")]
339+
/// assert_eq!(fs1, fs2);
340+
/// ```
315341
pub fn with_time_hms(self) -> $type_time {
316342
$type_time {
317343
length: self.length,
@@ -320,6 +346,27 @@ macro_rules! impl_date_to_time_helpers {
320346
$(year_style: yes_to!(self.year_style, $yearstyle_yes),)?
321347
}
322348
}
349+
/// Shorthand to associate this field set with [`TimePrecision::Subsecond`].
350+
///
351+
/// # Examples
352+
///
353+
/// ```
354+
#[doc = concat!("use icu::datetime::fieldsets::", stringify!($type), ";")]
355+
/// use icu::datetime::options::TimePrecision;
356+
/// use icu::datetime::options::SubsecondDigits::S2;
357+
///
358+
#[doc = concat!("let fs1 = ", stringify!($type), "::medium().with_time(TimePrecision::Subsecond(S2));")]
359+
#[doc = concat!("let fs2 = ", stringify!($type), "::medium().with_time_hmss(S2);")]
360+
/// assert_eq!(fs1, fs2);
361+
/// ```
362+
pub fn with_time_hmss(self, subsecond_digits: SubsecondDigits) -> $type_time {
363+
$type_time {
364+
length: self.length,
365+
time_precision: Some(TimePrecision::Subsecond(subsecond_digits)),
366+
alignment: ternary!(self.alignment, Default::default(), $($alignment_yes)?),
367+
$(year_style: yes_to!(self.year_style, $yearstyle_yes),)?
368+
}
369+
}
323370
}
324371
};
325372
}
@@ -1192,6 +1239,28 @@ impl_time_marker!(
11921239
/// );
11931240
/// ```
11941241
///
1242+
/// Conveniently construct a time formatter with subseconds:
1243+
///
1244+
/// ```
1245+
/// use icu::datetime::input::Time;
1246+
/// use icu::datetime::fieldsets::T;
1247+
/// use icu::datetime::options::SubsecondDigits;
1248+
/// use icu::datetime::NoCalendarFormatter;
1249+
/// use icu::locale::locale;
1250+
/// use writeable::assert_writeable_eq;
1251+
///
1252+
/// let formatter = NoCalendarFormatter::try_new(
1253+
/// locale!("en").into(),
1254+
/// T::hmss(SubsecondDigits::S4),
1255+
/// )
1256+
/// .unwrap();
1257+
///
1258+
/// assert_writeable_eq!(
1259+
/// formatter.format(&Time::try_new(18, 24, 36, 987654321).unwrap()),
1260+
/// "6:24:36.9876 PM"
1261+
/// );
1262+
/// ```
1263+
///
11951264
/// [`DateTimeFormatterPreferences`]: crate::DateTimeFormatterPreferences
11961265
T,
11971266
description = "time (locale-dependent hour cycle)",

components/datetime/src/options/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ impl IntoOption<YearStyle> for YearStyle {
326326
///
327327
/// # Examples
328328
///
329+
/// Comparison of all time precision options:
330+
///
329331
/// ```
330332
/// use icu::datetime::input::Time;
331333
/// use icu::datetime::fieldsets::T;

0 commit comments

Comments
 (0)