Skip to content

Commit

Permalink
Windows: rewrite using GetTimeZoneInformationForYear
Browse files Browse the repository at this point in the history
And add test against previous implementation
  • Loading branch information
pitdicker committed Feb 10, 2024
1 parent 5fb4fab commit acb693a
Show file tree
Hide file tree
Showing 5 changed files with 526 additions and 106 deletions.
17 changes: 17 additions & 0 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,23 @@ impl NaiveDateTime {
NaiveDateTime { date, time }
}

/// Subtracts given `FixedOffset` from the current datetime.
/// The resulting value may be outside the valid range of [`NaiveDateTime`].
///
/// This can be useful for intermediate values, but the resulting out-of-range `NaiveDate`
/// should not be exposed to library users.
#[must_use]
#[allow(unused)] // currently only used in `Local` but not on all platforms
pub(crate) fn overflowing_sub_offset(self, rhs: FixedOffset) -> NaiveDateTime {
let (time, days) = self.time.overflowing_sub_offset(rhs);
let date = match days {
-1 => self.date.pred_opt().unwrap_or(NaiveDate::BEFORE_MIN),
1 => self.date.succ_opt().unwrap_or(NaiveDate::AFTER_MAX),
_ => self.date,
};
NaiveDateTime { date, time }
}

/// Subtracts given `TimeDelta` from the current date and time.
///
/// As a part of Chrono's [leap second handling](./struct.NaiveTime.html#leap-second-handling),
Expand Down
259 changes: 259 additions & 0 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

//! The local (system) time zone.

#[cfg(windows)]
use std::cmp::Ordering;

#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
use rkyv::{Archive, Deserialize, Serialize};

Expand Down Expand Up @@ -183,11 +186,96 @@ impl TimeZone for Local {
}
}

#[cfg(windows)]
#[derive(Copy, Clone, Eq, PartialEq)]
struct Transition {
transition_utc: NaiveDateTime,
offset_before: FixedOffset,
offset_after: FixedOffset,
}

#[cfg(windows)]
impl Transition {
fn new(
transition_local: NaiveDateTime,
offset_before: FixedOffset,
offset_after: FixedOffset,
) -> Transition {
// It is no problem if the transition time in UTC falls a couple of hours inside the buffer
// space around the `NaiveDateTime` range (although it is very theoretical to have a
// transition at midnight around `NaiveDate::(MIN|MAX)`.
let transition_utc = transition_local.overflowing_sub_offset(offset_before);
Transition { transition_utc, offset_before, offset_after }
}
}

#[cfg(windows)]
impl PartialOrd for Transition {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.transition_utc.cmp(&other.transition_utc))
}
}

#[cfg(windows)]
impl Ord for Transition {
fn cmp(&self, other: &Self) -> Ordering {
self.transition_utc.cmp(&other.transition_utc)
}
}

// Calculate the time in UTC given a local time and transitions.
// `transitions` must be sorted.
#[cfg(windows)]
fn lookup_with_dst_transitions(
transitions: &[Transition],
dt: NaiveDateTime,
) -> LocalResult<FixedOffset> {
for t in transitions.iter() {
// A transition can result in the wall clock time going forward (creating a gap) or going
// backward (creating a fold). We are interested in the earliest and latest wall time of the
// transition, as this are the times between which `dt` does may not exist or is ambiguous.
//
// It is no problem if the transition times falls a couple of hours inside the buffer
// space around the `NaiveDateTime` range (although it is very theoretical to have a
// transition at midnight around `NaiveDate::(MIN|MAX)`.
let (offset_min, offset_max) =
match t.offset_after.local_minus_utc() > t.offset_before.local_minus_utc() {
true => (t.offset_before, t.offset_after),
false => (t.offset_after, t.offset_before),
};
let wall_earliest = t.transition_utc.overflowing_add_offset(offset_min);
let wall_latest = t.transition_utc.overflowing_add_offset(offset_max);

if dt < wall_earliest {
return LocalResult::Single(t.offset_before);
} else if dt <= wall_latest {
return match t.offset_after.local_minus_utc().cmp(&t.offset_before.local_minus_utc()) {
Ordering::Equal => LocalResult::Single(t.offset_before),
Ordering::Less => LocalResult::Ambiguous(t.offset_before, t.offset_after),
Ordering::Greater => {
if dt == wall_earliest {
LocalResult::Single(t.offset_before)
} else if dt == wall_latest {
LocalResult::Single(t.offset_after)
} else {
LocalResult::None
}
}
};
}
}
LocalResult::Single(transitions.last().unwrap().offset_after)
}

