#timezone #zone #chrono #tz #system #system-time #utc

tzfile

chrono::TimeZone implementation using system tz database

4 releases

0.1.3 Mar 21, 2021
0.1.2 May 30, 2020
0.1.1 Dec 22, 2019
0.1.0 Apr 23, 2019

#119 in Date and time

Download history 602/week @ 2024-07-21 957/week @ 2024-07-28 371/week @ 2024-08-04 481/week @ 2024-08-11 673/week @ 2024-08-18 477/week @ 2024-08-25 387/week @ 2024-09-01 423/week @ 2024-09-08 211/week @ 2024-09-15 315/week @ 2024-09-22 325/week @ 2024-09-29 453/week @ 2024-10-06 332/week @ 2024-10-13 586/week @ 2024-10-20 618/week @ 2024-10-27 405/week @ 2024-11-03

1,968 downloads per month
Used in 4 crates

MIT license

41KB
814 lines

tzfile

Crates.io docs.rs Build status MIT License

tzfile is a chrono::TimeZone implementation using the system tz database. It can parse compiled (binary) time zone files inside /usr/share/zoneinfo into time zone objects for chrono.

use chrono::{Utc, TimeZone};
use tzfile::Tz;

let tz = Tz::named("America/New_York")?;
let dt1 = Utc.ymd(2019, 3, 10).and_hms(6, 45, 0);
assert_eq!(dt1.with_timezone(&&tz).to_string(), "2019-03-10 01:45:00 EST");
let dt2 = Utc.ymd(2019, 3, 10).and_hms(7, 15, 0);
assert_eq!(dt2.with_timezone(&&tz).to_string(), "2019-03-10 03:15:00 EDT");

tzfile vs chrono-tz

tzfile loads the time zone information dynamically from the OS, while chrono-tz pins to a particular version of tz database and embeds the parsed result statically in the resulting executable.

Users of tzfile can get the same time zone configuration as the system, and guarantees the same behavior with other non-Rust programs. However, some systems do not provide a complete tz database (e.g. Windows), making the program not portable across multiple platforms.

Performance

Conversion from UTC to local has comparable performance with chrono-tz as both uses the same data structure (sorted array). In the reverse direction (local to UTC), tzfile caches the conversion table, allowing to be faster than chrono-tz in all cases.

Time zone type Asia/Tehran → UTC UTC → Asia/Tehran
&tzfile::Tz 107 ns 33 ns
tzfile::RcTz 125 ns 38 ns
tzfile::ArcTz 156 ns 48 ns
chrono_tz::Tz 227 ns 43 ns

Note: parsing the tz file has an upfront cost of 16 µs.

Limitations

Due to how chrono is designed, leap seconds are always ignored, even when present in the tz file. Also, only Gregorian calendar is supported.

Currently the Tz::named() and Tz::local() convenient functions are available on Unix only.

Parsing POSIX TZ rules is not yet supported, so the predicted time for the far future may be incorrect.

Dependencies

~1.5MB
~20K SLoC