devela::_dep::jiff::tz

Struct TimeZone

pub struct TimeZone { /* private fields */ }
Available on crate features dep_jiff and alloc only.
Expand description

A representation of a time zone.

A time zone is a set of rules for determining the civil time, via an offset from UTC, in a particular geographic region. In many cases, the offset in a particular time zone can vary over the course of a year through transitions into and out of daylight saving time.

A TimeZone can be one of three possible representations:

  • An identifier from the IANA Time Zone Database and the rules associated with that identifier.
  • A fixed offset where there are never any time zone transitions.
  • A POSIX TZ string that specifies a standard offset and an optional daylight saving time offset along with a rule for when DST is in effect. The rule applies for every year. Since POSIX TZ strings cannot capture the full complexity of time zone rules, they generally should not be used.

The most practical and useful representation is an IANA time zone. Namely, it enjoys broad support and its database is regularly updated to reflect real changes in time zone rules throughout the world. On Unix systems, the time zone database is typically found at /usr/share/zoneinfo. For more information on how Jiff interacts with The Time Zone Database, see TimeZoneDatabase.

In typical usage, users of Jiff shouldn’t need to reference a TimeZone directly. Instead, there are convenience APIs on datetime types that accept IANA time zone identifiers and do automatic database lookups for you. For example, to convert a timestamp to a zone aware datetime:

use jiff::Timestamp;

let ts = Timestamp::from_second(1_456_789_123)?;
let zdt = ts.intz("America/New_York")?;
assert_eq!(zdt.to_string(), "2016-02-29T18:38:43-05:00[America/New_York]");

Or to convert a civil datetime to a zoned datetime corresponding to a precise instant in time:

use jiff::civil::date;

let dt = date(2024, 7, 15).at(21, 27, 0, 0);
let zdt = dt.intz("America/New_York")?;
assert_eq!(zdt.to_string(), "2024-07-15T21:27:00-04:00[America/New_York]");

Or even converted a zoned datetime from one time zone to another:

use jiff::civil::date;

let dt = date(2024, 7, 15).at(21, 27, 0, 0);
let zdt1 = dt.intz("America/New_York")?;
let zdt2 = zdt1.intz("Israel")?;
assert_eq!(zdt2.to_string(), "2024-07-16T04:27:00+03:00[Israel]");

§The system time zone

The system time zone can be retrieved via TimeZone::system. If it couldn’t be detected or if the tz-system crate feature is not enabled, then TimeZone::UTC is returned. TimeZone::system is what’s used internally for retrieving the current zoned datetime via Zoned::now.

While there is no platform independent way to detect your system’s “default” time zone, Jiff employs best-effort heuristics to determine it. (For example, by examining /etc/localtime on Unix systems.) When the heuristics fail, Jiff will emit a WARN level log. It can be viewed by installing a log compatible logger, such as env_logger.

§Custom time zones

At present, Jiff doesn’t provide any APIs for manually constructing a custom time zone. However, TimeZone::tzif is provided for reading any valid TZif formatted data, as specified by RFC 8536. This provides an interoperable way of utilizing custom time zone rules.

§A TimeZone is immutable

Once a TimeZone is created, it is immutable. That is, its underlying time zone transition rules will never change. This is true for system time zones or even if the IANA Time Zone Database it was loaded from changes on disk. The only way such changes can be observed is by re-requesting the TimeZone from a TimeZoneDatabase. (Or, in the case of the system time zone, by calling TimeZone::system.)

§Time zone equality

TimeZone provides an imperfect notion of equality. That is, when two time zones are equal, then it is guaranteed for them to have the same rules. However, two time zones may compare unequal and yet still have the same rules.

The equality semantics are as follows:

  • Two fixed offset time zones are equal when their offsets are equal.
  • Two POSIX time zones are equal when their original rule strings are byte-for-byte identical.
  • Two IANA time zones are equal when their identifiers are equal and checksums of their rules are equal.
  • In all other cases, time zones are unequal.

Time zone equality is, for example, used in APIs like Zoned::since when asking for spans with calendar units. Namely, since days can be of different lengths in different time zones, Zoned::since will return an error when the two zoned datetimes are in different time zones and when the caller requests units greater than hours.

§Dealing with ambiguity

