17 stable releases
2.0.5 | Jun 21, 2022 |
---|---|
2.0.4 | Oct 21, 2021 |
2.0.3 | Aug 13, 2021 |
2.0.2 | Jul 10, 2021 |
0.3.0 | Nov 20, 2019 |
#52 in Date and time
539 downloads per month
Used in 3 crates
39KB
616 lines
libtzfile
This library reads the system timezone information files provided by IANA and returns a Tz struct containing the TZfile fields as described in the man page (http://man7.org/linux/man-pages/man5/tzfile.5.html).
For higher level parsing, you can enable the parse or json features (merged from the former tzparse library).
In this documentation's examples, tzfile is the TZfile's path, for instance "/usr/share/zoneinfo/Europe/Paris".
Without any feature enabled, one available method : new(), which returns a Tz struct:
use libtzfile::Tz;
println!("{:?}", Tz::new(tzfile).unwrap());
Tz { tzh_timecnt_data: [-2717643600, -1633273200, -1615132800, -1601823600, -1583683200, -880210800, -820519140, -812653140, -796845540, -84380400, -68659200], tzh_timecnt_indices: [2, 1, 2, 1, 2, 3, 2, 3, 2, 1, 2], tzh_typecnt: [Ttinfo { tt_gmtoff: -26898, tt_isdst: 0, tt_abbrind: 0 }, Ttinfo { tt_gmtoff: -21600, tt_isdst: 1, tt_abbrind: 1 }, Ttinfo { tt_gmtoff: -25200, tt_isdst: 0, tt_abbrind: 2 }, Ttinfo { tt_gmtoff: -21600, tt_isdst: 1, tt_abbrind: 3 }], tz_abbr: ["LMT", "MDT", "MST", "MWT"] }
With the parse or json features enabled, you have access to additional methods. For instance, to display 2020 DST transitions in France, you can use the transition_times method:
use libtzfile::Tz;
println!("{:?}", Tz::new(tzfile).unwrap().transition_times(Some(2020)).unwrap());
[TransitionTime { time: 2020-03-29T01:00:00Z, utc_offset: 7200, isdst: true, abbreviation: "CEST" }, TransitionTime { time: 2020-10-25T01:00:00Z, utc_offset: 3600, isdst: false, abbreviation: "CET" }]
If you want more complete information about the timezone, you can use the zoneinfo method, which returns a more complete structure:
use libtzfile::Tz;
println!("{:?}", Tz::new(tzfile).unwrap().zoneinfo().unwrap());
Tzinfo { timezone: "Europe/Paris", utc_datetime: 2020-09-05T16:41:44.279502100Z, datetime: 2020-09-05T18:41:44.279502100+02:00, dst_from: Some(2020-03-29T01:00:00Z), dst_until: Some(2020-10-25T01:00:00Z), dst_period: true, raw_offset: 3600, dst_offset: 7200, utc_offset: +02:00, abbreviation: "CEST", week_number: 36 }
This more complete structure can be transformed to json via a method of the json feature (which includes methods from the parse feature):
use libtzfile::{Tz, TzError};
let tz = Tz::new(tzfile)?
.zoneinfo()?
.to_json()?;
println!("{}", tz);
{"timezone":"Europe/Paris","utc_datetime":"2020-09-05T18:04:50.546668500Z","datetime":"2020-09-05T20:04:50.546668500+02:00","dst_from":"2020-03-29T01:00:00Z","dst_until":"2020-10-25T01:00:00Z","dst_period":true,"raw_offset":3600,"dst_offset":7200,"utc_offset":"+02:00","abbreviation":"CEST","week_number":36}
This last method and feature is used for instance in my world time API.
The tests (cargo test --features=json) are working with the 2021a timezone database (MacOS 11.2.2).
License: MIT
Dependencies
~88–650KB
~12K SLoC