#time #tz

time-tz

Implementation of tz database (IANA) for the time Rust crate

13 releases (3 stable)

1.0.2 May 22, 2022
1.0.1 Apr 14, 2022
0.6.0 Apr 10, 2022
0.5.2 Mar 11, 2022
0.1.0 Feb 7, 2022

#43 in Date and time

Download history 1474/week @ 2022-11-27 2390/week @ 2022-12-04 2589/week @ 2022-12-11 2188/week @ 2022-12-18 516/week @ 2022-12-25 425/week @ 2023-01-01 1518/week @ 2023-01-08 1673/week @ 2023-01-15 1734/week @ 2023-01-22 2333/week @ 2023-01-29 2806/week @ 2023-02-05 2083/week @ 2023-02-12 2816/week @ 2023-02-19 1523/week @ 2023-02-26 1623/week @ 2023-03-05 1251/week @ 2023-03-12

7,240 downloads per month
Used in 12 crates (10 directly)

BSD-3-Clause

595KB
8K SLoC

C 6.5K SLoC // 0.2% comments Rust 1.5K SLoC // 0.2% comments Perl 42 SLoC // 0.1% comments Shell 29 SLoC // 0.1% comments

time-tz

An implementation of the tz database for the time-rs Rust crate.

chat

This implementation is based off of chrono-tz (https://github.com/chronotope/chrono-tz) but uses time-rs instead of chrono. This is designed to replace use of chono dependency which is impacted by CVE-2020-26235 (localtime_r thread safety issue linked to std::env::set_var).

Features

  • Injects an assume_timezone member function to any PrimitiveDateTime.
  • Injects a to_timezone member function to any OffsetDateTime.
  • Provides a timezones::get_by_name function to get a timezone by name.
  • Supports finding the closest IANA match from a windows timezone name.
  • Supports obtaining system's current timezone (through the system feature).

Usage

use time::macros::datetime;
use time_tz::{PrimitiveDateTimeExt, OffsetDateTimeExt, timezones};

fn main()
{
    // ===========================================
    //  Create a new datetime in a given timezone
    // ===========================================
    
    // First we have to get the source timezone:
    let london = timezones::db::europe::LONDON;

    // Now we can create a primitive date time and call the extension function:
    let dt = datetime!(2016-10-8 17:0:0).assume_timezone_utc(london);


    // ===========================
    //  Convert to a new timezone
    // ===========================

    // First we get the target timezone:
    let berlin = timezones::db::europe::BERLIN;

    // Now we can convert (again by calling an extension function):
    let converted = dt.to_timezone(berlin);

    // ... do something with converted
}

Dependencies

~1–10MB
~170K SLoC