#时区 #iana #json #高阶 #tzif #tzfiles

无 std libtzfile

本库提供对 IANA 系统时区信息文件 (TZIF) 的低级和高级解析

19 个稳定版本 (3 个主要版本)

3.1.0 2024年4月6日
3.0.0 2024年3月27日
2.0.5 2022年6月21日
2.0.4 2021年10月21日
0.3.0 2019年11月20日

42日期和时间 分类中

Download history 86/week @ 2024-05-02 51/week @ 2024-05-09 80/week @ 2024-05-16 60/week @ 2024-05-23 66/week @ 2024-05-30 67/week @ 2024-06-06 63/week @ 2024-06-13 81/week @ 2024-06-20 60/week @ 2024-06-27 43/week @ 2024-07-04 26/week @ 2024-07-11 41/week @ 2024-07-18 59/week @ 2024-07-25 36/week @ 2024-08-01 54/week @ 2024-08-08 62/week @ 2024-08-15

224 每月下载量
3 crates 中使用

MIT 许可证

46KB
767

libtzfile

Current Crates.io Version Current docs Version Downloads badge

本库读取和解析由 IANA 提供的系统时区信息文件 (TZ 文件)

默认特性是 std。使用 default-features = false,该 crate 为 no_stduses alloc::vec。在两种情况下,new() 方法返回一个包含 TZfile 字段的 Tz 结构体,如 man 页面中所述 (http://man7.org/linux/man-pages/man5/tzfile.5.html)。

  • 使用 no_std 时,函数签名是 new(buf: Vec<u8>) 其中 buf 是 TZ 文件数据
// no_std
[dependencies]
libtzfile = { version = "3.1.0", default-features = false }
let tzfile = include_bytes!("/usr/share/zoneinfo/America/Phoenix").to_vec();
let tz = Tz::new(tzfile).unwrap();
  • 使用 std(默认特性),函数签名是 new(tz: &str) 其中 tz 是 TZ 文件名
// std is the default
[dependencies]
libtzfile = "3.1.0"
use libtzfile::Tz;
let tzfile: &str = "/usr/share/zoneinfo/America/Phoenix";
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_utoff: -26898, tt_isdst: 0, tt_abbrind: 0 }, Ttinfo { tt_utoff: -21600, tt_isdst: 1, tt_abbrind: 1 }, Ttinfo { tt_utoff: -25200, tt_isdst: 0, tt_abbrind: 2 }, Ttinfo { tt_utoff: -21600, tt_isdst: 1, tt_abbrind: 3 }], tz_abbr: ["LMT", "MDT", "MST", "MWT"] }

对于高级解析,您可以启用 parsejson 特性。例如,要显示法国 2020 年夏令时的转换,您可以使用 transition_times 方法

use libtzfile::Tz;
let tzfile: &str = "/usr/share/zoneinfo/Europe/Paris";
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" }]

如果您想获取更多关于时区的信息,可以使用 zoneinfo 方法,它返回一个更完整的结构体

use libtzfile::Tz;
let tzfile: &str = "/usr/share/zoneinfo/Europe/Paris";
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 }

这个更完整的结构体实现了 Serialize 特性,可以通过 json 特性(包括 parse 特性中的方法)的方法转换为 json 字符串

use libtzfile::{Tz, TzError};
let tzfile: &str = "/usr/share/zoneinfo/Europe/Paris";
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}

该特性用在我的 world time API 中。

测试(cargo testcargo test --no-default-featurescargo test --features parse | json)与2024a时区数据库兼容。

许可:MIT

依赖项

~81–700KB
~13K SLoC