Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for Windows local time prior to 1601 #1377

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/datetime/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,17 @@ fn test_datetime_from_timestamp_millis() {
);
}

#[test]
#[cfg(feature = "clock")]
fn test_datetime_before_windows_api_limits() {
// dt corresponds to `FILETIME = 147221225472` from issue 651.
// This used to fail on Windows for timezones with an offset of -5:00 or greater.
// The API limits years to 1601..=30827.
let dt = NaiveDate::from_ymd_opt(1601, 1, 1).unwrap().and_hms_milli_opt(4, 5, 22, 122).unwrap();
let local_dt = Local.from_utc_datetime(&dt);
dbg!(local_dt);
}

#[test]
#[cfg(feature = "clock")]
fn test_years_elapsed() {
Expand Down
2 changes: 2 additions & 0 deletions src/offset/local/win_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
::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 const ERROR_INVALID_PARAMETER: WIN32_ERROR = 87u32;
#[repr(C)]
pub struct FILETIME {
pub dwLowDateTime: u32,
Expand Down Expand Up @@ -49,3 +50,4 @@ impl ::core::clone::Clone for TIME_ZONE_INFORMATION {
*self
}
}
pub type WIN32_ERROR = u32;
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.Foundation.ERROR_INVALID_PARAMETER
Windows.Win32.System.Time.SystemTimeToFileTime
Windows.Win32.System.Time.SystemTimeToTzSpecificLocalTime
Windows.Win32.System.Time.TzSpecificLocalTimeToSystemTime
26 changes: 23 additions & 3 deletions src/offset/local/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::result::Result;

use super::win_bindings::{
SystemTimeToFileTime, SystemTimeToTzSpecificLocalTime, TzSpecificLocalTimeToSystemTime,
FILETIME, SYSTEMTIME,
ERROR_INVALID_PARAMETER, FILETIME, SYSTEMTIME,
};

use super::FixedOffset;
Expand Down Expand Up @@ -61,8 +61,28 @@ pub(super) fn offset(d: &NaiveDateTime, local: bool) -> LocalResult<FixedOffset>
LocalResult::None
}

fn from_utc_time(utc_time: SYSTEMTIME) -> Result<FixedOffset, Error> {
let local_time = utc_to_local_time(&utc_time)?;
fn from_utc_time(mut utc_time: SYSTEMTIME) -> Result<FixedOffset, Error> {
let local_time = match utc_to_local_time(&utc_time) {
Ok(time) => time,
// Workaround for #1364
// If converting to local time fails with `ERROR_INVALID_PARAMETER`
// and the date is 1601-01-01 then try again with the year set to 1602.
// This is usually a bad idea as offsets can be different depending on the year.
// However, 1601 is long enough ago that dynamic timezones aren't an issue
// and timezone offsets are much less than a year.
Err(e) => {
if e.raw_os_error() == Some(ERROR_INVALID_PARAMETER as i32)
&& utc_time.wYear == 1601
&& utc_time.wMonth == 1
&& utc_time.wDay == 1
{
utc_time.wYear = 1602;
return from_utc_time(utc_time);
} else {
return Err(e);
}
}
};
let utc_secs = system_time_as_unix_seconds(&utc_time)?;
let local_secs = system_time_as_unix_seconds(&local_time)?;
let offset = (local_secs - utc_secs) as i32;
Expand Down