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.
This commit is contained in:
Ben S 2016-03-31 21:19:29 +01:00
parent 1dd9e6153b
commit ee4c09dd30
4 changed files with 29 additions and 51 deletions

24
Cargo.lock generated
View File

@ -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)",
]

View File

@ -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"

View File

@ -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;

View File

@ -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<U: Users+Groups> {
/// The computer's current time zone. This gets used to determine how to
/// offset files' timestamps.
tz: TimeZone,
tz: Option<TimeZone>,
/// Mapping cache of user IDs to usernames.
users: Mutex<U>,
@ -165,33 +164,24 @@ pub struct Environment<U: Users+Groups> {
impl Default for Environment<UsersCache> {
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<TimeZone> {
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.
let datestamp = if date.year() == self.env.current_year {
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 {
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<MockUsers> {
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)),
}
}