The principal job of a TimeZone is to provide two different transformations:

  • A conversion from a Timestamp to a civil time (also known as local, naive or plain time). This conversion is always unambiguous. That is, there is always precisely one representation of civil time for any particular instant in time for a particular time zone.
  • A conversion from a civil::DateTime to an instant in time. This conversion is sometimes ambiguous in that a civil time might have either never appear on the clocks in a particular time zone (a gap), or in that the civil time may have been repeated on the clocks in a particular time zone (a fold). Typically, a transition to daylight saving time is a gap, while a transition out of daylight saving time is a fold.

The timestamp-to-civil time conversion is done via TimeZone::to_datetime, or its lower level counterpart, TimeZone::to_offset. The civil time-to-timestamp conversion is done via one of the following routines:

Here is an example where we explore the different disambiguation strategies for a fold in time, where in this case, the 1 o’clock hour is repeated:

use jiff::{civil::date, tz::TimeZone};

let tz = TimeZone::get("America/New_York")?;
let dt = date(2024, 11, 3).at(1, 30, 0, 0);
// It's ambiguous, so asking for an unambiguous instant presents an error!
assert!(tz.to_ambiguous_zoned(dt).unambiguous().is_err());
// Gives you the earlier time in a fold, i.e., before DST ends:
assert_eq!(
    tz.to_ambiguous_zoned(dt).earlier()?.to_string(),
    "2024-11-03T01:30:00-04:00[America/New_York]",
);
// Gives you the later time in a fold, i.e., after DST ends.
// Notice the offset change from the previous example!
assert_eq!(
    tz.to_ambiguous_zoned(dt).later()?.to_string(),
    "2024-11-03T01:30:00-05:00[America/New_York]",
);
// "Just give me something reasonable"
assert_eq!(
    tz.to_ambiguous_zoned(dt).compatible()?.to_string(),
    "2024-11-03T01:30:00-04:00[America/New_York]",
);

Implementations§

§

impl TimeZone

pub const UTC: TimeZone

The UTC time zone.

The offset of this time is 0 and never has any transitions.

pub fn system() -> TimeZone

Returns the system configured time zone, if available.

If the system’s default time zone could not be determined, or if the tz-system crate feature is not enabled, then this returns TimeZone::UTC. A WARN level log will also be emitted with a message explaining why time zone detection failed. The fallback to UTC is a practical trade-off, is what most other systems tend to do and is also recommended by relevant standards such as freedesktop.org.

Detection of a system’s default time zone is generally heuristic based and platform specific.

If callers need to know whether discovery of the system time zone failed, then use TimeZone::try_system.

§Platform behavior

This section is a “best effort” explanation of how the time zone is detected on supported platforms. The behavior is subject to change.

On all platforms, the TZ environment variable overrides any other heuristic, and provides a way for end users to set the time zone for specific use cases. In general, Jiff respects the POSIX TZ rules. Here are some examples:

  • TZ=America/New_York for setting a time zone via an IANA Time Zone Database Identifier.
  • TZ=/usr/share/zoneinfo/America/New_York for setting a time zone by providing a file path to a TZif file directly.
  • TZ=EST5EDT,M3.2.0,M11.1.0 for setting a time zone via a daylight saving time transition rule.

Otherwise, when TZ isn’t set, then:

On Unix systems, this inspects /etc/localtime. If it’s a symbolic link to an entry in /usr/share/zoneinfo, then the suffix is considered an IANA Time Zone Database identifier. Otherwise, /etc/localtime is read as a TZif file directly.

On Windows, the system time zone is determined via GetDynamicTimeZoneInformation. The result is then mapped to an IANA Time Zone Database identifier via Unicode’s CLDR XML data.

pub fn try_system() -> Result<TimeZone, Error>

Returns the system configured time zone, if available.

If the system’s default time zone could not be determined, or if the tz-system crate feature is not enabled, then this returns an error.

Detection of a system’s default time zone is generally heuristic based and platform specific.

Note that callers should generally prefer using TimeZone::system. If a system time zone could not be found, then it falls back to TimeZone::UTC automatically. This is often what is recommended by relevant standards such as freedesktop.org. Conversely, this routine is useful if detection of a system’s default time zone is critical.

§Platform behavior

This section is a “best effort” explanation of how the time zone is detected on supported platforms. The behavior is subject to change.