#[cfg(test)]
mod tests {
use super::Local;
#[cfg(windows)]
use crate::offset::local::{lookup_with_dst_transitions, Transition};
use crate::offset::TimeZone;
use crate::{Datelike, TimeDelta, Utc};
#[cfg(windows)]
use crate::{FixedOffset, LocalResult, NaiveDate, NaiveDateTime};

#[test]
fn verify_correct_offsets() {
Expand Down Expand Up @@ -264,6 +352,177 @@ mod tests {
}
}

#[test]
#[cfg(windows)]
fn test_lookup_with_dst_transitions() {
let ymdhms = |y, m, d, h, n, s| {
NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap()
};

#[track_caller]
#[allow(clippy::too_many_arguments)]
fn compare_lookup(
transitions: &[Transition],
y: i32,
m: u32,
d: u32,
h: u32,
n: u32,
s: u32,
result: LocalResult<FixedOffset>,
) {
let dt = NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
assert_eq!(lookup_with_dst_transitions(transitions, dt), result);
}

// dst transition before std transition
// dst offset > std offset
let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap();
let transitions = [
Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, dst),
Transition::new(ymdhms(2023, 10, 29, 3, 0, 0), dst, std),
];
compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, LocalResult::None);
compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 3, 26, 4, 0, 0, LocalResult::Single(dst));

compare_lookup(&transitions, 2023, 10, 29, 1, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 10, 29, 2, 0, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&transitions, 2023, 10, 29, 2, 30, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&transitions, 2023, 10, 29, 4, 0, 0, LocalResult::Single(std));

// std transition before dst transition
// dst offset > std offset
let std = FixedOffset::east_opt(-5 * 60 * 60).unwrap();
let dst = FixedOffset::east_opt(-4 * 60 * 60).unwrap();
let transitions = [
Transition::new(ymdhms(2023, 3, 24, 3, 0, 0), dst, std),
Transition::new(ymdhms(2023, 10, 27, 2, 0, 0), std, dst),
];
compare_lookup(&transitions, 2023, 3, 24, 1, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 3, 24, 2, 0, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&transitions, 2023, 3, 24, 2, 30, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&transitions, 2023, 3, 24, 3, 0, 0, LocalResult::Ambiguous(dst, std));
compare_lookup(&transitions, 2023, 3, 24, 4, 0, 0, LocalResult::Single(std));

compare_lookup(&transitions, 2023, 10, 27, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 10, 27, 2, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 10, 27, 2, 30, 0, LocalResult::None);
compare_lookup(&transitions, 2023, 10, 27, 3, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 10, 27, 4, 0, 0, LocalResult::Single(dst));

// dst transition before std transition
// dst offset < std offset
let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
let dst = FixedOffset::east_opt((2 * 60 + 30) * 60).unwrap();
let transitions = [
Transition::new(ymdhms(2023, 3, 26, 2, 30, 0), std, dst),
Transition::new(ymdhms(2023, 10, 29, 2, 0, 0), dst, std),
];
compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&transitions, 2023, 3, 26, 2, 15, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, LocalResult::Single(dst));

compare_lookup(&transitions, 2023, 10, 29, 1, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 10, 29, 2, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 10, 29, 2, 15, 0, LocalResult::None);
compare_lookup(&transitions, 2023, 10, 29, 2, 30, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, LocalResult::Single(std));

// std transition before dst transition
// dst offset < std offset
let std = FixedOffset::east_opt(-(4 * 60 + 30) * 60).unwrap();
let dst = FixedOffset::east_opt(-5 * 60 * 60).unwrap();
let transitions = [
Transition::new(ymdhms(2023, 3, 24, 2, 0, 0), dst, std),
Transition::new(ymdhms(2023, 10, 27, 2, 30, 0), std, dst),
];
compare_lookup(&transitions, 2023, 3, 24, 1, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 3, 24, 2, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 3, 24, 2, 15, 0, LocalResult::None);
compare_lookup(&transitions, 2023, 3, 24, 2, 30, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 3, 24, 3, 0, 0, LocalResult::Single(std));

compare_lookup(&transitions, 2023, 10, 27, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 10, 27, 2, 0, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&transitions, 2023, 10, 27, 2, 15, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&transitions, 2023, 10, 27, 2, 30, 0, LocalResult::Ambiguous(std, dst));
compare_lookup(&transitions, 2023, 10, 27, 3, 0, 0, LocalResult::Single(dst));

// offset stays the same
let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
let transitions = [
Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, std),
Transition::new(ymdhms(2023, 10, 29, 3, 0, 0), std, std),
];
compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, LocalResult::Single(std));

