Skip to content

Commit

Permalink
Switch to windows-bindgen
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 25, 2023
1 parent 080f7a0 commit 5862d08
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ name = "chrono"
default = ["clock", "std", "oldtime", "wasmbind"]
alloc = []
libc = []
winapi = ["windows-targets"]
std = []
clock = ["std", "winapi", "iana-time-zone"]
oldtime = ["time"]
Expand All @@ -44,7 +45,10 @@ js-sys = { version = "0.3", optional = true } # contains FFI bindings for


[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.0", features = ["std", "minwinbase", "minwindef", "timezoneapi", "sysinfoapi"], optional = true }
windows-targets = { version = "0.48", optional = true }

[target.'cfg(windows)'.dev-dependencies]
windows-bindgen = { version = "0.49" }

[target.'cfg(unix)'.dependencies]
iana-time-zone = { version = "0.1.45", optional = true, features = ["fallback"] }
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,9 @@ pub use month::{Month, Months, ParseMonthError};
mod traits;
pub use traits::{Datelike, Timelike};

#[cfg(all(windows, feature = "clock"))]
mod win_bindings;

#[cfg(feature = "__internal_bench")]
#[doc(hidden)]
pub use naive::__BenchYearFlags;
Expand Down
6 changes: 1 addition & 5 deletions src/offset/local/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ use std::io::Error;
use std::ptr;
use std::result::Result;

use winapi::shared::minwindef::FILETIME;
use winapi::um::minwinbase::SYSTEMTIME;
use winapi::um::timezoneapi::{
SystemTimeToFileTime, SystemTimeToTzSpecificLocalTime, TzSpecificLocalTimeToSystemTime,
};
use crate::win_bindings::*;

use super::FixedOffset;
use crate::{Datelike, LocalResult, NaiveDateTime, Timelike};
Expand Down
51 changes: 51 additions & 0 deletions src/win_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#![allow(unreachable_pub)]
// Bindings generated by `windows-bindgen` 0.49.0
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]
::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;
#[repr(C)]
pub struct FILETIME {
pub dwLowDateTime: u32,
pub dwHighDateTime: u32,
}
impl ::core::marker::Copy for FILETIME {}
impl ::core::clone::Clone for FILETIME {
fn clone(&self) -> Self {
*self
}
}
#[repr(C)]
pub struct SYSTEMTIME {
pub wYear: u16,
pub wMonth: u16,
pub wDayOfWeek: u16,
pub wDay: u16,
pub wHour: u16,
pub wMinute: u16,
pub wSecond: u16,
pub wMilliseconds: u16,
}
impl ::core::marker::Copy for SYSTEMTIME {}
impl ::core::clone::Clone for SYSTEMTIME {
fn clone(&self) -> Self {
*self
}
}
#[repr(C)]
pub struct 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,
}
impl ::core::marker::Copy for TIME_ZONE_INFORMATION {}
impl ::core::clone::Clone for TIME_ZONE_INFORMATION {
fn clone(&self) -> Self {
*self
}
}
20 changes: 20 additions & 0 deletions tests/win_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![cfg(all(windows, feature = "clock", feature = "std"))]

use std::fs::OpenOptions;
use std::io::{Result, Write};

#[test]
fn gen_bindings() -> Result<()> {
let apis = [
"Windows.Win32.System.Time.SystemTimeToFileTime",
"Windows.Win32.System.Time.SystemTimeToTzSpecificLocalTime",
"Windows.Win32.System.Time.TzSpecificLocalTimeToSystemTime",
];

let bindings = windows_bindgen::standalone(&apis);
let mut file =
OpenOptions::new().write(true).create(true).truncate(true).open("src/win_bindings.rs")?;
file.write("#![allow(unreachable_pub)]\n".as_bytes())?;
file.write(bindings.as_bytes())?;
Ok(())
}

0 comments on commit 5862d08

Please sign in to comment.