On all platforms, the TZ environment variable overrides any other heuristic, and provides a way for end users to set the time zone for specific use cases. In general, Jiff respects the POSIX TZ rules. Here are some examples:

  • TZ=America/New_York for setting a time zone via an IANA Time Zone Database Identifier.
  • TZ=/usr/share/zoneinfo/America/New_York for setting a time zone by providing a file path to a TZif file directly.
  • TZ=EST5EDT,M3.2.0,M11.1.0 for setting a time zone via a daylight saving time transition rule.

Otherwise, when TZ isn’t set, then:

On Unix systems, this inspects /etc/localtime. If it’s a symbolic link to an entry in /usr/share/zoneinfo, then the suffix is considered an IANA Time Zone Database identifier. Otherwise, /etc/localtime is read as a TZif file directly.

On Windows, the system time zone is determined via GetDynamicTimeZoneInformation. The result is then mapped to an IANA Time Zone Database identifier via Unicode’s CLDR XML data.

pub fn get(time_zone_name: &str) -> Result<TimeZone, Error>

A convenience function for performing a time zone database lookup for the given time zone identifier. It uses the default global time zone database via tz::db().

§Errors

This returns an error if the given time zone identifier could not be found in the default TimeZoneDatabase.

§Example
use jiff::{tz::TimeZone, Timestamp};

let tz = TimeZone::get("Japan")?;
assert_eq!(
    tz.to_datetime(Timestamp::UNIX_EPOCH).to_string(),
    "1970-01-01T09:00:00",
);

pub fn fixed(offset: Offset) -> TimeZone

Returns a time zone with a fixed offset.

A fixed offset will never have any transitions and won’t follow any particular time zone rules. In general, one should avoid using fixed offset time zones unless you have a specific need for them. Otherwise, IANA time zones via TimeZone::get should be preferred, as they more accurately model the actual time zone transitions rules used in practice.

§Example
use jiff::{tz::{self, TimeZone}, Timestamp};

let tz = TimeZone::fixed(tz::offset(10));
assert_eq!(
    tz.to_datetime(Timestamp::UNIX_EPOCH).to_string(),
    "1970-01-01T10:00:00",
);

pub fn posix(posix_tz_string: &str) -> Result<TimeZone, Error>

Creates a time zone from a POSIX TZ rule string.

A POSIX time zone provides a way to tersely define a single daylight saving time transition rule (or none at all) that applies for all years.

Users should avoid using this kind of time zone unless there is a specific need for it. Namely, POSIX time zones cannot capture the full complexity of time zone transition rules in the real world. (See the example below.)

§Errors

This returns an error if the given POSIX time zone string is invalid.

§Example

This example demonstrates how a POSIX time zone may be historically inaccurate:

use jiff::{civil::date, tz::TimeZone};

// The tzdb entry for America/New_York.
let iana = TimeZone::get("America/New_York")?;
// The POSIX TZ string for New York DST that went into effect in 2007.
let posix = TimeZone::posix("EST5EDT,M3.2.0,M11.1.0")?;

// New York entered DST on April 2, 2006 at 2am:
let dt = date(2006, 4, 2).at(2, 0, 0, 0);
// The IANA tzdb entry correctly reports it as ambiguous:
assert!(iana.to_ambiguous_timestamp(dt).is_ambiguous());
// But the POSIX time zone does not:
assert!(!posix.to_ambiguous_timestamp(dt).is_ambiguous());

pub fn tzif(name: &str, data: &[u8]) -> Result<TimeZone, Error>

Creates a time zone from TZif binary data, whose format is specified in RFC 8536. All versions of TZif (up through version 4) are supported.

This constructor is typically not used, and instead, one should rely on time zone lookups via time zone identifiers with routines like TimeZone::get. However, this constructor does provide one way of using custom time zones with Jiff.

The name given should be a IANA time zone database identifier.

§Errors

This returns an error if the given data was not recognized as valid TZif.

pub fn iana_name(&self) -> Option<&str>

When this time zone was loaded from an IANA time zone database entry, then this returns the canonicalized name for that time zone.

§Example
use jiff::tz::TimeZone;

let tz = TimeZone::get("america/NEW_YORK")?;
assert_eq!(tz.iana_name(), Some("America/New_York"));

pub fn to_datetime(&self, timestamp: Timestamp) -> DateTime

Returns the civil datetime corresponding to the given timestamp in this time zone.

This operation is always unambiguous. That is, for any instant in time supported by Jiff (that is, a Timestamp), there is always precisely one civil datetime corresponding to that instant.

Note that this is considered a lower level routine. Consider working with zoned datetimes instead, and use Zoned::datetime to get its civil time if necessary.