// single transition
let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap();
let transitions = [Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, dst)];
compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, LocalResult::Single(std));
compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, LocalResult::None);
compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, LocalResult::Single(dst));
compare_lookup(&transitions, 2023, 3, 26, 4, 0, 0, LocalResult::Single(dst));
}

#[test]
#[cfg(windows)]
fn test_lookup_with_dst_transitions_limits() {
// Transition beyond UTC year end doesn't panic in year of `NaiveDate::MAX`
let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap();
let transitions = [
Transition::new(NaiveDateTime::MAX.with_month(7).unwrap(), std, dst),
Transition::new(NaiveDateTime::MAX, dst, std),
];
assert_eq!(
lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX.with_month(3).unwrap()),
LocalResult::Single(std)
);
assert_eq!(
lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX.with_month(8).unwrap()),
LocalResult::Single(dst)
);
// Doesn't panic with `NaiveDateTime::MAX` as argument (which would be out of range when
// converted to UTC).
assert_eq!(
lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX),
LocalResult::Ambiguous(dst, std)
);

// Transition before UTC year end doesn't panic in year of `NaiveDate::MIN`
let std = FixedOffset::west_opt(3 * 60 * 60).unwrap();
let dst = FixedOffset::west_opt(4 * 60 * 60).unwrap();
let transitions = [
Transition::new(NaiveDateTime::MIN, std, dst),
Transition::new(NaiveDateTime::MIN.with_month(6).unwrap(), dst, std),
];
assert_eq!(
lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN.with_month(3).unwrap()),
LocalResult::Single(dst)
);
assert_eq!(
lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN.with_month(8).unwrap()),
LocalResult::Single(std)
);
// Doesn't panic with `NaiveDateTime::MIN` as argument (which would be out of range when
// converted to UTC).
assert_eq!(
lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN),
LocalResult::Ambiguous(std, dst)
);
}

#[test]
#[cfg(feature = "rkyv-validation")]
fn test_rkyv_validation() {
Expand Down
20 changes: 20 additions & 0 deletions src/offset/local/win_bindings.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
// Bindings generated by `windows-bindgen` 0.52.0

#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]
::windows_targets::link!("kernel32.dll" "system" fn GetTimeZoneInformationForYear(wyear : u16, pdtzi : *const DYNAMIC_TIME_ZONE_INFORMATION, ptzi : *mut TIME_ZONE_INFORMATION) -> BOOL);
::windows_targets::link!("kernel32.dll" "system" fn SystemTimeToFileTime(lpsystemtime : *const SYSTEMTIME, lpfiletime : *mut FILETIME) -> BOOL);
::windows_targets::link!("kernel32.dll" "system" fn SystemTimeToTzSpecificLocalTime(lptimezoneinformation : *const TIME_ZONE_INFORMATION, lpuniversaltime : *const SYSTEMTIME, lplocaltime : *mut SYSTEMTIME) -> BOOL);
::windows_targets::link!("kernel32.dll" "system" fn TzSpecificLocalTimeToSystemTime(lptimezoneinformation : *const TIME_ZONE_INFORMATION, lplocaltime : *const SYSTEMTIME, lpuniversaltime : *mut SYSTEMTIME) -> BOOL);
pub type BOOL = i32;
pub type BOOLEAN = u8;
#[repr(C)]
pub struct DYNAMIC_TIME_ZONE_INFORMATION {
pub Bias: i32,
pub StandardName: [u16; 32],
pub StandardDate: SYSTEMTIME,
pub StandardBias: i32,
pub DaylightName: [u16; 32],
pub DaylightDate: SYSTEMTIME,
pub DaylightBias: i32,
pub TimeZoneKeyName: [u16; 128],
pub DynamicDaylightTimeDisabled: BOOLEAN,
}
impl ::core::marker::Copy for DYNAMIC_TIME_ZONE_INFORMATION {}
impl ::core::clone::Clone for DYNAMIC_TIME_ZONE_INFORMATION {
fn clone(&self) -> Self {
*self
}
}
#[repr(C)]
pub struct FILETIME {
pub dwLowDateTime: u32,
Expand Down
1 change: 1 addition & 0 deletions src/offset/local/win_bindings.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
--out src/offset/local/win_bindings.rs
--config flatten sys
--filter
Windows.Win32.System.Time.GetTimeZoneInformationForYear
Windows.Win32.System.Time.SystemTimeToFileTime
Windows.Win32.System.Time.SystemTimeToTzSpecificLocalTime
Windows.Win32.System.Time.TzSpecificLocalTimeToSystemTime
Loading

0 comments on commit acb693a

Please sign in to comment.