Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_name to retrieve human-readable Tz names #7

Merged
merged 2 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,31 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) {
zone = zone_name,
raw_zone_name = zone).unwrap();
}
write!(timezone_file,
write!(timezone_file,
" s => Err(s.to_string())
}}
}}
}}\n\n").unwrap();

write!(timezone_file,
"impl Debug for Tz {{
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {{
"impl Tz {{
pub fn name(self: &Tz) -> &'static str {{
match *self {{\n").unwrap();
for zone in &zones {
let zone_name = convert_bad_chars(zone);
write!(timezone_file,
" Tz::{zone} => write!(f, \"{raw_zone_name}\"),\n",
" Tz::{zone} => \"{raw_zone_name}\",\n",
zone = zone_name,
raw_zone_name = zone).unwrap();
}
write!(timezone_file,
" }}
" }}
}}
}}\n\n").unwrap();
write!(timezone_file,
"impl Debug for Tz {{
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {{
write!(f, \"{{}}\", self.name())
}}
}}\n\n").unwrap();
write!(timezone_file,
Expand All @@ -110,7 +116,7 @@ fn write_timezone_file(timezone_file: &mut File, table: &Table) {
let timespans = table.timespans(&zone).unwrap();
let zone_name = convert_bad_chars(zone);
write!(
timezone_file,
timezone_file,
" Tz::{zone} => {{
const REST: &'static [(i64, FixedTimespan)] = {rest};
FixedTimespanSet {{
Expand Down
17 changes: 13 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
//! # 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();
Expand All @@ -114,7 +114,7 @@
//! # 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");
Expand All @@ -123,14 +123,14 @@
//! ```
//!
//! You can convert a timezone string to a timezone using the FromStr trait
//!
//!
//! ```
//! # extern crate chrono;
//! # extern crate chrono_tz;
//! use chrono::TimeZone;
//! use chrono_tz::Tz;
//! use chrono_tz::UTC;
//!
//!
//! # fn main() {
//! let tz: Tz = "Antarctica/South_Pole".parse().unwrap();
//! let dt = tz.ymd(2016, 10, 22).and_hms(12, 0, 0);
Expand Down Expand Up @@ -165,6 +165,7 @@ mod tests {
use super::Pacific::Noumea;
use super::Pacific::Tahiti;
use super::US::Eastern;
use super::Tz;
use chrono::{TimeZone, Duration};

#[test]
Expand Down Expand Up @@ -362,4 +363,12 @@ mod tests {
fn unambiguous_time_2() {
let _ = Moscow.ymd(2014, 10, 26).and_hms(2, 0, 0);
}

#[test]
fn test_get_name() {
assert_eq!(London.name(), "Europe/London");
assert_eq!(Tz::Africa__Abidjan.name(), "Africa/Abidjan");
assert_eq!(Tz::UTC.name(), "UTC");
assert_eq!(Tz::Zulu.name(), "Zulu");
}
}