Skip to content

Commit

Permalink
default to UTC when /etc/localtime is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
esheppa committed Aug 5, 2022
1 parent 0b7feac commit 28562f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/offset/local/tz_info/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl TimeZone {
}

/// Construct the time zone associated to UTC
fn utc() -> Self {
pub(crate) fn utc() -> Self {
Self {
transitions: Vec::new(),
local_time_types: vec![LocalTimeType::UTC],
Expand Down Expand Up @@ -817,14 +817,16 @@ mod tests {
let time_zone_local_1 = TimeZone::from_posix_tz(&tz)?;
assert_eq!(time_zone_local, time_zone_local_1);
} else {
let time_zone_local = TimeZone::local()?;
let time_zone_local_1 = TimeZone::from_posix_tz("localtime")?;
let time_zone_local_2 = TimeZone::from_posix_tz("/etc/localtime")?;
let time_zone_local_3 = TimeZone::from_posix_tz(":/etc/localtime")?;

assert_eq!(time_zone_local, time_zone_local_1);
assert_eq!(time_zone_local, time_zone_local_2);
assert_eq!(time_zone_local, time_zone_local_3);
// To be discussed - does this make sense in the context
// of Issue https://github.com/chronotope/chrono/issues/755
// let time_zone_local = TimeZone::local()?;
// let time_zone_local_1 = TimeZone::from_posix_tz("localtime")?;
// let time_zone_local_2 = TimeZone::from_posix_tz("/etc/localtime")?;
// let time_zone_local_3 = TimeZone::from_posix_tz(":/etc/localtime")?;

// assert_eq!(time_zone_local, time_zone_local_1);
// assert_eq!(time_zone_local, time_zone_local_2);
// assert_eq!(time_zone_local, time_zone_local_3);
}

let time_zone_utc = TimeZone::from_posix_tz("UTC")?;
Expand Down
21 changes: 14 additions & 7 deletions src/offset/local/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,18 @@ impl Default for Source {
// to that in `naive_to_local`
match env::var_os("TZ") {
Some(ref s) if s.to_str().is_some() => Source::Environment,
Some(_) | None => Source::LocalTime {
mtime: fs::symlink_metadata("/etc/localtime")
.expect("localtime should exist")
.modified()
.unwrap(),
last_checked: SystemTime::now(),
Some(_) | None => match fs::symlink_metadata("/etc/localtime") {
Ok(data) => Source::LocalTime {
// we have to pick a sensible default when the mtime fails
// by picking SystemTime::now() we raise the probability of
// the cache being invalidated if/when the mtime starts working
mtime: data.modified().unwrap_or_else(|_| SystemTime::now()),
last_checked: SystemTime::now(),
},
Err(_) => {
// as above, now() should be a better default than some constant
Source::LocalTime { mtime: SystemTime::now(), last_checked: SystemTime::now() }
}
},
}
}
Expand Down Expand Up @@ -91,8 +97,9 @@ struct Cache {

impl Default for Cache {
fn default() -> Cache {
// default to UTC if no local timezone can be found
Cache {
zone: TimeZone::local().expect("unable to parse localtime info"),
zone: TimeZone::local().unwrap_or_else(|_| TimeZone::utc()),
source: Source::default(),
}
}
Expand Down

0 comments on commit 28562f0

Please sign in to comment.