Skip to content

Commit

Permalink
Migrate some tests to neo formatter (#5246)
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc authored Jul 19, 2024
1 parent b2c55a6 commit 1544cbe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 118 deletions.
69 changes: 69 additions & 0 deletions components/datetime/src/neo_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,75 @@
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! Temporary module for neo formatter markers.
//!
//! # Examples
//!
//! ## Time Zone Formatting
//!
//! Here, we configure a [`NeoFormatter`] to format with generic non-location short,
//! which falls back to GMT when unavailable (see [`NeoTimeZoneGenericShortMarker`]).
//!
//! ```
//! use icu::calendar::DateTime;
//! use icu::timezone::{CustomTimeZone, MetazoneCalculator, TimeZoneIdMapper};
//! use icu::datetime::neo::TypedNeoFormatter;
//! use icu::datetime::neo_marker::NeoTimeZoneGenericShortMarker;
//! use icu::datetime::NeverCalendar;
//! use icu::locale::locale;
//! use tinystr::tinystr;
//! use writeable::assert_try_writeable_eq;
//!
//! // Set up the time zone. Note: the inputs here are
//! // 1. The GMT offset
//! // 2. The IANA time zone ID
//! // 3. A datetime (for metazone resolution)
//! // 4. Note: we do not need the zone variant because of `load_generic_*()`
//!
//! // Set up the Metazone calculator, time zone ID mapper,
//! // and the DateTime to use in calculation
//! let mzc = MetazoneCalculator::new();
//! let mapper = TimeZoneIdMapper::new();
//! let datetime = DateTime::try_new_iso_datetime(2022, 8, 29, 0, 0, 0)
//! .unwrap();
//!
//! // Set up the formatter
//! let mut tzf = TypedNeoFormatter::<NeverCalendar, NeoTimeZoneGenericShortMarker>::try_new(
//! &locale!("en").into(),
//! // Length does not matter here: it is specified in the type parameter
//! Default::default(),
//! )
//! .unwrap();
//!
//! // "uschi" - has metazone symbol data for generic_non_location_short
//! let mut time_zone = "-0600".parse::<CustomTimeZone>().unwrap();
//! time_zone.time_zone_id = mapper.as_borrowed().iana_to_bcp47("America/Chicago");
//! time_zone.maybe_calculate_metazone(&mzc, &datetime);
//! assert_try_writeable_eq!(
//! tzf.format(&time_zone),
//! "CT"
//! );
//!
//! // "ushnl" - has time zone override symbol data for generic_non_location_short
//! let mut time_zone = "-1000".parse::<CustomTimeZone>().unwrap();
//! time_zone.time_zone_id = Some(tinystr!(8, "ushnl").into());
//! time_zone.maybe_calculate_metazone(&mzc, &datetime);
//! assert_try_writeable_eq!(
//! tzf.format(&time_zone),
//! "HST"
//! );
//!
//! // GMT with offset - used when metazone is not available
//! let mut time_zone = "+0530".parse::<CustomTimeZone>().unwrap();
//! assert_try_writeable_eq!(
//! tzf.format(&time_zone),
//! "GMT+05:30"
//! );
//!
//! # Ok::<(), icu::datetime::DateTimeError>(())
//! ```
#[cfg(doc)]
use crate::neo::NeoFormatter;

use core::marker::PhantomData;

Expand Down
118 changes: 0 additions & 118 deletions components/datetime/src/time_zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,80 +89,6 @@ where
///
/// [`CustomTimeZone`](icu_timezone::CustomTimeZone) can be used as formatting input.
///
/// # Examples
///
/// Here, we configure the [`TimeZoneFormatter`] to first look for time zone formatting symbol
/// data for `generic_non_location_short`, and if it does not exist, to subsequently check
/// for `generic_non_location_long` data.
///
/// ```
/// use icu::calendar::DateTime;
/// use icu::timezone::{CustomTimeZone, MetazoneCalculator, TimeZoneIdMapper};
/// use icu::datetime::{DateTimeError, time_zone::TimeZoneFormatter};
/// use icu::locale::locale;
/// use tinystr::tinystr;
/// use writeable::assert_writeable_eq;
///
/// // Set up the time zone. Note: the inputs here are
/// // 1. The GMT offset
/// // 2. The IANA time zone ID
/// // 3. A datetime (for metazone resolution)
/// // 4. Note: we do not need the zone variant because of `load_generic_*()`
///
/// // Set up the Metazone calculator, time zone ID mapper,
/// // and the DateTime to use in calculation
/// let mzc = MetazoneCalculator::new();
/// let mapper = TimeZoneIdMapper::new();
/// let datetime = DateTime::try_new_iso_datetime(2022, 8, 29, 0, 0, 0)
/// .unwrap();
///
/// // Set up the formatter
/// let mut tzf = TimeZoneFormatter::try_new(
/// &locale!("en").into(),
/// Default::default(),
/// )
/// .unwrap();
/// tzf.include_generic_non_location_short()?
/// .include_generic_non_location_long()?;
///
/// // "uschi" - has metazone symbol data for generic_non_location_short
/// let mut time_zone = "-0600".parse::<CustomTimeZone>().unwrap();
/// time_zone.time_zone_id = mapper.as_borrowed().iana_to_bcp47("America/Chicago");
/// time_zone.maybe_calculate_metazone(&mzc, &datetime);
/// assert_writeable_eq!(
/// tzf.format(&time_zone),
/// "CT"
/// );
///
/// // "ushnl" - has time zone override symbol data for generic_non_location_short
/// let mut time_zone = "-1000".parse::<CustomTimeZone>().unwrap();
/// time_zone.time_zone_id = Some(tinystr!(8, "ushnl").into());
/// time_zone.maybe_calculate_metazone(&mzc, &datetime);
/// assert_writeable_eq!(
/// tzf.format(&time_zone),
/// "HST"
/// );
///
/// // "frpar" - does not have symbol data for generic_non_location_short, so falls
/// // back to generic_non_location_long
/// let mut time_zone = "+0100".parse::<CustomTimeZone>().unwrap();
/// time_zone.time_zone_id = Some(tinystr!(8, "frpar").into());
/// time_zone.maybe_calculate_metazone(&mzc, &datetime);
/// assert_writeable_eq!(
/// tzf.format(&time_zone),
/// "Central European Time"
/// );
///
/// // GMT with offset - used when metazone is not available
/// let mut time_zone = "+0530".parse::<CustomTimeZone>().unwrap();
/// assert_writeable_eq!(
/// tzf.format(&time_zone),
/// "GMT+05:30"
/// );
///
/// # Ok::<(), DateTimeError>(())
/// ```
///
/// [data provider]: icu_provider
#[derive(Debug)]
pub struct TimeZoneFormatter {
Expand Down Expand Up @@ -477,29 +403,6 @@ impl TimeZoneFormatter {
/// ✨ *Enabled with the `compiled_data` Cargo feature.*
///
/// [📚 Help choosing a constructor](icu_provider::constructors)
///
/// # Examples
///
/// Default format is Localized GMT:
///
/// ```
/// use icu::datetime::time_zone::{
/// TimeZoneFormatter, TimeZoneFormatterOptions,
/// };
/// use icu::locale::locale;
/// use icu::timezone::CustomTimeZone;
/// use writeable::assert_writeable_eq;
///
/// let tzf = TimeZoneFormatter::try_new(
/// &locale!("es").into(),
/// TimeZoneFormatterOptions::default(),
/// )
/// .unwrap();
///
/// let time_zone = "-0700".parse::<CustomTimeZone>().unwrap();
///
/// assert_writeable_eq!(tzf.format(&time_zone), "GMT-07:00");
/// ```
);

#[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
Expand Down Expand Up @@ -732,27 +635,6 @@ impl TimeZoneFormatter {

/// Takes a [`TimeZoneInput`] implementer and returns an instance of a [`FormattedTimeZone`]
/// that contains all information necessary to display a formatted time zone and operate on it.
///
/// # Examples
///
/// ```
/// use icu::datetime::time_zone::{
/// TimeZoneFormatter, TimeZoneFormatterOptions,
/// };
/// use icu::locale::locale;
/// use icu::timezone::CustomTimeZone;
/// use writeable::assert_writeable_eq;
///
/// let tzf = TimeZoneFormatter::try_new(
/// &locale!("en").into(),
/// TimeZoneFormatterOptions::default(),
/// )
/// .expect("Failed to create TimeZoneFormatter");
///
/// let time_zone = CustomTimeZone::utc();
///
/// assert_writeable_eq!(tzf.format(&time_zone), "GMT");
/// ```
pub fn format<'l, T>(&'l self, value: &T) -> FormattedTimeZone<'l>
where
T: TimeZoneInput,
Expand Down

0 comments on commit 1544cbe

Please sign in to comment.