1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-02 05:57:46 +00:00

date: migrate winapi to windows-sys crate

This commit is contained in:
Niyaz Nigmatullin 2022-10-19 23:28:30 +03:00 committed by Niyaz Nigmatullin
parent b0b7565ba9
commit ff30cacbe1
3 changed files with 10 additions and 13 deletions

2
Cargo.lock generated
View file

@ -2308,7 +2308,7 @@ dependencies = [
"clap 4.0.17",
"libc",
"uucore",
"winapi",
"windows-sys 0.42.0",
]
[[package]]

View file

@ -23,7 +23,7 @@ uucore = { version=">=0.0.16", package="uucore", path="../../uucore" }
libc = "0.2"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["minwinbase", "sysinfoapi", "minwindef"] }
windows-sys = { version = "0.42.0", default-features = false, features = ["Win32_Foundation", "Win32_System_SystemInformation"] }
[[bin]]
name = "date"

View file

@ -23,10 +23,7 @@ use uucore::error::FromIo;
use uucore::error::{UResult, USimpleError};
use uucore::{format_usage, show_error};
#[cfg(windows)]
use winapi::{
shared::minwindef::WORD,
um::{minwinbase::SYSTEMTIME, sysinfoapi::SetSystemTime},
};
use windows_sys::Win32::{Foundation::SYSTEMTIME, System::SystemInformation::SetSystemTime};
// Options
const DATE: &str = "date";
@ -409,16 +406,16 @@ fn set_system_datetime(date: DateTime<Utc>) -> UResult<()> {
/// https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime
fn set_system_datetime(date: DateTime<Utc>) -> UResult<()> {
let system_time = SYSTEMTIME {
wYear: date.year() as WORD,
wMonth: date.month() as WORD,
wYear: date.year() as u16,
wMonth: date.month() as u16,
// Ignored
wDayOfWeek: 0,
wDay: date.day() as WORD,
wHour: date.hour() as WORD,
wMinute: date.minute() as WORD,
wSecond: date.second() as WORD,
wDay: date.day() as u16,
wHour: date.hour() as u16,
wMinute: date.minute() as u16,
wSecond: date.second() as u16,
// TODO: be careful of leap seconds - valid range is [0, 999] - how to handle?
wMilliseconds: ((date.nanosecond() / 1_000_000) % 1000) as WORD,
wMilliseconds: ((date.nanosecond() / 1_000_000) % 1000) as u16,
};
let result = unsafe { SetSystemTime(&system_time) };