[go: up one dir, main page]

Crate jiff_icu

Crate jiff_icu 

Source
Expand description

This crate provides conversion routines between jiff and icu.

The conversion routines are implemented via conversion traits defined in this crate. The traits mirror the From, Into, TryFrom and TryInto traits from the standard library.

§Available conversions

§Format a zoned datetime in another locale

This example shows how one can bridge jiff to icu in order to format a zoned datetime in a particular locale. This makes use of the infallible ConvertFrom trait to convert a jiff::Zoned into a icu::time::ZonedDateTime. In order to get icu to emit the time zone information, we need to add a zone marker to our fieldset:

use icu::{
    calendar::Iso,
    time::{ZonedDateTime, TimeZoneInfo, zone::models::AtTime},
    datetime::{fieldsets, DateTimeFormatter},
    locale::locale,
};
use jiff::Zoned;
use jiff_icu::{ConvertFrom as _};

let zdt: Zoned = "2024-09-10T23:37:20-04[America/New_York]".parse()?;
let icu_zdt = ZonedDateTime::<Iso, TimeZoneInfo<AtTime>>::convert_from(&zdt);

// Format for the en-GB locale:
let formatter = DateTimeFormatter::try_new(
    locale!("en-GB").into(),
    fieldsets::YMDET::medium().with_zone(fieldsets::zone::SpecificLong),
)?;
assert_eq!(
    formatter.format(&icu_zdt).to_string(),
    "Tue, 10 Sept 2024, 23:37:20 Eastern Daylight Time",
);

// Or in the en-US locale:
let formatter = DateTimeFormatter::try_new(
    locale!("en-US").into(),
    fieldsets::YMDET::medium().with_zone(fieldsets::zone::SpecificShort),
)?;
assert_eq!(
    formatter.format(&icu_zdt).to_string(),
    "Tue, Sep 10, 2024, 11:37:20 PM EDT",
);

// Or in the "unknown" locale:
let formatter = DateTimeFormatter::try_new(
    icu::locale::Locale::UNKNOWN.into(),
    fieldsets::YMDET::medium().with_zone(fieldsets::zone::SpecificShort),
)?;
assert_eq!(
    formatter.format(&icu_zdt).to_string(),
    "2024 M09 10, Tue 23:37:20 GMT-4",
);

§Format a civil datetime in another locale

This example shows how one can bridge jiff to icu in order to format a datetime in a particular locale. This makes use of the infallible ConvertFrom trait to convert a jiff::civil::DateTime into a icu::time::DateTime.

use icu::{
    time::DateTime,
    datetime::{fieldsets, DateTimeFormatter},
    locale::locale,
};
use jiff::Zoned;
use jiff_icu::{ConvertFrom as _};

let zdt: Zoned = "2024-09-10T23:37:20-04[America/New_York]".parse()?;
let icu_datetime = DateTime::convert_from(zdt.datetime());

// Format for the en-GB locale:
let formatter = DateTimeFormatter::try_new(
    locale!("en-GB").into(),
    fieldsets::YMDET::medium(),
)?;
assert_eq!(
    formatter.format(&icu_datetime).to_string(),
    "Tue, 10 Sept 2024, 23:37:20",
);

// Or in the en-US locale:
let formatter = DateTimeFormatter::try_new(
    locale!("en-US").into(),
    fieldsets::YMDET::medium(),
)?;
assert_eq!(
    formatter.format(&icu_datetime).to_string(),
    "Tue, Sep 10, 2024, 11:37:20 PM",
);

§Convert to another calendar

While Jiff only supports the Gregorian calendar (technically the ISO 8601 calendar), ICU4X supports many calendars. This example shows how to convert a date in the Hebrew calendar to a Jiff date. This makes use of the fallible ConvertTryFrom trait to convert a icu::time::DateTime into a jiff::civil::DateTime:

use icu::{
    calendar::Date as IcuDate,
    time::{DateTime as IcuDateTime, Time as IcuTime},
};
use jiff::civil::DateTime;
use jiff_icu::{ConvertTryFrom as _};

let datetime = IcuDateTime {
    date: IcuDate::try_new_hebrew(5785, 5, 4)?,
    time: IcuTime::try_new(17, 30, 0, 0)?,
};
let zdt = DateTime::convert_try_from(datetime)?.in_tz("America/New_York")?;
assert_eq!(zdt.to_string(), "2025-02-02T17:30:00-05:00[America/New_York]");

Structs§

Error
An error that can occur when converting between types in this crate.

Traits§

ConvertFrom
Adds infallible conversions between crates that mirrors From.
ConvertInto
Adds infallible conversions between crates that mirrors Into.
ConvertTryFrom
Adds fallible conversions between crates that mirrors TryFrom.
ConvertTryInto
Adds fallible conversions between crates that mirrors TryInto.