§Example
use jiff::{tz::TimeZone, Timestamp};

let tz = TimeZone::get("Europe/Rome")?;
assert_eq!(
    tz.to_datetime(Timestamp::UNIX_EPOCH).to_string(),
    "1970-01-01T01:00:00",
);

As mentioned above, consider using Zoned instead:

use jiff::{tz::TimeZone, Timestamp};

let zdt = Timestamp::UNIX_EPOCH.intz("Europe/Rome")?;
assert_eq!(zdt.datetime().to_string(), "1970-01-01T01:00:00");

pub fn to_offset(&self, _timestamp: Timestamp) -> (Offset, Dst, &str)

Returns the offset corresponding to the given timestamp in this time zone.

This operation is always unambiguous. That is, for any instant in time supported by Jiff (that is, a Timestamp), there is always precisely one offset corresponding to that instant.

Given an offset, one can use APIs like Offset::to_datetime to create a civil datetime from a timestamp.

This also returns whether this timestamp is considered to be in “daylight saving time,” as well as the abbreviation for the time zone at this time.

§Example
use jiff::{tz::{self, Dst, TimeZone}, Timestamp};

let tz = TimeZone::get("America/New_York")?;

// A timestamp in DST in New York.
let ts = Timestamp::from_second(1_720_493_204)?;
let (offset, dst, abbrev) = tz.to_offset(ts);
assert_eq!(offset, tz::offset(-4));
assert_eq!(dst, Dst::Yes);
assert_eq!(abbrev, "EDT");
assert_eq!(offset.to_datetime(ts).to_string(), "2024-07-08T22:46:44");

// A timestamp *not* in DST in New York.
let ts = Timestamp::from_second(1_704_941_204)?;
let (offset, dst, abbrev) = tz.to_offset(ts);
assert_eq!(offset, tz::offset(-5));
assert_eq!(dst, Dst::No);
assert_eq!(abbrev, "EST");
assert_eq!(offset.to_datetime(ts).to_string(), "2024-01-10T21:46:44");

pub fn to_fixed_offset(&self) -> Result<Offset, Error>

If this time zone is a fixed offset, then this returns the offset. If this time zone is not a fixed offset, then an error is returned.

If you just need an offset for a given timestamp, then you can use TimeZone::to_offset. Or, if you need an offset for a civil datetime, then you can use TimeZone::to_ambiguous_timestamp or TimeZone::to_ambiguous_zoned, although the result may be ambiguous.

Generally, this routine is useful when you need to know whether the time zone is fixed, and you want to get the offset without having to specify a timestamp. This is sometimes required for interoperating with other datetime systems that need to distinguish between time zones that are fixed and time zones that are based on rules such as those found in the IANA time zone database.

§Example
use jiff::tz::{Offset, TimeZone};

let tz = TimeZone::get("America/New_York")?;
// A named time zone is not a fixed offset
// and so cannot be converted to an offset
// without a timestamp or civil datetime.
assert_eq!(
    tz.to_fixed_offset().unwrap_err().to_string(),
    "cannot convert non-fixed IANA time zone \
     to offset without timestamp or civil datetime",
);

let tz = TimeZone::UTC;
// UTC is a fixed offset and so can be converted
// without a timestamp.
assert_eq!(tz.to_fixed_offset()?, Offset::UTC);

// And of course, creating a time zone from a
// fixed offset results in a fixed offset time
// zone too:
let tz = TimeZone::fixed(jiff::tz::offset(-10));
assert_eq!(tz.to_fixed_offset()?, jiff::tz::offset(-10));

pub fn to_zoned(&self, dt: DateTime) -> Result<Zoned, Error>

Converts a civil datetime to a Zoned in this time zone.

The given civil datetime may be ambiguous in this time zone. A civil datetime is ambiguous when either of the following occurs:

  • When the civil datetime falls into a “gap.” That is, when there is a jump forward in time where a span of time does not appear on the clocks in this time zone. This typically manifests as a 1 hour jump forward into daylight saving time.
  • When the civil datetime falls into a “fold.” That is, when there is a jump backward in time where a span of time is repeated on the clocks in this time zone. This typically manifests as a 1 hour jump backward out of daylight saving time.

This routine automatically resolves both of the above ambiguities via the Disambiguation::Compatible strategy. That in, the case of a gap, the time after the gap is used. In the case of a fold, the first repetition of the clock time is used.

