From ce183c24042516e1b33222a174e69c17c928317c Mon Sep 17 00:00:00 2001 From: Nicolas Boichat Date: Sat, 5 Apr 2025 19:36:37 +0200 Subject: [PATCH] TimeZone::from_posix_tz: Treat empty TZ variable as UTC This is not technically POSIX, but glibc and musl behave that way: e.g. `TZ= date` returns a UTC date. --- src/offset/local/tz_info/timezone.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/offset/local/tz_info/timezone.rs b/src/offset/local/tz_info/timezone.rs index 61d4498a94..9cd37f4ff1 100644 --- a/src/offset/local/tz_info/timezone.rs +++ b/src/offset/local/tz_info/timezone.rs @@ -37,8 +37,9 @@ impl TimeZone { /// Construct a time zone from a POSIX TZ string, as described in [the POSIX documentation of the `TZ` environment variable](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html). fn from_posix_tz(tz_string: &str) -> Result { + // It is commonly agreed (but not standard) that setting an empty `TZ=` uses UTC. if tz_string.is_empty() { - return Err(Error::InvalidTzString("empty TZ string")); + return Ok(Self::utc()); } if tz_string == "localtime" { @@ -925,7 +926,7 @@ mod tests { } assert!(TimeZone::from_posix_tz("EST5EDT,0/0,J365/25").is_err()); - assert!(TimeZone::from_posix_tz("").is_err()); + assert_eq!(TimeZone::from_posix_tz("").unwrap().find_local_time_type(0)?.offset(), 0); Ok(()) }