Skip to content

Commit ee4c09d

Browse files
committed
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.
1 parent 1dd9e61 commit ee4c09d

File tree

4 files changed

+29
-51
lines changed

4 files changed

+29
-51
lines changed

Cargo.lock

Lines changed: 0 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,5 @@ lto = true
3535
version = "0.3.2"
3636
optional = true
3737

38-
[dependencies.zoneinfo_data]
39-
git = "https://github.com/rust-datetime/zoneinfo-data.git"
40-
4138
[dependencies.zoneinfo_compiled]
4239
git = "https://github.com/rust-datetime/zoneinfo-compiled.git"

src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ extern crate term_grid;
1515
extern crate unicode_width;
1616
extern crate users;
1717
extern crate zoneinfo_compiled;
18-
extern crate zoneinfo_data;
1918

2019
#[cfg(feature="git")] extern crate git2;
2120
#[macro_use] extern crate lazy_static;

src/output/details.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ use datetime::fmt::DateFormat;
8383
use datetime::{LocalDateTime, DatePiece};
8484
use datetime::TimeZone;
8585
use zoneinfo_compiled::{CompiledData, Result as TZResult};
86-
use zoneinfo_data::ZoneinfoData;
8786

8887
use locale;
8988

@@ -157,41 +156,32 @@ pub struct Environment<U: Users+Groups> {
157156

158157
/// The computer's current time zone. This gets used to determine how to
159158
/// offset files' timestamps.
160-
tz: TimeZone,
159+
tz: Option<TimeZone>,
161160

162161
/// Mapping cache of user IDs to usernames.
163162
users: Mutex<U>,
164163
}
165164

166165
impl Default for Environment<UsersCache> {
167166
fn default() -> Self {
168-
use std::process::exit;
167+
let tz = determine_time_zone();
169168

170-
let tz = match determine_time_zone() {
171-
Ok(tz) => tz,
172-
Err(e) => {
173-
println!("Unable to determine time zone: {}", e);
174-
exit(1);
175-
},
176-
};
169+
if let Err(ref e) = tz {
170+
println!("Unable to determine time zone: {}", e);
171+
}
177172

178173
Environment {
179174
current_year: LocalDateTime::now().year(),
180175
numeric: locale::Numeric::load_user_locale().unwrap_or_else(|_| locale::Numeric::english()),
181176
time: locale::Time::load_user_locale().unwrap_or_else(|_| locale::Time::english()),
182-
tz: tz,
177+
tz: tz.ok(),
183178
users: Mutex::new(UsersCache::new()),
184179
}
185180
}
186181
}
187182

188183
fn determine_time_zone() -> TZResult<TimeZone> {
189-
if let Some(system_zone) = TimeZone::system() {
190-
Ok(system_zone)
191-
}
192-
else {
193-
TimeZone::from_file("/etc/localtime")
194-
}
184+
TimeZone::from_file("/etc/localtime")
195185
}
196186

197187
impl Details {
@@ -597,16 +587,34 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
597587

598588
#[allow(trivial_numeric_casts)]
599589
fn render_time(&self, timestamp: f::Time) -> TextCell {
600-
let date = self.env.tz.to_zoned(LocalDateTime::at(timestamp.0 as i64));
590+
// TODO(ogham): This method needs some serious de-duping!
591+
// zoned and local times have different types at the moment,
592+
// so it's tricky.
593+
594+
if let Some(ref tz) = self.env.tz {
595+
let date = tz.to_zoned(LocalDateTime::at(timestamp.0 as i64));
601596

602-
let datestamp = if date.year() == self.env.current_year {
597+
let datestamp = if date.year() == self.env.current_year {
603598
DATE_AND_TIME.format(&date, &self.env.time)
604599
}
605600
else {
606601
DATE_AND_YEAR.format(&date, &self.env.time)
607602
};
608603

609-
TextCell::paint(self.opts.colours.date, datestamp)
604+
TextCell::paint(self.opts.colours.date, datestamp)
605+
}
606+
else {
607+
let date = LocalDateTime::at(timestamp.0 as i64);
608+
609+
let datestamp = if date.year() == self.env.current_year {
610+
DATE_AND_TIME.format(&date, &self.env.time)
611+
}
612+
else {
613+
DATE_AND_YEAR.format(&date, &self.env.time)
614+
};
615+
616+
TextCell::paint(self.opts.colours.date, datestamp)
617+
}
610618
}
611619

612620
fn render_git_status(&self, git: f::Git) -> TextCell {
@@ -750,16 +758,14 @@ pub mod test {
750758
impl Default for Environment<MockUsers> {
751759
fn default() -> Self {
752760
use locale;
753-
use datetime::TimeZone;
754-
use zoneinfo_data::ZoneinfoData;
755761
use users::mock::MockUsers;
756762
use std::sync::Mutex;
757763

758764
Environment {
759765
current_year: 1234,
760766
numeric: locale::Numeric::english(),
761767
time: locale::Time::english(),
762-
tz: TimeZone::get("Europe/London").unwrap(),
768+
tz: None,
763769
users: Mutex::new(MockUsers::with_current_uid(0)),
764770
}
765771
}

0 commit comments

Comments
 (0)