§Example

This example shows how disambiguation works:

use jiff::{civil::date, tz::TimeZone};

let tz = TimeZone::get("America/New_York")?;

// This demonstrates disambiguation behavior for a gap.
let zdt = tz.to_zoned(date(2024, 3, 10).at(2, 30, 0, 0))?;
assert_eq!(zdt.to_string(), "2024-03-10T03:30:00-04:00[America/New_York]");
// This demonstrates disambiguation behavior for a fold.
// Notice the offset: the -04 corresponds to the time while
// still in DST. The second repetition of the 1 o'clock hour
// occurs outside of DST, in "standard" time, with the offset -5.
let zdt = tz.to_zoned(date(2024, 11, 3).at(1, 30, 0, 0))?;
assert_eq!(zdt.to_string(), "2024-11-03T01:30:00-04:00[America/New_York]");

pub fn to_ambiguous_zoned(&self, dt: DateTime) -> AmbiguousZoned

Converts a civil datetime to a possibly ambiguous zoned datetime in this time zone.

The given civil datetime may be ambiguous in this time zone. A civil datetime is ambiguous when either of the following occurs:

  • When the civil datetime falls into a “gap.” That is, when there is a jump forward in time where a span of time does not appear on the clocks in this time zone. This typically manifests as a 1 hour jump forward into daylight saving time.
  • When the civil datetime falls into a “fold.” That is, when there is a jump backward in time where a span of time is repeated on the clocks in this time zone. This typically manifests as a 1 hour jump backward out of daylight saving time.

Unlike TimeZone::to_zoned, this method does not do any automatic disambiguation. Instead, callers are expected to use the methods on AmbiguousZoned to resolve any ambiguity, if it occurs.

§Example

This example shows how to return an error when the civil datetime given is ambiguous:

use jiff::{civil::date, tz::TimeZone};

let tz = TimeZone::get("America/New_York")?;

// This is not ambiguous:
let dt = date(2024, 3, 10).at(1, 0, 0, 0);
assert_eq!(
    tz.to_ambiguous_zoned(dt).unambiguous()?.to_string(),
    "2024-03-10T01:00:00-05:00[America/New_York]",
);
// But this is a gap, and thus ambiguous! So an error is returned.
let dt = date(2024, 3, 10).at(2, 0, 0, 0);
assert!(tz.to_ambiguous_zoned(dt).unambiguous().is_err());
// And so is this, because it's a fold.
let dt = date(2024, 11, 3).at(1, 0, 0, 0);
assert!(tz.to_ambiguous_zoned(dt).unambiguous().is_err());

pub fn into_ambiguous_zoned(self, dt: DateTime) -> AmbiguousZoned

Converts a civil datetime to a possibly ambiguous zoned datetime in this time zone, and does so by assuming ownership of this TimeZone.

This is identical to TimeZone::to_ambiguous_zoned, but it avoids a TimeZone::clone() call. (Which are cheap, but not completely free.)

§Example

This example shows how to create a Zoned value from a TimeZone and a DateTime without cloning the TimeZone:

use jiff::{civil::date, tz::TimeZone};

let tz = TimeZone::get("America/New_York")?;
let dt = date(2024, 3, 10).at(1, 0, 0, 0);
assert_eq!(
    tz.into_ambiguous_zoned(dt).unambiguous()?.to_string(),
    "2024-03-10T01:00:00-05:00[America/New_York]",
);

pub fn to_timestamp(&self, dt: DateTime) -> Result<Timestamp, Error>

Converts a civil datetime to a Timestamp in this time zone.

The given civil datetime may be ambiguous in this time zone. A civil datetime is ambiguous when either of the following occurs:

  • When the civil datetime falls into a “gap.” That is, when there is a jump forward in time where a span of time does not appear on the clocks in this time zone. This typically manifests as a 1 hour jump forward into daylight saving time.
  • When the civil datetime falls into a “fold.” That is, when there is a jump backward in time where a span of time is repeated on the clocks in this time zone. This typically manifests as a 1 hour jump backward out of daylight saving time.

This routine automatically resolves both of the above ambiguities via the Disambiguation::Compatible strategy. That in, the case of a gap, the time after the gap is used. In the case of a fold, the first repetition of the clock time is used.

