#timezone #date-time #time #calendar #date #zone #unix-timestamp

no-std tz-rs

A pure Rust reimplementation of libc functions localtime, gmtime and mktime

28 releases (5 breaking)

0.6.14 Aug 13, 2022
0.6.12 Jul 26, 2022
0.6.7 Mar 23, 2022

#16 in Date and time

Download history 23974/week @ 2023-12-12 22834/week @ 2023-12-19 17944/week @ 2023-12-26 22558/week @ 2024-01-02 23552/week @ 2024-01-09 25749/week @ 2024-01-16 25021/week @ 2024-01-23 29689/week @ 2024-01-30 29526/week @ 2024-02-06 25801/week @ 2024-02-13 25676/week @ 2024-02-20 24108/week @ 2024-02-27 24989/week @ 2024-03-05 29796/week @ 2024-03-12 30139/week @ 2024-03-19 25551/week @ 2024-03-26

114,871 downloads per month
Used in 71 crates (16 directly)

MIT/Apache

230KB
4K SLoC

tz-rs

version Minimum supported Rust version Documentation

A pure Rust reimplementation of libc functions localtime, gmtime and mktime.

This crate allows to convert between a Unix timestamp and a calendar time exprimed in the proleptic gregorian calendar with a provided time zone.

Time zones are provided to the library with a POSIX TZ string which can be read from the environment.

Two formats are currently accepted for the TZ string:

  • std offset[dst[offset][,start[/time],end[/time]]] providing a time zone description,
  • file or :file providing the path to a TZif file, which is absolute or relative to the system timezone directory.

See also the Linux manual page of tzset(3) and the glibc documentation of the TZ environment variable.

Context

Calls to libc localtime_r and other related functions from Rust are not safe in a multithreaded application, because they may internally set the TZ environment variable with the setenv function, which is not thread-safe.

See RUSTSEC-2020-0071 and RUSTSEC-2020-0159 for more information.

Documentation

Documentation is hosted on docs.rs.

Platform support

This crate is mainly intended for UNIX platforms.

Since the time zone database files are not included in this crate, non-UNIX users can download a copy of the database on the IANA site and compile the time zone database files to a local directory.

The database files can then be read by specifying an absolute path in the TZ string:

TimeZone::from_posix_tz(format!("{local_database_dir}/usr/share/zoneinfo/Pacific/Auckland"))?;

Note that the determination of the local time zone with this crate is not supported on non-UNIX platforms.

Alternatively, a crate like tzdb can be used, which statically provides existing time zone definitions for this crate, and supports finding the local time zone for all Tier 1 platforms.

Date time formatting (equivalent of libc strftime function)

This crate doesn't provide custom date time formatting support, but the custom-format crate can be used to provide custom format specifiers to the standard library formatting macros.

Compiler support

Requires rustc 1.45+ when building with no default features, or rustc 1.55+ otherwise.

License

This project is licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies