From ee4c09dd30075117473baa4ad2cf108e512fe05c Mon Sep 17 00:00:00 2001 From: Ben S Date: Thu, 31 Mar 2016 21:19:29 +0100 Subject: [PATCH] Use only the time zone data present on the system Thinking about it, it doesn't make sense to use an *external* time zone source when the program we want to compare it to, ls, uses the system one. So just use the system one. Also, handle the case where the time zone data file can't be loaded by showing the files in UTC rather than falling over and quitting. --- Cargo.lock | 24 -------------------- Cargo.toml | 3 --- src/main.rs | 1 - src/output/details.rs | 52 ++++++++++++++++++++++++------------------- 4 files changed, 29 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ceaa98ea..81753011 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,7 +18,6 @@ dependencies = [ "unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "users 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "zoneinfo_compiled 0.2.1 (git+https://github.com/rust-datetime/zoneinfo-compiled.git)", - "zoneinfo_data 0.1.0 (git+https://github.com/rust-datetime/zoneinfo-data.git)", ] [[package]] @@ -216,19 +215,6 @@ dependencies = [ "unicode-width 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "phf" -version = "0.7.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "phf_shared 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "phf_shared" -version = "0.7.14" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "pkg-config" version = "0.3.8" @@ -336,13 +322,3 @@ dependencies = [ "datetime 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "zoneinfo_data" -version = "0.1.0" -source = "git+https://github.com/rust-datetime/zoneinfo-data.git#6807bb6be1a444f8133dd62c3af5d1dcffb46bee" -dependencies = [ - "datetime 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", - "locale 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "phf 0.7.14 (registry+https://github.com/rust-lang/crates.io-index)", -] - diff --git a/Cargo.toml b/Cargo.toml index cbb6df07..9bac9731 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,8 +35,5 @@ lto = true version = "0.3.2" optional = true -[dependencies.zoneinfo_data] -git = "https://github.com/rust-datetime/zoneinfo-data.git" - [dependencies.zoneinfo_compiled] git = "https://github.com/rust-datetime/zoneinfo-compiled.git" diff --git a/src/main.rs b/src/main.rs index d09a034e..dc9f724b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,6 @@ extern crate term_grid; extern crate unicode_width; extern crate users; extern crate zoneinfo_compiled; -extern crate zoneinfo_data; #[cfg(feature="git")] extern crate git2; #[macro_use] extern crate lazy_static; diff --git a/src/output/details.rs b/src/output/details.rs index 6074b4ff..ed75abfa 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -83,7 +83,6 @@ use datetime::fmt::DateFormat; use datetime::{LocalDateTime, DatePiece}; use datetime::TimeZone; use zoneinfo_compiled::{CompiledData, Result as TZResult}; -use zoneinfo_data::ZoneinfoData; use locale; @@ -157,7 +156,7 @@ pub struct Environment { /// The computer's current time zone. This gets used to determine how to /// offset files' timestamps. - tz: TimeZone, + tz: Option, /// Mapping cache of user IDs to usernames. users: Mutex, @@ -165,33 +164,24 @@ pub struct Environment { impl Default for Environment { fn default() -> Self { - use std::process::exit; + let tz = determine_time_zone(); - let tz = match determine_time_zone() { - Ok(tz) => tz, - Err(e) => { - println!("Unable to determine time zone: {}", e); - exit(1); - }, - }; + if let Err(ref e) = tz { + println!("Unable to determine time zone: {}", e); + } Environment { current_year: LocalDateTime::now().year(), numeric: locale::Numeric::load_user_locale().unwrap_or_else(|_| locale::Numeric::english()), time: locale::Time::load_user_locale().unwrap_or_else(|_| locale::Time::english()), - tz: tz, + tz: tz.ok(), users: Mutex::new(UsersCache::new()), } } } fn determine_time_zone() -> TZResult { - if let Some(system_zone) = TimeZone::system() { - Ok(system_zone) - } - else { - TimeZone::from_file("/etc/localtime") - } + TimeZone::from_file("/etc/localtime") } impl Details { @@ -597,16 +587,34 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> { #[allow(trivial_numeric_casts)] fn render_time(&self, timestamp: f::Time) -> TextCell { - let date = self.env.tz.to_zoned(LocalDateTime::at(timestamp.0 as i64)); + // TODO(ogham): This method needs some serious de-duping! + // zoned and local times have different types at the moment, + // so it's tricky. + + if let Some(ref tz) = self.env.tz { + let date = tz.to_zoned(LocalDateTime::at(timestamp.0 as i64)); - let datestamp = if date.year() == self.env.current_year { + let datestamp = if date.year() == self.env.current_year { DATE_AND_TIME.format(&date, &self.env.time) } else { DATE_AND_YEAR.format(&date, &self.env.time) }; - TextCell::paint(self.opts.colours.date, datestamp) + TextCell::paint(self.opts.colours.date, datestamp) + } + else { + let date = LocalDateTime::at(timestamp.0 as i64); + + let datestamp = if date.year() == self.env.current_year { + DATE_AND_TIME.format(&date, &self.env.time) + } + else { + DATE_AND_YEAR.format(&date, &self.env.time) + }; + + TextCell::paint(self.opts.colours.date, datestamp) + } } fn render_git_status(&self, git: f::Git) -> TextCell { @@ -750,8 +758,6 @@ pub mod test { impl Default for Environment { fn default() -> Self { use locale; - use datetime::TimeZone; - use zoneinfo_data::ZoneinfoData; use users::mock::MockUsers; use std::sync::Mutex; @@ -759,7 +765,7 @@ pub mod test { current_year: 1234, numeric: locale::Numeric::english(), time: locale::Time::english(), - tz: TimeZone::get("Europe/London").unwrap(), + tz: None, users: Mutex::new(MockUsers::with_current_uid(0)), } }