Skip to content

Commit

Permalink
update windows-sys
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuxiujia committed Dec 29, 2023
1 parent bde1138 commit b780121
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastdate"
version = "0.3.27"
version = "0.3.28"
edition = "2021"
description = "Rust fast date carte"
readme = "Readme.md"
Expand All @@ -18,6 +18,9 @@ serde_json = "1"
[target.'cfg(unix)'.dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["std", "minwinbase", "minwindef", "timezoneapi"] }

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.52.0"
features = [
"Win32_Foundation",
"Win32_System_Time",
]
4 changes: 2 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ fn bench_from_timestamp_millis(b: &mut Bencher) {
//181 ns/iter (+/- 2)
#[bench]
fn bench_format(b: &mut Bencher) {
let dt=DateTime::from_str("1997-12-13T11:12:13.123456+09:00").unwrap();
let dt = DateTime::from_str("1997-12-13T11:12:13.123456+09:00").unwrap();
b.iter(|| {
std::hint::black_box({
dt.format("YYYY-MM-DD/hh/mm/ss.000000");
});
});
}
}
24 changes: 14 additions & 10 deletions src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ impl DateTime {
}
write!(result, ".{:09}", self.nano()).unwrap()
} else if result.ends_with(".000000") {
if (index + 3) < fmt.len() && chars[index + 1] == '0' as u8 && chars[index + 2] == '0' as u8 && chars[index + 3] == '0' as u8 {
if (index + 3) < fmt.len()
&& chars[index + 1] == '0' as u8
&& chars[index + 2] == '0' as u8
&& chars[index + 3] == '0' as u8
{
index += 1;
continue;
}
Expand Down Expand Up @@ -445,7 +449,7 @@ impl DateTime {
Self {
inner: time1::OffsetDateTime::from(s),
}
.set_offset(offset)
.set_offset(offset)
}

/// stand "0000-00-00 00:00:00.000000000"
Expand Down Expand Up @@ -656,7 +660,7 @@ impl From<Date> for DateTime {
"{:04}-{:02}-{:02} 00:00:00.000000000Z",
arg.year, arg.mon, arg.day
))
.unwrap()
.unwrap()
}
}

Expand All @@ -675,7 +679,7 @@ impl From<Time> for DateTime {
"0000-01-01 {:02}:{:02}:{:02}.{:09}Z",
arg.hour, arg.minute, arg.sec, arg.nano
))
.unwrap()
.unwrap()
}
}

Expand All @@ -685,7 +689,7 @@ impl From<(Date, Time)> for DateTime {
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:09}Z",
arg.0.year, arg.0.mon, arg.0.day, arg.1.hour, arg.1.minute, arg.1.sec, arg.1.nano
))
.unwrap()
.unwrap()
}
}

Expand All @@ -696,7 +700,7 @@ impl From<(Date, Time, i32)> for DateTime {
"{:04}-{:02}-{:02} {:02}:{:02}:{:02}.{:09}Z",
arg.0.year, arg.0.mon, arg.0.day, arg.1.hour, arg.1.minute, arg.1.sec, arg.1.nano
))
.unwrap();
.unwrap();
datetime = datetime.set_offset(arg.2).add_sub_sec(-arg.2 as i64);
datetime
}
Expand Down Expand Up @@ -739,8 +743,8 @@ impl PartialOrd for DateTime {

impl Serialize for DateTime {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
Expand All @@ -749,8 +753,8 @@ impl Serialize for DateTime {
#[cfg(not(tarpaulin_include))]
impl<'de> Deserialize<'de> for DateTime {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
where
D: Deserializer<'de>,
{
use serde::de::Error;
let s = String::deserialize(deserializer)?;
Expand Down
27 changes: 14 additions & 13 deletions src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
use super::Tm;
use std::io;
use std::mem;

use winapi::shared::minwindef::*;
use winapi::um::minwinbase::SYSTEMTIME;
use winapi::um::timezoneapi::*;
use windows_sys::Win32::Foundation::{FILETIME, SYSTEMTIME};
use windows_sys::Win32::System::Time::FileTimeToSystemTime;
use windows_sys::Win32::System::Time::SystemTimeToTzSpecificLocalTime;
use windows_sys::Win32::System::Time::TzSpecificLocalTimeToSystemTime;
use windows_sys::Win32::System::Time::{GetTimeZoneInformation, SystemTimeToFileTime};

const HECTONANOSECS_IN_SEC: i64 = 10_000_000;
const HECTONANOSEC_TO_UNIX_EPOCH: i64 = 11_644_473_600 * HECTONANOSECS_IN_SEC;

fn time_to_file_time(sec: i64) -> FILETIME {
let t = ((sec * HECTONANOSECS_IN_SEC) + HECTONANOSEC_TO_UNIX_EPOCH) as u64;
FILETIME {
dwLowDateTime: t as DWORD,
dwHighDateTime: (t >> 32) as DWORD,
dwLowDateTime: t as u32,
dwHighDateTime: (t >> 32) as u32,
}
}

Expand All @@ -46,13 +47,13 @@ fn system_time_to_file_time(sys: &SYSTEMTIME) -> FILETIME {

fn tm_to_system_time(tm: &Tm) -> SYSTEMTIME {
let mut sys: SYSTEMTIME = unsafe { mem::zeroed() };
sys.wSecond = tm.tm_sec as WORD;
sys.wMinute = tm.tm_min as WORD;
sys.wHour = tm.tm_hour as WORD;
sys.wDay = tm.tm_mday as WORD;
sys.wDayOfWeek = tm.tm_wday as WORD;
sys.wMonth = (tm.tm_mon + 1) as WORD;
sys.wYear = (tm.tm_year + 1900) as WORD;
sys.wSecond = tm.tm_sec as u16;
sys.wMinute = tm.tm_min as u16;
sys.wHour = tm.tm_hour as u16;
sys.wDay = tm.tm_mday as u16;
sys.wDayOfWeek = tm.tm_wday as u16;
sys.wMonth = (tm.tm_mon + 1) as u16;
sys.wYear = (tm.tm_year + 1900) as u16;
sys
}

Expand Down

0 comments on commit b780121

Please sign in to comment.