This routine is identical to TimeZone::to_zoned, except it returns a Timestamp instead of a zoned datetime. The benefit of this method is that it never requires cloning or consuming ownership of a TimeZone, and it doesn’t require construction of Zoned which has a small but non-zero cost. (This is partially because a Zoned value contains a TimeZone, but of course, a Timestamp does not.)

§Example

This example shows how disambiguation works:

use jiff::{civil::date, tz::TimeZone};

let tz = TimeZone::get("America/New_York")?;

// This demonstrates disambiguation behavior for a gap.
let ts = tz.to_timestamp(date(2024, 3, 10).at(2, 30, 0, 0))?;
assert_eq!(ts.to_string(), "2024-03-10T07:30:00Z");
// This demonstrates disambiguation behavior for a fold.
// Notice the offset: the -04 corresponds to the time while
// still in DST. The second repetition of the 1 o'clock hour
// occurs outside of DST, in "standard" time, with the offset -5.
let ts = tz.to_timestamp(date(2024, 11, 3).at(1, 30, 0, 0))?;
assert_eq!(ts.to_string(), "2024-11-03T05:30:00Z");

pub fn to_ambiguous_timestamp(&self, dt: DateTime) -> AmbiguousTimestamp

Converts a civil datetime to a possibly ambiguous timestamp in this time zone.

The given civil datetime may be ambiguous in this time zone. A civil datetime is ambiguous when either of the following occurs:

  • When the civil datetime falls into a “gap.” That is, when there is a jump forward in time where a span of time does not appear on the clocks in this time zone. This typically manifests as a 1 hour jump forward into daylight saving time.
  • When the civil datetime falls into a “fold.” That is, when there is a jump backward in time where a span of time is repeated on the clocks in this time zone. This typically manifests as a 1 hour jump backward out of daylight saving time.

Unlike TimeZone::to_timestamp, this method does not do any automatic disambiguation. Instead, callers are expected to use the methods on AmbiguousTimestamp to resolve any ambiguity, if it occurs.

This routine is identical to TimeZone::to_ambiguous_zoned, except it returns an AmbiguousTimestamp instead of a AmbiguousZoned. The benefit of this method is that it never requires cloning or consuming ownership of a TimeZone, and it doesn’t require construction of Zoned which has a small but non-zero cost. (This is partially because a Zoned value contains a TimeZone, but of course, a Timestamp does not.)

§Example

This example shows how to return an error when the civil datetime given is ambiguous:

use jiff::{civil::date, tz::TimeZone};

let tz = TimeZone::get("America/New_York")?;

// This is not ambiguous:
let dt = date(2024, 3, 10).at(1, 0, 0, 0);
assert_eq!(
    tz.to_ambiguous_timestamp(dt).unambiguous()?.to_string(),
    "2024-03-10T06:00:00Z",
);
// But this is a gap, and thus ambiguous! So an error is returned.
let dt = date(2024, 3, 10).at(2, 0, 0, 0);
assert!(tz.to_ambiguous_timestamp(dt).unambiguous().is_err());
// And so is this, because it's a fold.
let dt = date(2024, 11, 3).at(1, 0, 0, 0);
assert!(tz.to_ambiguous_timestamp(dt).unambiguous().is_err());

pub fn preceding<'t>( &'t self, timestamp: Timestamp, ) -> TimeZonePrecedingTransitions<'t>

Returns an iterator of time zone transitions preceding the given timestamp. The iterator returned yields TimeZoneTransition elements.

The order of the iterator returned moves backward through time. If there is a previous transition, then the timestamp of that transition is guaranteed to be strictly less than the timestamp given.

This is a low level API that you generally shouldn’t need. It’s useful in cases where you need to know something about the specific instants at which time zone transitions occur. For example, an embedded device might need to be explicitly programmed with daylight saving time transitions. APIs like this enable callers to explore those transitions.

A time zone transition refers to a specific point in time when the offset from UTC for a particular geographical region changes. This is usually a result of daylight saving time, but it can also occur when a geographic region changes its permanent offset from UTC.

The iterator returned is not guaranteed to yield any elements. For example, this occurs with a fixed offset time zone. Logically, it would also be possible for the iterator to be infinite, except that eventually the timestamp would overflow Jiff’s minimum timestamp value, at which point, iteration stops.

§Example: time since the previous transition

This example shows how much time has passed since the previous time zone transition:

use jiff::{Unit, Zoned};

