diff options
author | Vincent Breitmoser <look@my.amazin.horse> | 2019-01-21 02:04:57 +0100 |
---|---|---|
committer | Vincent Breitmoser <look@my.amazin.horse> | 2019-01-21 02:19:44 +0100 |
commit | a129d318c3e2b26f453ba16d3f408ef3436354bf (patch) | |
tree | fef3c832d3066ecf6afa36ae9777680f39aed616 /src/utils | |
parent | ff8458be910f1a6fe53b5026534bb3efdd259ab6 (diff) |
more work on icaltime and icaltimezone
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/dateutil.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/utils/dateutil.rs b/src/utils/dateutil.rs index f76b9c1..c07b602 100644 --- a/src/utils/dateutil.rs +++ b/src/utils/dateutil.rs @@ -1,5 +1,9 @@ use chrono::*; +use std::path::PathBuf; +use std::env; +use utils::fileutil; + pub fn date_from_str(date_str: &str) -> ParseResult<Date<Local>> { if date_str == "today" || date_str == "now" { return Ok(Local::now().date()); @@ -30,6 +34,19 @@ pub fn week_from_str_begin(date_str: &str) -> Result<Date<Local>,String> { Err("Could not parse '{}' as week".to_string()) } +pub fn find_local_timezone() -> String { + if let Ok(candidate) = env::var("TZ") { + return candidate; + } + if let Ok(candidate) = fileutil::read_file_to_string(&PathBuf::from("/etc/timezone")) { + return candidate; + } + if let Ok(candidate) = fileutil::read_file_to_string(&PathBuf::from("/etc/localtime")) { + return candidate; + } + "UTC".to_owned() +} + #[cfg(not(test))] pub fn now() -> DateTime<Utc> { Utc::now() @@ -66,6 +83,8 @@ pub fn datetime_from_timestamp(timestamp: &str) -> Option<DateTime<Local>> { mod tests { use super::*; + use testdata; + #[test] fn test_date_from_str() { let date = date_from_str("2018-12-10").unwrap(); @@ -128,4 +147,12 @@ mod tests { let dt = Utc.ymd(2019, 01, 11).and_hms(19, 24, 47); assert_eq!(dt, dt_from_ts); } + + #[test] + fn test_find_local_timezone() { + testdata::setup(); + + let tz_name = find_local_timezone(); + assert_eq!("Europe/Berlin", tz_name); + } } |