Chrono-TZ
is a library that provides implementors of the
TimeZone
trait for rust-chrono
. The
impls are generated by a build script using the IANA database
.
Put this in your Cargo.toml
:
toml
[dependencies]
chrono = "0.2"
chrono-tz = "0.1"
Then you will need to write (in your crate root):
rust
extern crate chrono;
extern crate chrono_tz;
Create a time in one timezone and convert it to UTC
```rust use chrono::{TimeZone, UTC}; use chrono_tz::US::Pacific;
let pacifictime = Pacific.ymd(1990, 5, 6).andhms(12, 30, 45); let utctime = pacifictime.withtimezone(&UTC); asserteq!(utctime, UTC.ymd(1990, 5, 6).andhms(19, 30, 45)); ```
London and New York change their clocks on different days in March so only have a 4-hour difference on certain days.
```rust use chrono::TimeZone; use chronotz::Europe::London; use chronotz::America::New_York;
let londontime = London.ymd(2016, 3, 18).andhms(3, 0, 0); let nytime = londontime.withtimezone(&NewYork); asserteq!(nytime, NewYork.ymd(2016, 3, 17).andhms(23, 0, 0)); ```
Adding 24 hours across a daylight savings change causes a change in local time
```rust use chrono::{TimeZone, Duration}; use chrono_tz::Europe::London;
let dt = London.ymd(2016, 10, 29).andhms(12, 0, 0); let later = dt + Duration::hours(24); asserteq!(later, London.ymd(2016, 10, 30).and_hms(11, 0, 0)); ```
And of course you can always convert a local time to a unix timestamp
```rust use chrono::TimeZone; use chrono_tz::Asia::Kolkata;
let dt = Kolkata.ymd(2000, 1, 1).andhms(0, 0, 0); let timestamp = dt.timestamp(); asserteq!(timestamp, 946665000); ```
Pretty-printing a string will use the correct abbreviation for the timezone
```rust use chrono::TimeZone; use chrono_tz::Europe::London;
let dt = London.ymd(2016, 5, 10).andhms(12, 0, 0); asserteq!(dt.tostring(), "2016-05-10 12:00:00 BST"); asserteq!(dt.to_rfc3339(), "2016-05-10T12:00:00+01:00"); ```
The timezone info for Dushanbe is not parsed correctly by zoneinfo_parse
and so I have commented out a line from that file to work around it.
```
Zone Asia/Dushanbe 4:35:12 - LMT 1924 May 2 5:00 - +05 1930 Jun 21 6:00 RussiaAsia +06/+07 1991 Mar 31 2:00s
5:00 - +05
```
Strings cannot be parsed into appropriate datetimes currently, they can only be printed out. Chrono handles fixed offsets only for parsing.
Currently no rustc-serialize or serde support.