time_zone/lib.rs
1//! Time zone and daylight saving time conversion with POSIX TZ rule strings.
2//!
3//! This library stays outside the mandatory kernel clock (xila-project/core#463):
4//! it converts between UTC and local civil time, and it never touches a clock.
5//! It is `no_std` and performs no allocation, so it runs on devices with
6//! 256 KB of RAM.
7//!
8//! A time zone is described by one POSIX TZ string, the `TZ` format of POSIX
9//! `tzset`: `std offset [dst [offset] [,rule,rule]]`, for example
10//! `CET-1CEST,M3.5.0,M10.5.0/3` for `Europe/Paris`. [`TimeZone::parse`] turns the
11//! string into a `Copy` value of 32 bytes that owns its abbreviations; each of
12//! the two transition rules is packed into one 32-bit [`Rule`] value. All
13//! invalid strings return an explicit [`ParseError`].
14//!
15//! Conversions use plain `i64` seconds since the Unix epoch
16//! (1970-01-01T00:00:00Z), not the `Timestamp` type of the `time` subsystem, so
17//! that this library stays independent of the kernel clock. Callers that hold a
18//! `time::Timestamp` convert it with `Timestamp::try_as_seconds`.
19//!
20//! - UTC to local: [`TimeZone::from_utc`] returns the local date and time, the
21//! offset in seconds, the zone abbreviation and whether daylight saving time
22//! applies.
23//! - Local to UTC: [`TimeZone::to_utc`] returns an explicit [`LocalToUtc`] result:
24//! a unique instant, both instants of a repeated local time (autumn), or a
25//! nonexistent local time (spring gap). It never picks one silently.
26//!
27//! With a region feature (`iana-europe`, `iana-america` and the others, which
28//! combine) or `iana-all`, generated tables map IANA identifiers such as
29//! `Europe/Paris` to their POSIX TZ string; see the `iana` module. Without a
30//! feature, the `iana` module has no names and links no table.
31//!
32//! Limit: a POSIX TZ string holds only the current rules of a zone, not its
33//! history. Instants far in the past or future can differ from the full IANA
34//! database by the rule changes in between. The current rules of some zones are
35//! also not expressible in POSIX form, and their tzdata POSIX footer is an
36//! approximation: America/Havana (Cuba keeps -04 until 2027, while the footer
37//! says -05 with United States rules), Asia/Gaza and Asia/Hebron, and
38//! Africa/Casablanca and Africa/El_Aaiun (Morocco during Ramadan, footer
39//! `<+00>0`).
40//!
41//! ```
42//! use time_zone::TimeZone;
43//!
44//! let paris = TimeZone::parse("CET-1CEST,M3.5.0,M10.5.0/3").unwrap();
45//! let local = paris.from_utc(1_721_044_800); // 2024-07-15T12:00:00Z
46//! assert_eq!(local.offset_seconds, 7200);
47//! assert_eq!(local.abbreviation, "CEST");
48//! assert!(local.daylight_saving);
49//! assert_eq!((local.date_time.hour, local.date_time.minute), (14, 0));
50//! ```
51#![no_std]
52#![forbid(unsafe_code)]
53
54#[cfg(test)]
55extern crate std;
56
57mod civil;
58mod generated;
59pub mod iana;
60mod parse;
61mod rule;
62mod zone;
63
64pub use civil::DateTime;
65pub use parse::ParseError;
66pub use rule::{Rule, RuleKind};
67pub use zone::{ABBREVIATION_MAX, Local, LocalToUtc, TimeZone};