// Licensed under the Apache License, Version 2.0 // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. // All files in the project carrying such notice may not be copied, modified, or distributed // except according to those terms.
//! Windows [`FILETIME`](https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime) //! and [`SYSTEMTIME`](https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-systemtime) //! string and binary serialization //! //! A transparent wrapper is provided for each type, with //! `Display` for [`SystemTimeUTC`](struct.SystemTimeUTC.html) and //! `Ord` and `Eq` for [`FileTime`](struct.FileTime.html). //! //! # serde # //! //! Use the `filetime_serde` feature to derive `Serialize` and `Deserialize`, you can then //! derive them for structs containing `FILETIME` and `SYSTEMTIME` like so: //! //! ``` //! # fn main() {} //! # //! # #[cfg(feature = "filetime_serde")] //! # extern crate serde_derive; //! # extern crate winapi; //! # //! # #[cfg(feature = "filetime_serde")] //! # mod test { //! use filetime_win::{FileTimeSerde, SystemTimeSerde}; //! use serde_derive::{Deserialize, Serialize}; //! use winapi::shared::minwindef::FILETIME; //! use winapi::um::minwinbase::SYSTEMTIME; //! //! #[derive(Serialize, Deserialize)] //! struct SerdeTest { //! #[serde(with = "FileTimeSerde")] //! ft: FILETIME, //! #[serde(with = "SystemTimeSerde")] //! st: SYSTEMTIME, //! } //! # } //! ``` externcrate comedy; #[cfg(feature = "filetime_serde")] externcrate serde; #[cfg(feature = "filetime_serde")] externcrate serde_derive; externcrate winapi;
use std::cmp::Ordering; use std::fmt::{Debug, Display, Formatter, Result}; use std::mem; use std::result;
use comedy::check_true;
use winapi::shared::minwindef::FILETIME; #[cfg(feature = "filetime_serde")] use winapi::shared::minwindef::{DWORD, WORD}; use winapi::shared::ntdef::ULARGE_INTEGER; use winapi::um::minwinbase::SYSTEMTIME; use winapi::um::sysinfoapi::GetSystemTime; use winapi::um::timezoneapi::{FileTimeToSystemTime, SystemTimeToFileTime};
#[cfg(feature = "filetime_serde")] use serde_derive::{Deserialize, Serialize};
#[cfg(feature = "filetime_serde")] #[allow(non_snake_case)] #[derive(Serialize, Deserialize)] #[serde(remote = "SYSTEMTIME")] pubstruct SystemTimeSerde {
wYear: WORD,
wMonth: WORD,
wDayOfWeek: WORD,
wDay: WORD,
wHour: WORD,
wMinute: WORD,
wSecond: WORD,
wMilliseconds: WORD,
}
/// Wraps `SYSTEMTIME` /// /// The `SYSTEMTIME` struct can be UTC or local time, but `SystemTimeUTC` should only be used for /// UTC. /// #[derive(Copy, Clone)] #[cfg_attr(feature = "filetime_serde", derive(Serialize, Deserialize))] #[repr(transparent)] pubstruct SystemTimeUTC( #[cfg_attr(feature = "filetime_serde", serde(with = "SystemTimeSerde"))] pub SYSTEMTIME,
);
impl FileTime { /// Convert to raw integer /// /// `FILETIME` is 100-nanosecond intervals since January 1, 1601 (UTC), but if the high /// bit is 1 there may be a different interpretation. pubfn to_u64(self) -> u64 { unsafe { letmut v: ULARGE_INTEGER = mem::zeroed();
v.s_mut().LowPart = self.0.dwLowDateTime;
v.s_mut().HighPart = self.0.dwHighDateTime;
*v.QuadPart()
}
}
/// Convert to `SystemTimeUTC` via `FileTimeToSystemTime()` pubfn to_system_time_utc(self) -> result::Result<SystemTimeUTC, comedy::Win32Error> { unsafe { letmut system_time = mem::zeroed();
impl SystemTimeUTC { /// Get current system time in UTC via `GetSystemTime()` /// /// "Because the system time can be adjusted either forward or backward, do not compare /// system time readings to determine elapsed time." pubfn now() -> SystemTimeUTC { unsafe { letmut system_time = mem::zeroed();
GetSystemTime(&mut system_time);
SystemTimeUTC(system_time)
}
}
/// Convert to `FileTime` via `SystemTimeToFileTime()` pubfn to_file_time(&self) -> result::Result<FileTime, comedy::Win32Error> { unsafe { letmut file_time = mem::zeroed();
/// Format as ISO 8601 date and time: `YYYY-MM-DDThh:mm:ss.fffZ` impl Display for SystemTimeUTC { fn fmt(&self, f: &mut Formatter) -> Result {
write!(
f, "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z", self.0.wYear, self.0.wMonth, self.0.wDay, self.0.wHour, self.0.wMinute, self.0.wSecond, self.0.wMilliseconds
)
}
}
#[cfg(test)] mod tests { usesuper::{FileTime, SystemTimeUTC}; use winapi::shared::minwindef::FILETIME; use winapi::um::minwinbase::SYSTEMTIME;
#[test] fn roundtrip() { let ft = SystemTimeUTC::now().to_file_time().unwrap();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.