Chrono-TZ 0.2.5
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
and parse-zoneinfo
.
Documentation
Documentation is hosted on docs.rs
Usage
Put this in your Cargo.toml
:
[]
= "0.2"
= "0.2"
Then you will need to write (in your crate root):
extern crate chrono;
extern crate chrono_tz;
Examples
Create a time in one timezone and convert it to UTC
use ;
use US Pacific;
let pacific_time = Pacific.ymd.and_hms;
let utc_time = pacific_time.with_timezone;
assert_eq!;
Create a naive datetime and convert it to a timezone-aware datetime
use ;
use Johannesburg;
let naive_dt = from_ymd.and_hms;
let tz_aware = Johannesburg.from_local_datetime.unwrap;
assert_eq!;
London and New York change their clocks on different days in March so only have a 4-hour difference on certain days.
use TimeZone;
use London;
use New_York;
let london_time = London.ymd.and_hms;
let ny_time = london_time.with_timezone;
assert_eq!;
Adding 24 hours across a daylight savings change causes a change in local time
use ;
use London;
let dt = London.ymd.and_hms;
let later = dt + hours;
assert_eq!;
And of course you can always convert a local time to a unix timestamp
use TimeZone;
use Kolkata;
let dt = Kolkata.ymd.and_hms;
let timestamp = dt.timestamp;
assert_eq!;
Pretty-printing a string will use the correct abbreviation for the timezone
use TimeZone;
use London;
let dt = London.ymd.and_hms;
assert_eq!;
assert_eq!;
You can convert a timezone string to a timezone using the FromStr trait
use TimeZone;
use Tz;
use UTC;
let tz: Tz = "Antarctica/South_Pole".parse.unwrap;
let dt = tz.ymd.and_hms;
let utc = dt.with_timezone;
assert_eq!;
Future Improvements
rustc-serialize
andserde
support.- Handle leap seconds
- Handle Julian to Gregorian calendar transitions
- Load tzdata always from latest version
- Dynamic tzdata loading