let now: Zoned = "2024-12-31 18:25-05[US/Eastern]".parse()?;
let trans = now.time_zone().preceding(now.timestamp()).next().unwrap();
let prev_at = trans.timestamp().to_zoned(now.time_zone().clone());
let span = now.since((Unit::Year, &prev_at))?;
assert_eq!(format!("{span:#}"), "1mo 27d 17h 25m");
§Example: show the 5 previous time zone transitions

This shows how to find the 5 preceding time zone transitions (from a particular datetime) for a particular time zone:

use jiff::{tz::offset, Zoned};

let now: Zoned = "2024-12-31 18:25-05[US/Eastern]".parse()?;
let transitions = now
    .time_zone()
    .preceding(now.timestamp())
    .take(5)
    .map(|t| (
        t.timestamp().to_zoned(now.time_zone().clone()),
        t.offset(),
        t.abbreviation(),
    ))
    .collect::<Vec<_>>();
assert_eq!(transitions, vec![
    ("2024-11-03 01:00-05[US/Eastern]".parse()?, offset(-5), "EST"),
    ("2024-03-10 03:00-04[US/Eastern]".parse()?, offset(-4), "EDT"),
    ("2023-11-05 01:00-05[US/Eastern]".parse()?, offset(-5), "EST"),
    ("2023-03-12 03:00-04[US/Eastern]".parse()?, offset(-4), "EDT"),
    ("2022-11-06 01:00-05[US/Eastern]".parse()?, offset(-5), "EST"),
]);

pub fn following<'t>( &'t self, timestamp: Timestamp, ) -> TimeZoneFollowingTransitions<'t>

Returns an iterator of time zone transitions following the given timestamp. The iterator returned yields TimeZoneTransition elements.

The order of the iterator returned moves forward through time. If there is a following transition, then the timestamp of that transition is guaranteed to be strictly greater than the timestamp given.

This is a low level API that you generally shouldn’t need. It’s useful in cases where you need to know something about the specific instants at which time zone transitions occur. For example, an embedded device might need to be explicitly programmed with daylight saving time transitions. APIs like this enable callers to explore those transitions.

A time zone transition refers to a specific point in time when the offset from UTC for a particular geographical region changes. This is usually a result of daylight saving time, but it can also occur when a geographic region changes its permanent offset from UTC.

The iterator returned is not guaranteed to yield any elements. For example, this occurs with a fixed offset time zone. Logically, it would also be possible for the iterator to be infinite, except that eventually the timestamp would overflow Jiff’s maximum timestamp value, at which point, iteration stops.

§Example: time until the next transition

This example shows how much time is left until the next time zone transition:

use jiff::{Unit, Zoned};

let now: Zoned = "2024-12-31 18:25-05[US/Eastern]".parse()?;
let trans = now.time_zone().following(now.timestamp()).next().unwrap();
let next_at = trans.timestamp().to_zoned(now.time_zone().clone());
let span = now.until((Unit::Year, &next_at))?;
assert_eq!(format!("{span:#}"), "2mo 8d 7h 35m");
§Example: show the 5 next time zone transitions

This shows how to find the 5 following time zone transitions (from a particular datetime) for a particular time zone:

use jiff::{tz::offset, Zoned};

let now: Zoned = "2024-12-31 18:25-05[US/Eastern]".parse()?;
let transitions = now
    .time_zone()
    .following(now.timestamp())
    .take(5)
    .map(|t| (
        t.timestamp().to_zoned(now.time_zone().clone()),
        t.offset(),
        t.abbreviation(),
    ))
    .collect::<Vec<_>>();
assert_eq!(transitions, vec![
    ("2025-03-09 03:00-04[US/Eastern]".parse()?, offset(-4), "EDT"),
    ("2025-11-02 01:00-05[US/Eastern]".parse()?, offset(-5), "EST"),
    ("2026-03-08 03:00-04[US/Eastern]".parse()?, offset(-4), "EDT"),
    ("2026-11-01 01:00-05[US/Eastern]".parse()?, offset(-5), "EST"),
    ("2027-03-14 03:00-04[US/Eastern]".parse()?, offset(-4), "EDT"),
]);

Trait Implementations§

§

impl Clone for TimeZone

§

fn clone(&self) -> TimeZone

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for TimeZone

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl PartialEq for TimeZone

§

fn eq(&self, other: &TimeZone) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl Eq for TimeZone

§

impl StructuralPartialEq for TimeZone

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> ArchivePointee for T

§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ByteSized for T

Source§

const BYTE_ALIGN: usize = _

