| 1 |
//! Get the system's UTC offset on Windows. |
| 2 |
|
| 3 |
use core::mem::MaybeUninit; |
| 4 |
|
| 5 |
use num_conv::prelude::*; |
| 6 |
|
| 7 |
use crate::convert::*; |
| 8 |
use crate::{OffsetDateTime, UtcOffset}; |
| 9 |
|
| 10 |
// ffi: WINAPI FILETIME struct |
| 11 |
#[repr(C)] |
| 12 |
#[allow(non_snake_case, clippy::missing_docs_in_private_items)] |
| 13 |
struct FileTime { |
| 14 |
dwLowDateTime: u32, |
| 15 |
dwHighDateTime: u32, |
| 16 |
} |
| 17 |
|
| 18 |
// ffi: WINAPI SYSTEMTIME struct |
| 19 |
#[repr(C)] |
| 20 |
#[allow(non_snake_case, clippy::missing_docs_in_private_items)] |
| 21 |
struct SystemTime { |
| 22 |
wYear: u16, |
| 23 |
wMonth: u16, |
| 24 |
wDayOfWeek: u16, |
| 25 |
wDay: u16, |
| 26 |
wHour: u16, |
| 27 |
wMinute: u16, |
| 28 |
wSecond: u16, |
| 29 |
wMilliseconds: u16, |
| 30 |
} |
| 31 |
|
| 32 |
#[link(name = "kernel32")] |
| 33 |
extern "system" { |
| 34 |
// https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-systemtimetofiletime |
| 35 |
fn SystemTimeToFileTime(lpSystemTime: *const SystemTime, lpFileTime: *mut FileTime) -> i32; |
| 36 |
|
| 37 |
// https://docs.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-systemtimetotzspecificlocaltime |
| 38 |
fn SystemTimeToTzSpecificLocalTime( |
| 39 |
lpTimeZoneInformation: *const core::ffi::c_void, // We only pass `nullptr` here |
| 40 |
lpUniversalTime: *const SystemTime, |
| 41 |
lpLocalTime: *mut SystemTime, |
| 42 |
) -> i32; |
| 43 |
} |
| 44 |
|
| 45 |
/// Convert a `SYSTEMTIME` to a `FILETIME`. Returns `None` if any error occurred. |
| 46 |
fn systemtime_to_filetime(systime: &SystemTime) -> Option<FileTime> { |
| 47 |
let mut ft = MaybeUninit::uninit(); |
| 48 |
|
| 49 |
// Safety: `SystemTimeToFileTime` is thread-safe. |
| 50 |
if 0 == unsafe { SystemTimeToFileTime(systime, ft.as_mut_ptr()) } { |
| 51 |
// failed |
| 52 |
None |
| 53 |
} else { |
| 54 |
// Safety: The call succeeded. |
| 55 |
Some(unsafe { ft.assume_init() }) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/// Convert a `FILETIME` to an `i64`, representing a number of seconds. |
| 60 |
fn filetime_to_secs(filetime: &FileTime) -> i64 { |
| 61 |
/// FILETIME represents 100-nanosecond intervals |
| 62 |
const FT_TO_SECS: u64 = Nanosecond::per(Second) as u64 / 100; |
| 63 |
((filetime.dwHighDateTime.extend::<u64>() << 32 | filetime.dwLowDateTime.extend::<u64>()) |
| 64 |
/ FT_TO_SECS) as i64 |
| 65 |
} |
| 66 |
|
| 67 |
/// Convert an [`OffsetDateTime`] to a `SYSTEMTIME`. |
| 68 |
fn offset_to_systemtime(datetime: OffsetDateTime) -> SystemTime { |
| 69 |
let (_, month, day_of_month) = datetime.to_offset(UtcOffset::UTC).date().to_calendar_date(); |
| 70 |
SystemTime { |
| 71 |
wYear: datetime.year().cast_unsigned().truncate(), |
| 72 |
wMonth: u8::from(month).extend(), |
| 73 |
wDay: day_of_month.extend(), |
| 74 |
wDayOfWeek: 0, // ignored |
| 75 |
wHour: datetime.hour().extend(), |
| 76 |
wMinute: datetime.minute().extend(), |
| 77 |
wSecond: datetime.second().extend(), |
| 78 |
wMilliseconds: datetime.millisecond(), |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
/// Obtain the system's UTC offset. |
| 83 |
pub(super) fn local_offset_at(datetime: OffsetDateTime) -> Option<UtcOffset> { |
| 84 |
// This function falls back to UTC if any system call fails. |
| 85 |
let systime_utc = offset_to_systemtime(datetime.to_offset(UtcOffset::UTC)); |
| 86 |
|
| 87 |
// Safety: `local_time` is only read if it is properly initialized, and |
| 88 |
// `SystemTimeToTzSpecificLocalTime` is thread-safe. |
| 89 |
let systime_local = unsafe { |
| 90 |
let mut local_time = MaybeUninit::uninit(); |
| 91 |
|
| 92 |
if 0 == SystemTimeToTzSpecificLocalTime( |
| 93 |
core::ptr::null(), // use system's current timezone |
| 94 |
&systime_utc, |
| 95 |
local_time.as_mut_ptr(), |
| 96 |
) { |
| 97 |
// call failed |
| 98 |
return None; |
| 99 |
} else { |
| 100 |
local_time.assume_init() |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
// Convert SYSTEMTIMEs to FILETIMEs so we can perform arithmetic on them. |
| 105 |
let ft_system = systemtime_to_filetime(&systime_utc)?; |
| 106 |
let ft_local = systemtime_to_filetime(&systime_local)?; |
| 107 |
|
| 108 |
let diff_secs = (filetime_to_secs(&ft_local) - filetime_to_secs(&ft_system)) |
| 109 |
.try_into() |
| 110 |
.ok()?; |
| 111 |
|
| 112 |
UtcOffset::from_whole_seconds(diff_secs).ok() |
| 113 |
} |