#chrono

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
Download history 22/week @ 2021-10-04 58/week @ 2021-10-11 45/week @ 2021-10-18 85/week @ 2021-10-25 66/week @ 2021-11-01 59/week @ 2021-11-08 49/week @ 2021-11-15 106/week @ 2021-11-22 37/week @ 2021-11-29 33/week @ 2021-12-06 57/week @ 2021-12-13 69/week @ 2021-12-20 8/week @ 2021-12-27 74/week @ 2022-01-03 91/week @ 2022-01-10 54/week @ 2022-01-17

229 downloads per month
Used in 2 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

~1MB
~14K SLoC