The alignment of this type in bytes.
Source§

const BYTE_SIZE: usize = _

The size of this type in bytes.
Source§

fn byte_align(&self) -> usize

Returns the alignment of this type in bytes.
Source§

fn byte_size(&self) -> usize

Returns the size of this type in bytes. Read more
Source§

fn ptr_size_ratio(&self) -> [usize; 2]

Returns the size ratio between Ptr::BYTES and BYTE_SIZE. Read more
Source§

impl<T, R> Chain<R> for T
where T: ?Sized,

Source§

fn chain<F>(self, f: F) -> R
where F: FnOnce(Self) -> R, Self: Sized,

Chain a function which takes the parameter by value.
Source§

fn chain_ref<F>(&self, f: F) -> R
where F: FnOnce(&Self) -> R,

Chain a function which takes the parameter by shared reference.
Source§

fn chain_mut<F>(&mut self, f: F) -> R
where F: FnOnce(&mut Self) -> R,

Chain a function which takes the parameter by exclusive reference.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> ExtAny for T
where T: Any + ?Sized,

Source§

fn type_id() -> TypeId

Returns the TypeId of Self. Read more
Source§

fn type_of(&self) -> TypeId

Returns the TypeId of self. Read more
Source§

fn type_name(&self) -> &'static str

Returns the type name of self. Read more
Source§

fn type_is<T: 'static>(&self) -> bool

Returns true if Self is of type T. Read more
Source§

fn as_any_ref(&self) -> &dyn Any
where Self: Sized,

Upcasts &self as &dyn Any. Read more
Source§

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Upcasts &mut self as &mut dyn Any. Read more
Source§

fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized,

Upcasts Box<self> as Box<dyn Any>. Read more
Source§

fn downcast_ref<T: 'static>(&self) -> Option<&T>

Available on crate feature unsafe_layout only.
Returns some shared reference to the inner value if it is of type T. Read more
Source§

fn downcast_mut<T: 'static>(&mut self) -> Option<&mut T>

Available on crate feature unsafe_layout only.
Returns some exclusive reference to the inner value if it is of type T. Read more
Source§

impl<T> ExtMem for T
where T: ?Sized,

Source§

const NEEDS_DROP: bool = _

Know whether dropping values of this type matters, in compile-time.
Source§

fn mem_align_of<T>() -> usize

Returns the minimum alignment of the type in bytes. Read more
Source§

fn mem_align_of_val(&self) -> usize

Returns the alignment of the pointed-to value in bytes. Read more
Source§

fn mem_size_of<T>() -> usize

Returns the size of a type in bytes. Read more
Source§

fn mem_size_of_val(&self) -> usize

Returns the size of the pointed-to value in bytes. Read more
Source§

fn mem_copy(&self) -> Self
where Self: Copy,

Bitwise-copies a value. Read more
Source§

fn mem_needs_drop(&self) -> bool

Returns true if dropping values of this type matters. Read more
Source§

fn mem_drop(self)
where Self: Sized,

Drops self by running its destructor. Read more
Source§

fn mem_forget(self)
where Self: Sized,

Forgets about self without running its destructor. Read more
Source§

fn mem_replace(&mut self, other: Self) -> Self
where Self: Sized,

Replaces self with other, returning the previous value of self. Read more
Source§

fn mem_take(&mut self) -> Self
where Self: Default,

Replaces self with its default value, returning the previous value of self. Read more
Source§

fn mem_swap(&mut self, other: &mut Self)
where Self: Sized,

Swaps the value of self and other without deinitializing either one. Read more
Source§

unsafe fn mem_zeroed<T>() -> T

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Source§

unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst

Available on crate feature unsafe_layout only.
Returns the value of type T represented by the all-zero byte-pattern. Read more
Source§

fn mem_as_bytes(&self) -> &[u8]
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &[u8]. Read more
Source§

fn mem_as_bytes_mut(&mut self) -> &mut [u8]
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &mut [u8]. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

Source§

impl<T> Hook for T

Source§

fn hook_ref<F>(self, f: F) -> Self
where F: FnOnce(&Self),

Applies a function which takes the parameter by shared reference, and then returns the (possibly) modified owned value. Read more
Source§

fn hook_mut<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Applies a function which takes the parameter by exclusive reference, and then returns the (possibly) modified owned value. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> LayoutRaw for T

§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Pointee for T

§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

§

impl<T> Ungil for T
where T: Send,