From 1bfcb235ba4be378c102b11f95f4afbd47aa44ed Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Mon, 17 Feb 2025 21:15:58 -0800 Subject: [PATCH 1/6] function to retrieve tz info on windows Signed-off-by: Ashwin Naren --- vm/Cargo.toml | 1 + vm/src/stdlib/time.rs | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index 6eaca281f86..b461104cb27 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -115,6 +115,7 @@ features = [ "Win32_Foundation", "Win32_System_LibraryLoader", "Win32_System_Threading", + "Win32_System_Time", "Win32_UI_Shell", ] diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 566650d0f2f..e6581ce8473 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -152,6 +152,17 @@ mod decl { Ok(get_perf_time(vm)?.as_nanos()) } + #[cfg(target_env = "msvc")] + #[cfg(not(target_arch = "wasm32"))] + fn get_tz_info() -> Time::TIME_ZONE_INFORMATION { + let mut info = Time::TIME_ZONE_INFORMATION::default(); + let info_ptr = &mut info as *mut Time::TIME_ZONE_INFORMATION; + let _ = unsafe { + Time::GetTimeZoneInformation(info_ptr) + }; + info + } + // #[pyfunction] // fn tzset() { // unsafe { super::_tzset() }; From 42e4292af11f3e29839103ed88b69380737a7249 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Mon, 17 Feb 2025 21:16:17 -0800 Subject: [PATCH 2/6] tzname on windows Signed-off-by: Ashwin Naren --- vm/src/stdlib/time.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index e6581ce8473..d087cd3b1c9 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -43,6 +43,9 @@ mod decl { DateTime, Datelike, Timelike, }; use std::time::Duration; + #[cfg(target_env = "msvc")] + #[cfg(not(target_arch = "wasm32"))] + use windows::Win32::System::Time; #[allow(dead_code)] pub(super) const SEC_TO_MS: i64 = 1000; @@ -195,6 +198,18 @@ mod decl { unsafe { (to_str(super::c_tzname[0]), to_str(super::c_tzname[1])) }.into_pytuple(vm) } + #[cfg(target_env = "msvc")] + #[cfg(not(target_arch = "wasm32"))] + #[pyattr] + fn tzname(vm: &VirtualMachine) -> crate::builtins::PyTupleRef { + use crate::builtins::tuple::IntoPyTuple; + let info = get_tz_info(); + let standard = widestring::decode_utf16_lossy(info.StandardName).filter(|&c| c != '\0').collect::(); + let daylight = widestring::decode_utf16_lossy(info.DaylightName).filter(|&c| c != '\0').collect::(); + let tz_name = (&*standard, &*daylight); + tz_name.into_pytuple(vm) + } + fn pyobj_to_date_time( value: Either, vm: &VirtualMachine, From edb98eb20adbd279d076a9bb06163fdd7bb69f05 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Mon, 17 Feb 2025 21:19:36 -0800 Subject: [PATCH 3/6] time.timezone for windows Signed-off-by: Ashwin Naren --- vm/src/stdlib/time.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index d087cd3b1c9..e5398d498a1 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -178,6 +178,16 @@ mod decl { unsafe { super::c_timezone } } + #[cfg(target_env = "msvc")] + #[cfg(not(target_arch = "wasm32"))] + #[pyattr] + fn timezone() -> i32 { + let info = get_tz_info(); + // https://users.rust-lang.org/t/accessing-tzname-and-similar-constants-in-windows/125771/3 + (info.Bias + info.StandardBias) * 60 + } + + #[cfg(not(target_os = "freebsd"))] #[cfg(not(target_env = "msvc"))] #[cfg(not(target_arch = "wasm32"))] From 77fb165abdb459824e68e12fb151464d0afcdc60 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Mon, 17 Feb 2025 21:19:55 -0800 Subject: [PATCH 4/6] time.daylight for windows Signed-off-by: Ashwin Naren --- vm/src/stdlib/time.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index e5398d498a1..1277b6a7e10 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -196,6 +196,15 @@ mod decl { unsafe { super::c_daylight } } + #[cfg(target_env = "msvc")] + #[cfg(not(target_arch = "wasm32"))] + #[pyattr] + fn daylight() -> i32 { + let info = get_tz_info(); + // https://users.rust-lang.org/t/accessing-tzname-and-similar-constants-in-windows/125771/3 + (info.StandardBias != info.DaylightBias) as i32 + } + #[cfg(not(target_env = "msvc"))] #[cfg(not(target_arch = "wasm32"))] #[pyattr] From 1380e8dec78e7e3a5f7131715120a48358fc217a Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Mon, 17 Feb 2025 21:20:15 -0800 Subject: [PATCH 5/6] formatting Signed-off-by: Ashwin Naren --- vm/src/stdlib/time.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 1277b6a7e10..51411889a16 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -160,9 +160,7 @@ mod decl { fn get_tz_info() -> Time::TIME_ZONE_INFORMATION { let mut info = Time::TIME_ZONE_INFORMATION::default(); let info_ptr = &mut info as *mut Time::TIME_ZONE_INFORMATION; - let _ = unsafe { - Time::GetTimeZoneInformation(info_ptr) - }; + let _ = unsafe { Time::GetTimeZoneInformation(info_ptr) }; info } @@ -187,7 +185,6 @@ mod decl { (info.Bias + info.StandardBias) * 60 } - #[cfg(not(target_os = "freebsd"))] #[cfg(not(target_env = "msvc"))] #[cfg(not(target_arch = "wasm32"))] @@ -223,8 +220,12 @@ mod decl { fn tzname(vm: &VirtualMachine) -> crate::builtins::PyTupleRef { use crate::builtins::tuple::IntoPyTuple; let info = get_tz_info(); - let standard = widestring::decode_utf16_lossy(info.StandardName).filter(|&c| c != '\0').collect::(); - let daylight = widestring::decode_utf16_lossy(info.DaylightName).filter(|&c| c != '\0').collect::(); + let standard = widestring::decode_utf16_lossy(info.StandardName) + .filter(|&c| c != '\0') + .collect::(); + let daylight = widestring::decode_utf16_lossy(info.DaylightName) + .filter(|&c| c != '\0') + .collect::(); let tz_name = (&*standard, &*daylight); tz_name.into_pytuple(vm) } From 2053ab9ca318c08b2298815178fd3cce9bea7f52 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Mon, 17 Feb 2025 21:22:10 -0800 Subject: [PATCH 6/6] clippy Signed-off-by: Ashwin Naren --- vm/src/stdlib/time.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/stdlib/time.rs b/vm/src/stdlib/time.rs index 51411889a16..5a493c34724 100644 --- a/vm/src/stdlib/time.rs +++ b/vm/src/stdlib/time.rs @@ -179,7 +179,7 @@ mod decl { #[cfg(target_env = "msvc")] #[cfg(not(target_arch = "wasm32"))] #[pyattr] - fn timezone() -> i32 { + fn timezone(_vm: &VirtualMachine) -> i32 { let info = get_tz_info(); // https://users.rust-lang.org/t/accessing-tzname-and-similar-constants-in-windows/125771/3 (info.Bias + info.StandardBias) * 60 @@ -196,7 +196,7 @@ mod decl { #[cfg(target_env = "msvc")] #[cfg(not(target_arch = "wasm32"))] #[pyattr] - fn daylight() -> i32 { + fn daylight(_vm: &VirtualMachine) -> i32 { let info = get_tz_info(); // https://users.rust-lang.org/t/accessing-tzname-and-similar-constants-in-windows/125771/3 (info.StandardBias != info.DaylightBias) as i32