Skip to content

Commit

Permalink
Use only the time zone data present on the system
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ogham committed Mar 31, 2016
1 parent 1dd9e61 commit ee4c09d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 51 deletions.
24 changes: 0 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
52 changes: 29 additions & 23 deletions src/output/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -157,41 +156,32 @@ 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>,
}

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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -750,16 +758,14 @@ 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;

Environment {
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)),
}
}
Expand Down

0 comments on commit ee4c09d

Please sign in to comment.