tz #
The IANA time zone database, read through TZif (RFC 9636), and the local time
of a zone at an instant.
A hotel's check-in, a till's daily closure, an accounting entry's date and a
calendar's event all read local time, and local time is a zone's rules: the
offset from UT, whether it is daylight saving time, and the changes between
them. tz reads those rules from TZif files, versions 1 to 4, including the
footer's POSIX TZ string that carries them past the last stored transition.
tz.db holds a tzdata release compiled by zic, and the tz command line
queries it.
Usage #
The local time type of a zone at an instant:
# let paris = Option.get (Tzdb.find "Europe/Paris");;
val paris : Tz.t = <abstr>
# let t = Option.get (Timestamp.Rfc3339.parse "2024-07-01T12:00:00Z");;
val t : Ptime.t = <abstr>
# Fmt.pr "%a@." Tz.Local_time_type.pp (Tz.type_at paris t);;
CEST +02:00 DST
- : unit = ()
A local time maps to the instants at which the clock reads it. When the clock
moves forward, a local time is missing, and the result names the instant of
the change; when it moves back, a local time is ambiguous, and both instants
come back for the caller to choose between:
# let day s = Result.get_ok (Timestamp.Date.of_string s);;
val day : string -> Timestamp.Date.t = <fun>
# Fmt.pr "%a@." Tz.pp_local (Tz.local paris (day "2024-03-31") (2 * 3600 + 1800));;
missing 2024-03-31T01:00:00Z
- : unit = ()
# Fmt.pr "%a@." Tz.pp_local (Tz.local paris (day "2024-10-27") (2 * 3600 + 1800));;
ambiguous 2024-10-27T00:30:00Z 2024-10-27T01:30:00Z
- : unit = ()
The next change of a zone's local time type:
# Tz.next_transition paris t
|> Option.iter (fun (at, ty) ->
Fmt.pr "%a %a@." Timestamp.Rfc3339.pp at Tz.Local_time_type.pp ty);;
2024-10-27T01:00:00Z CET +01:00
- : unit = ()
A TZif file decodes with Tz.decode, and a footer's TZ string with
Tz.Posix.of_string:
# let p = Result.get_ok (Tz.Posix.of_string "<-03>3<-02>,M3.5.0/-2,M10.5.0/-1");;
val p : Tz.Posix.t = <abstr>
# Fmt.pr "%a@." Tz.Local_time_type.pp (Tz.Posix.std p);;
-03 -03:00
- : unit = ()
The command line #
$ tz at Europe/Paris 2024-07-01T12:00:00Z
2024-07-01 14:00:00 CEST +02:00 DST
$ tz local Europe/Paris 2024-10-27 02:30
ambiguous 2024-10-27T00:30:00Z 2024-10-27T01:30:00Z
$ tz next Europe/Paris 2024-07-01T12:00:00Z
2024-10-27T01:00:00Z CET +01:00
$ tz version
2026f
tz zones lists the zones, and tz zones --links each link with its zone.
The data #
data/tzdata.zi is the tzdata 2026f release in the single-file form its
makefile writes, with the backzone data merged in for the zones of
zone.tab, as its third line records. data/zoneinfo is that file's zones
compiled by zic in the fat TZif form, and data/zdump-*.gz are zdump's traces
over every zone; sh data/update.sh [TZDATA_ZI] writes all three again from a
new release. The tools are the copies of the tz reference zic and zdump that
glibc 2.43 ships.
What it covers #
- TZif versions 1 to 4 (RFC 9636 section 3): the header, the 32-bit and 64-bit
data blocks, leap-second records with the truncation and expiry of version
4, and the footer, with each MUST of the section checked on decoding. - POSIX TZ strings with the version 3 extension of section 3.3.1: transition
times from -167 to 167 hours. A TZ string with daylight saving time and no
rule is refused rather than given a guessed one. - Before the first transition a zone's time is its first local time type, and
past the last one its footer's. - A file with leap seconds counts them in its transition times; the functions
that take an instant convert between that time scale and POSIX time.
The tests replay zdump's trace of every zone line by line, and check the
examples of RFC 9636 Appendix B and section 3.3.1.
License #
ISC. See LICENSE.md.