1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! # Chrono-TZ 0.1.0
//!
//! `Chrono-TZ` is a library that provides implementors of the
//! [`TimeZone`][timezone] trait for [`rust-chrono`][chrono]. The
//! impls are generated by a build script using the [`IANA database`][iana].
//!
//! [chrono]: https://github.com/lifthrasiir/rust-chrono
//! [timezone]: ../chrono/offset/trait.TimeZone.html
//! [iana]: http://www.iana.org/time-zones
//!
//! ## Usage
//!
//! 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):
//!
//! ```
//! extern crate chrono;
//! extern crate chrono_tz;
//! ```
//!
//! ## Examples
//!
//! Create a time in one timezone and convert it to UTC
//!
//! ```
//! # extern crate chrono;
//! # extern crate chrono_tz;
//! use chrono::{TimeZone, UTC};
//! use chrono_tz::US::Pacific;
//!
//! # fn main() {
//! let pacific_time = Pacific.ymd(1990, 5, 6).and_hms(12, 30, 45);
//! let utc_time = pacific_time.with_timezone(&UTC);
//! assert_eq!(utc_time, UTC.ymd(1990, 5, 6).and_hms(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.
//!
//! ```
//! # extern crate chrono;
//! # extern crate chrono_tz;
//! use chrono::TimeZone;
//! use chrono_tz::Europe::London;
//! use chrono_tz::America::New_York;
//!
//! # fn main() {
//! let london_time = London.ymd(2016, 3, 18).and_hms(3, 0, 0);
//! let ny_time = london_time.with_timezone(&New_York);
//! assert_eq!(ny_time, New_York.ymd(2016, 3, 17).and_hms(23, 0, 0));
//! # }
//! ```
//!
//! Adding 24 hours across a daylight savings change causes a change
//! in local time
//!
//! ```
//! # extern crate chrono;
//! # extern crate chrono_tz;
//! use chrono::{TimeZone, Duration};
//! use chrono_tz::Europe::London;
//!
//! # fn main() {
//! let dt = London.ymd(2016, 10, 29).and_hms(12, 0, 0);
//! let later = dt + Duration::hours(24);
//! assert_eq!(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
//!
//! ```
//! # extern crate chrono;
//! # extern crate chrono_tz;
//! use chrono::TimeZone;
//! use chrono_tz::Asia::Kolkata;
//! 
//! # fn main() {
//! let dt = Kolkata.ymd(2000, 1, 1).and_hms(0, 0, 0);
//! let timestamp = dt.timestamp();
//! assert_eq!(timestamp, 946665000);
//! # }
//! ```
//!
//! Pretty-printing a string will use the correct abbreviation for the timezone
//!
//! ```
//! # extern crate chrono;
//! # extern crate chrono_tz;
//! use chrono::TimeZone;
//! use chrono_tz::Europe::London;
//! 
//! # fn main() {
//! let dt = London.ymd(2016, 5, 10).and_hms(12, 0, 0);
//! assert_eq!(dt.to_string(), "2016-05-10 12:00:00 BST");
//! assert_eq!(dt.to_rfc3339(), "2016-05-10T12:00:00+01:00");
//! # }
//! ```

extern crate chrono;

mod timezone_impl;
mod timezones;
mod directory;

pub use directory::*;

#[cfg(test)]
mod tests {
    use super::Europe::London;
    use super::Europe::Berlin;
    use chrono::{TimeZone, Duration};

    #[test]
    fn london_to_berlin() {
        let dt = London.ymd(2016, 10, 8).and_hms(17, 0, 0);
        let converted = dt.with_timezone(&Berlin);
        let expected = Berlin.ymd(2016, 10, 8).and_hms(18, 0, 0);
        assert_eq!(converted, expected);
    }

    #[test]
    fn london_dst() {
        let dt = London.ymd(2016, 3, 10).and_hms(5, 0, 0);
        let later = dt + Duration::days(180);
        let expected = London.ymd(2016, 9, 6).and_hms(6, 0, 0);
        assert_eq!(later, expected);
    }

    #[test]
    #[should_panic]
    fn nonexistent_time() {
        let _ = London.ymd(2016, 3, 27).and_hms(1, 30, 0);
    }

    #[test]
    fn time_exists() {
        let _ = London.ymd(2016, 3, 27).and_hms(1, 0, 0);
    }

    #[test]
    fn time_exists_2() {
        let _ = London.ymd(2016, 3, 27).and_hms(2, 0, 0);
    }

    #[test]
    #[should_panic]
    fn ambiguous_time() {
        let _ = London.ymd(2016, 10, 30).and_hms(1, 0, 0);
    }

    #[test]
    #[should_panic]
    fn ambiguous_time_2() {
        let _ = London.ymd(2016, 10, 30).and_hms(2, 0, 0);
    }
}