Skip to content

Commit

Permalink
store hash of environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
esheppa authored and djc committed Nov 11, 2022
1 parent 57908e9 commit dc4287a
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/offset/local/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::{cell::RefCell, env, fs, time::SystemTime};
use std::{cell::RefCell, collections::hash_map, env, fs, hash::Hasher, time::SystemTime};

use super::tz_info::TimeZone;
use super::{DateTime, FixedOffset, Local, NaiveDateTime};
Expand All @@ -33,20 +33,18 @@ thread_local! {

enum Source {
LocalTime { mtime: SystemTime },
// we don't bother storing the contents of the environment variable in this case.
// changing the environment while the process is running is generally not reccomended
Environment,
Environment { hash: u64 },
}

impl Source {
fn new(env_tz: Option<&str>) -> Source {
// use of var_os avoids allocating, which is nice
// as we are only going to discard the string anyway
// but we must ensure the contents are valid unicode
// otherwise the behaivour here would be different
// to that in `naive_to_local`
match env_tz {
Some(_) => Source::Environment,
Some(tz) => {
let mut hasher = hash_map::DefaultHasher::new();
hasher.write(tz.as_bytes());
let hash = hasher.finish();
Source::Environment { hash }
}
None => match fs::symlink_metadata("/etc/localtime") {
Ok(data) => Source::LocalTime {
// we have to pick a sensible default when the mtime fails
Expand Down Expand Up @@ -121,14 +119,20 @@ impl Cache {

let out_of_date = match (&self.source, &new_source) {
// change from env to file or file to env, must recreate the zone
(Source::Environment, Source::LocalTime { .. })
| (Source::LocalTime { .. }, Source::Environment) => true,
(Source::Environment { .. }, Source::LocalTime { .. })
| (Source::LocalTime { .. }, Source::Environment { .. }) => true,
// stay as file, but mtime has changed
(Source::LocalTime { mtime: old_mtime }, Source::LocalTime { mtime })
if old_mtime != mtime =>
{
true
}
// stay as env, but hash of variable has changed
(Source::Environment { hash: old_hash }, Source::Environment { hash })
if old_hash != hash =>
{
true
}
// cache can be reused
_ => false,
};
Expand Down

0 comments on commit dc4287a

Please sign in to comment.