Make TZ env var override time_zone config option

This commit is contained in:
Joscha 2024-01-03 17:02:57 +01:00
parent f94eb00dcd
commit a8570cf9c5
2 changed files with 10 additions and 10 deletions

View file

@ -96,9 +96,8 @@ pub struct Config {
/// valid TZ strings:
/// https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
///
/// If the option is not specified, cove uses the contents of the `TZ`
/// environment variable instead. If the `TZ` environment variable does not
/// exist, cove uses the system's local time zone.
/// If the `TZ` environment variable exists, it overrides this option. If
/// neither exist, cove uses the system's local time zone.
///
/// **Warning:** On Windows, cove can't get the local time zone and uses UTC
/// instead. However, you can still specify a path to a tz data file or a

View file

@ -18,20 +18,21 @@ impl<T> InfallibleExt for Result<T, Infallible> {
}
}
/// Load a [`TimeZone`] specified by a string, or by the `TZ` environment
/// variable if no string is provided.
/// Load a [`TimeZone`] specified by the `TZ` environment varible, or by the
/// provided string if the environment variable does not exist.
///
/// If a string is provided, it is interpreted in the same format that the `TZ`
/// environment variable uses.
///
/// If no string and no `TZ` environment variable could be found, the system
/// local time is used.
/// If no `TZ` environment variable could be found and no string is provided,
/// the system local time (or UTC on Windows) is used.
pub fn load_time_zone(tz_string: Option<&str>) -> Result<TimeZone, TzError> {
let env_tz = env::var("TZ").ok();
let tz_string = tz_string.or(env_tz.as_ref().map(|s| s as &str));
let env_string = env::var("TZ").ok();
let tz_string = env_string.as_ref().map(|s| s as &str).or(tz_string);
match &tz_string {
// At the moment, TimeZone::from_posix_tz does not support "localtime" on windows, but other time zon
// At the moment, TimeZone::from_posix_tz does not support "localtime"
// on Windows, so we handle that case manually.
Some("localtime") | None => TimeZone::local(),
Some(tz_string) => TimeZone::from_posix_tz(tz_string),
}