| 1 |
use core::mem::MaybeUninit; |
| 2 |
|
| 3 |
use alloc::string::String; |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
error::{tz::system::Error as E, Error, ErrorContext}, |
| 7 |
tz::{TimeZone, TimeZoneDatabase}, |
| 8 |
util::utf8, |
| 9 |
}; |
| 10 |
|
| 11 |
use self::windows_zones::WINDOWS_TO_IANA; |
| 12 |
|
| 13 |
#[allow(dead_code)] // we don't currently read the version |
| 14 |
mod windows_zones; |
| 15 |
|
| 16 |
mod ffi; |
| 17 |
|
| 18 |
use ffi::{ |
| 19 |
GetDynamicTimeZoneInformation, DYNAMIC_TIME_ZONE_INFORMATION, |
| 20 |
TIME_ZONE_ID_INVALID, |
| 21 |
}; |
| 22 |
|
| 23 |
/// Attempts to find the default "system" time zone. |
| 24 |
/// |
| 25 |
/// This works by querying `GetDynamicTimeZoneInformation` via the Windows |
| 26 |
/// API, and mapping the time zone key name returned to an IANA time zone |
| 27 |
/// name via the [CLDR XML data]. |
| 28 |
/// |
| 29 |
/// If the API call fails or a valid mapping could not be found, then `None` |
| 30 |
/// is returned and some log messages are emitted. |
| 31 |
/// |
| 32 |
/// Windows does provide a [WinRT GetTimeZone] call that will return the IANA |
| 33 |
/// time zone name directly, but it looks like a mess to use WinRT from Rust |
| 34 |
/// currently. And this approach enjoys wider platform support. |
| 35 |
/// |
| 36 |
/// [CLDR XML data]: https://github.com/unicode-org/cldr/raw/main/common/supplemental/windowsZones.xml |
| 37 |
/// [WinRT GetTimeZone]: https://learn.microsoft.com/en-us/uwp/api/windows.globalization.calendar.gettimezone?view=winrt-22621 |
| 38 |
pub(super) fn get(db: &TimeZoneDatabase) -> Option<TimeZone> { |
| 39 |
let tz_key_name = match get_tz_key_name() { |
| 40 |
Ok(tz_key_name) => tz_key_name, |
| 41 |
Err(_err) => { |
| 42 |
warn!( |
| 43 |
"failed to discover current time zone via \ |
| 44 |
winapi GetDynamicTimeZoneInformation: {_err}", |
| 45 |
); |
| 46 |
return None; |
| 47 |
} |
| 48 |
}; |
| 49 |
let iana_name = match windows_to_iana(&tz_key_name) { |
| 50 |
Ok(iana_name) => iana_name, |
| 51 |
Err(_err) => { |
| 52 |
warn!("could not find IANA time zone name: {_err}"); |
| 53 |
return None; |
| 54 |
} |
| 55 |
}; |
| 56 |
let tz = match db.get(iana_name) { |
| 57 |
Ok(tz) => tz, |
| 58 |
Err(_err) => { |
| 59 |
warn!( |
| 60 |
"could not find mapped IANA time zone {iana_name} \ |
| 61 |
in zoneinfo database {db:?}: {_err}", |
| 62 |
); |
| 63 |
return None; |
| 64 |
} |
| 65 |
}; |
| 66 |
Some(tz) |
| 67 |
} |
| 68 |
|
| 69 |
pub(super) fn read(_db: &TimeZoneDatabase, path: &str) -> Option<TimeZone> { |
| 70 |
match super::read_unnamed_tzif_file(path) { |
| 71 |
Ok(tz) => Some(tz), |
| 72 |
Err(_err) => { |
| 73 |
trace!("failed to read {path} as unnamed time zone: {_err}"); |
| 74 |
None |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
fn windows_to_iana(tz_key_name: &str) -> Result<&'static str, Error> { |
| 80 |
let result = WINDOWS_TO_IANA.binary_search_by(|(win_name, _)| { |
| 81 |
utf8::cmp_ignore_ascii_case(win_name, &tz_key_name) |
| 82 |
}); |
| 83 |
let Ok(index) = result else { |
| 84 |
return Err(Error::from(E::WindowsMissingIanaMapping)); |
| 85 |
}; |
| 86 |
let iana_name = WINDOWS_TO_IANA[index].1; |
| 87 |
trace!( |
| 88 |
"found Windows time zone name `{tz_key_name}`, and \ |
| 89 |
successfully mapped it to IANA time zone `{iana_name}`", |
| 90 |
); |
| 91 |
Ok(iana_name) |
| 92 |
} |
| 93 |
|
| 94 |
fn get_tz_key_name() -> Result<String, Error> { |
| 95 |
let mut info: MaybeUninit<DYNAMIC_TIME_ZONE_INFORMATION> = |
| 96 |
MaybeUninit::uninit(); |
| 97 |
// SAFETY: We pass a pointer to the expected input and signal it as |
| 98 |
// unitializaed to rustc via MaybeUninit. |
| 99 |
let rc = unsafe { GetDynamicTimeZoneInformation(info.as_mut_ptr()) }; |
| 100 |
if rc == TIME_ZONE_ID_INVALID { |
| 101 |
return Err(Error::io(std::io::Error::last_os_error())); |
| 102 |
} |
| 103 |
// SAFETY: Windows API docs indicate that the pointer is correctly written |
| 104 |
// to unless it fails, and we check for failure above. So we're only here |
| 105 |
// when `info` is properly initialized. |
| 106 |
let info = unsafe { info.assume_init() }; |
| 107 |
let tz_key_name = nul_terminated_utf16_to_string(&info.timezone_key_name) |
| 108 |
.context(E::WindowsTimeZoneKeyName)?; |
| 109 |
Ok(tz_key_name) |
| 110 |
} |
| 111 |
|
| 112 |
fn nul_terminated_utf16_to_string( |
| 113 |
code_units: &[u16], |
| 114 |
) -> Result<String, Error> { |
| 115 |
let nul = code_units |
| 116 |
.iter() |
| 117 |
.position(|&cu| cu == 0) |
| 118 |
.ok_or(E::WindowsUtf16DecodeNul)?; |
| 119 |
let string = String::from_utf16(&code_units[..nul]) |
| 120 |
.map_err(|_| E::WindowsUtf16DecodeInvalid)?; |
| 121 |
Ok(string) |
| 122 |
} |
| 123 |
|
| 124 |
#[cfg(test)] |
| 125 |
mod tests { |
| 126 |
use super::*; |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn get_time_zone_name_windows_dynamic_time_zone() { |
| 130 |
let _ = crate::logging::Logger::init(); |
| 131 |
|
| 132 |
let db = crate::tz::db(); |
| 133 |
if crate::tz::db().is_definitively_empty() { |
| 134 |
return; |
| 135 |
} |
| 136 |
let path = std::path::Path::new("/etc/localtime"); |
| 137 |
if !path.exists() { |
| 138 |
return; |
| 139 |
} |
| 140 |
// It's hard to assert much other than that a time zone could be |
| 141 |
// successfully constructed. Presumably this may fail in certain |
| 142 |
// environments, but hopefully the `is_definitively_empty` check above |
| 143 |
// will filter most out. |
| 144 |
assert!(get(db).is_some()); |
| 145 |
} |
| 146 |
} |