10 releases (stable)

2.0.0-beta1 Nov 23, 2024
1.5.0 May 28, 2024
1.4.0 Nov 16, 2023
1.2.0 Apr 14, 2023
0.0.1 Apr 29, 2021

#217 in Internationalization (i18n)

Download history 22742/week @ 2024-08-19 26737/week @ 2024-08-26 23716/week @ 2024-09-02 25214/week @ 2024-09-09 28308/week @ 2024-09-16 60652/week @ 2024-09-23 60455/week @ 2024-09-30 59370/week @ 2024-10-07 35345/week @ 2024-10-14 33164/week @ 2024-10-21 27325/week @ 2024-10-28 36784/week @ 2024-11-04 35974/week @ 2024-11-11 33251/week @ 2024-11-18 18891/week @ 2024-11-25 29051/week @ 2024-12-02

117,768 downloads per month
Used in 47 crates (5 directly)

Unicode-3.0

2.5MB
42K SLoC

icu_timezone crates.io

Types for resolving and manipulating time zones.

Fields

In ICU4X, a formattable time zone consists of up to four different fields:

  1. The time zone ID
  2. The offset from UTC
  3. A timestamp, as time zone names can change over time
  4. The zone variant, representing concepts such as Standard, Summer, Daylight, and Ramadan time

Time Zone

The time zone ID corresponds to a time zone from the time zone database. The time zone ID usually corresponds to the largest city in the time zone.

There are two mostly-interchangeable standards for time zone IDs:

  1. IANA time zone IDs, like "America/Chicago"
  2. BCP-47 time zone IDs, like "uschi"

ICU4X uses BCP-47 time zone IDs for all of its APIs. To get a BCP-47 time zone from an IANA time zone, use TimeZoneIdMapper.

UTC Offset

The UTC offset precisely states the time difference between the time zone in question and Coordinated Universal Time (UTC).

In localized strings, it is often rendered as "UTC-6", meaning 6 hours less than UTC (some locales use the term "GMT" instead of "UTC").

Timestamp

Some time zones change names over time, such as when changing "metazone". For example, Portugal changed from "Western European Time" to "Central European Time" and back in the 1990s, without changing time zone id (Europe/Lisbon/ptlis). Therefore, a timestamp is needed to resolve such generic time zone names.

It is not required to set the timestamp on TimeZoneInfo. If it is not set, some string formats may be unsupported.

Zone Variant

Many zones use different names and offsets in the summer than in the winter. In ICU4X, this is called the zone variant.

CLDR has two zone variants, named "standard" and "daylight". However, the mapping of these variants to specific observed offsets varies from time zone to time zone, and they may not consistently represent winter versus summer time.

Note: It is not required to set the zone variant on TimeZoneInfo. If it is not set, some string formats may be unsupported.

Examples

use icu::calendar::{Date, Time};
use icu::timezone::TimeZoneBcp47Id;
use icu::timezone::TimeZoneIdMapper;
use icu::timezone::ZoneVariant;
use tinystr::tinystr;

// Parse the IANA ID
let id = TimeZoneIdMapper::new().iana_to_bcp47("America/Chicago");

// Alternatively, use the BCP47 ID directly
let id = TimeZoneBcp47Id(tinystr!(8, "uschi"));

// Create a TimeZoneInfo<Base> by associating the ID with an offset
let time_zone = id.with_offset("-0600".parse().ok());

// Extend to a TimeZoneInfo<AtTime> by adding a local time
let time_zone_at_time = time_zone
    .at_time((Date::try_new_iso(2023, 12, 2).unwrap(), Time::midnight()));

// Extend to a TimeZoneInfo<Full> by adding a zone variant
let time_zone_with_variant =
    time_zone_at_time.with_zone_variant(ZoneVariant::Standard);

More Information

For more information on development, authorship, contributing etc. please visit ICU4X home page.

Dependencies

~1.3–2MB
~39K SLoC