| 1 |
// Licensed under the Apache License, Version 2.0 |
| 2 |
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 4 |
// All files in the project carrying such notice may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
//! Windows [`FILETIME`](https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-filetime) |
| 8 |
//! and [`SYSTEMTIME`](https://docs.microsoft.com/en-us/windows/desktop/api/minwinbase/ns-minwinbase-systemtime) |
| 9 |
//! string and binary serialization |
| 10 |
//! |
| 11 |
//! A transparent wrapper is provided for each type, with |
| 12 |
//! `Display` for [`SystemTimeUTC`](struct.SystemTimeUTC.html) and |
| 13 |
//! `Ord` and `Eq` for [`FileTime`](struct.FileTime.html). |
| 14 |
//! |
| 15 |
//! # serde # |
| 16 |
//! |
| 17 |
//! Use the `filetime_serde` feature to derive `Serialize` and `Deserialize`, you can then |
| 18 |
//! derive them for structs containing `FILETIME` and `SYSTEMTIME` like so: |
| 19 |
//! |
| 20 |
//! ``` |
| 21 |
//! # fn main() {} |
| 22 |
//! # |
| 23 |
//! # #[cfg(feature = "filetime_serde")] |
| 24 |
//! # extern crate serde_derive; |
| 25 |
//! # extern crate winapi; |
| 26 |
//! # |
| 27 |
//! # #[cfg(feature = "filetime_serde")] |
| 28 |
//! # mod test { |
| 29 |
//! use filetime_win::{FileTimeSerde, SystemTimeSerde}; |
| 30 |
//! use serde_derive::{Deserialize, Serialize}; |
| 31 |
//! use winapi::shared::minwindef::FILETIME; |
| 32 |
//! use winapi::um::minwinbase::SYSTEMTIME; |
| 33 |
//! |
| 34 |
//! #[derive(Serialize, Deserialize)] |
| 35 |
//! struct SerdeTest { |
| 36 |
//! #[serde(with = "FileTimeSerde")] |
| 37 |
//! ft: FILETIME, |
| 38 |
//! #[serde(with = "SystemTimeSerde")] |
| 39 |
//! st: SYSTEMTIME, |
| 40 |
//! } |
| 41 |
//! # } |
| 42 |
//! ``` |
| 43 |
extern crate comedy; |
| 44 |
#[cfg(feature = "filetime_serde")] |
| 45 |
extern crate serde; |
| 46 |
#[cfg(feature = "filetime_serde")] |
| 47 |
extern crate serde_derive; |
| 48 |
extern crate winapi; |
| 49 |
|
| 50 |
use std::cmp::Ordering; |
| 51 |
use std::fmt::{Debug, Display, Formatter, Result}; |
| 52 |
use std::mem; |
| 53 |
use std::result; |
| 54 |
|
| 55 |
use comedy::check_true; |
| 56 |
|
| 57 |
use winapi::shared::minwindef::FILETIME; |
| 58 |
#[cfg(feature = "filetime_serde")] |
| 59 |
use winapi::shared::minwindef::{DWORD, WORD}; |
| 60 |
use winapi::shared::ntdef::ULARGE_INTEGER; |
| 61 |
use winapi::um::minwinbase::SYSTEMTIME; |
| 62 |
use winapi::um::sysinfoapi::GetSystemTime; |
| 63 |
use winapi::um::timezoneapi::{FileTimeToSystemTime, SystemTimeToFileTime}; |
| 64 |
|
| 65 |
#[cfg(feature = "filetime_serde")] |
| 66 |
use serde_derive::{Deserialize, Serialize}; |
| 67 |
|
| 68 |
#[cfg(feature = "filetime_serde")] |
| 69 |
#[allow(non_snake_case)] |
| 70 |
#[derive(Serialize, Deserialize)] |
| 71 |
#[serde(remote = "FILETIME")] |
| 72 |
pub struct FileTimeSerde { |
| 73 |
dwLowDateTime: DWORD, |
| 74 |
dwHighDateTime: DWORD, |
| 75 |
} |
| 76 |
|
| 77 |
/// Wraps `FILETIME` |
| 78 |
#[derive(Copy, Clone)] |
| 79 |
#[cfg_attr(feature = "filetime_serde", derive(Serialize, Deserialize))] |
| 80 |
#[repr(transparent)] |
| 81 |
pub struct FileTime( |
| 82 |
#[cfg_attr(feature = "filetime_serde", serde(with = "FileTimeSerde"))] pub FILETIME, |
| 83 |
); |
| 84 |
|
| 85 |
#[cfg(feature = "filetime_serde")] |
| 86 |
#[allow(non_snake_case)] |
| 87 |
#[derive(Serialize, Deserialize)] |
| 88 |
#[serde(remote = "SYSTEMTIME")] |
| 89 |
pub struct SystemTimeSerde { |
| 90 |
wYear: WORD, |
| 91 |
wMonth: WORD, |
| 92 |
wDayOfWeek: WORD, |
| 93 |
wDay: WORD, |
| 94 |
wHour: WORD, |
| 95 |
wMinute: WORD, |
| 96 |
wSecond: WORD, |
| 97 |
wMilliseconds: WORD, |
| 98 |
} |
| 99 |
|
| 100 |
/// Wraps `SYSTEMTIME` |
| 101 |
/// |
| 102 |
/// The `SYSTEMTIME` struct can be UTC or local time, but `SystemTimeUTC` should only be used for |
| 103 |
/// UTC. |
| 104 |
/// |
| 105 |
#[derive(Copy, Clone)] |
| 106 |
#[cfg_attr(feature = "filetime_serde", derive(Serialize, Deserialize))] |
| 107 |
#[repr(transparent)] |
| 108 |
pub struct SystemTimeUTC( |
| 109 |
#[cfg_attr(feature = "filetime_serde", serde(with = "SystemTimeSerde"))] pub SYSTEMTIME, |
| 110 |
); |
| 111 |
|
| 112 |
impl FileTime { |
| 113 |
/// Convert to raw integer |
| 114 |
/// |
| 115 |
/// `FILETIME` is 100-nanosecond intervals since January 1, 1601 (UTC), but if the high |
| 116 |
/// bit is 1 there may be a different interpretation. |
| 117 |
pub fn to_u64(self) -> u64 { |
| 118 |
unsafe { |
| 119 |
let mut v: ULARGE_INTEGER = mem::zeroed(); |
| 120 |
v.s_mut().LowPart = self.0.dwLowDateTime; |
| 121 |
v.s_mut().HighPart = self.0.dwHighDateTime; |
| 122 |
*v.QuadPart() |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/// Convert to `SystemTimeUTC` via `FileTimeToSystemTime()` |
| 127 |
pub fn to_system_time_utc(self) -> result::Result<SystemTimeUTC, comedy::Win32Error> { |
| 128 |
unsafe { |
| 129 |
let mut system_time = mem::zeroed(); |
| 130 |
|
| 131 |
check_true!(FileTimeToSystemTime(&self.0, &mut system_time))?; |
| 132 |
|
| 133 |
Ok(SystemTimeUTC(system_time)) |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
impl PartialEq for FileTime { |
| 139 |
fn eq(&self, other: &FileTime) -> bool { |
| 140 |
self.0.dwLowDateTime == other.0.dwLowDateTime |
| 141 |
&& self.0.dwHighDateTime == other.0.dwHighDateTime |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
impl Eq for FileTime {} |
| 146 |
|
| 147 |
impl PartialOrd for FileTime { |
| 148 |
fn partial_cmp(&self, other: &FileTime) -> Option<Ordering> { |
| 149 |
Some(self.cmp(other)) |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
impl Ord for FileTime { |
| 154 |
fn cmp(&self, other: &FileTime) -> Ordering { |
| 155 |
self.to_u64().cmp(&other.to_u64()) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
impl Debug for FileTime { |
| 160 |
fn fmt(&self, f: &mut Formatter) -> Result { |
| 161 |
write!( |
| 162 |
f, |
| 163 |
"FileTime {{ dwLowDateTime: {:?}, dwHighDateTime: {:?} }}", |
| 164 |
self.0.dwLowDateTime, self.0.dwHighDateTime |
| 165 |
) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
impl SystemTimeUTC { |
| 170 |
/// Get current system time in UTC via `GetSystemTime()` |
| 171 |
/// |
| 172 |
/// "Because the system time can be adjusted either forward or backward, do not compare |
| 173 |
/// system time readings to determine elapsed time." |
| 174 |
pub fn now() -> SystemTimeUTC { |
| 175 |
unsafe { |
| 176 |
let mut system_time = mem::zeroed(); |
| 177 |
GetSystemTime(&mut system_time); |
| 178 |
SystemTimeUTC(system_time) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/// Convert to `FileTime` via `SystemTimeToFileTime()` |
| 183 |
pub fn to_file_time(&self) -> result::Result<FileTime, comedy::Win32Error> { |
| 184 |
unsafe { |
| 185 |
let mut file_time = mem::zeroed(); |
| 186 |
|
| 187 |
check_true!(SystemTimeToFileTime(&self.0, &mut file_time))?; |
| 188 |
|
| 189 |
Ok(FileTime(file_time)) |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
impl Debug for SystemTimeUTC { |
| 195 |
fn fmt(&self, f: &mut Formatter) -> Result { |
| 196 |
write!( |
| 197 |
f, |
| 198 |
concat!( |
| 199 |
"SystemTimeUTC {{ ", |
| 200 |
"wYear: {:?}, wMonth: {:?}, wDayOfWeek: {:?}, wDay: {:?}, ", |
| 201 |
"wHour: {:?}, wMinute: {:?}, wSecond: {:?}, wMilliseconds: {:?} }}" |
| 202 |
), |
| 203 |
self.0.wYear, |
| 204 |
self.0.wMonth, |
| 205 |
self.0.wDayOfWeek, |
| 206 |
self.0.wDay, |
| 207 |
self.0.wHour, |
| 208 |
self.0.wMinute, |
| 209 |
self.0.wSecond, |
| 210 |
self.0.wMilliseconds |
| 211 |
) |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/// Format as ISO 8601 date and time: `YYYY-MM-DDThh:mm:ss.fffZ` |
| 216 |
impl Display for SystemTimeUTC { |
| 217 |
fn fmt(&self, f: &mut Formatter) -> Result { |
| 218 |
write!( |
| 219 |
f, |
| 220 |
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z", |
| 221 |
self.0.wYear, |
| 222 |
self.0.wMonth, |
| 223 |
self.0.wDay, |
| 224 |
self.0.wHour, |
| 225 |
self.0.wMinute, |
| 226 |
self.0.wSecond, |
| 227 |
self.0.wMilliseconds |
| 228 |
) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
#[cfg(test)] |
| 233 |
mod tests { |
| 234 |
use super::{FileTime, SystemTimeUTC}; |
| 235 |
use winapi::shared::minwindef::FILETIME; |
| 236 |
use winapi::um::minwinbase::SYSTEMTIME; |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn roundtrip() { |
| 240 |
let ft = SystemTimeUTC::now().to_file_time().unwrap(); |
| 241 |
|
| 242 |
assert_eq!(ft, ft); |
| 243 |
assert_eq!(ft, ft.to_system_time_utc().unwrap().to_file_time().unwrap()); |
| 244 |
} |
| 245 |
|
| 246 |
#[test] |
| 247 |
fn next_year() { |
| 248 |
let st_now = SystemTimeUTC::now(); |
| 249 |
let st_next_year = SystemTimeUTC(SYSTEMTIME { |
| 250 |
wYear: st_now.0.wYear + 1, |
| 251 |
..st_now.0 |
| 252 |
}); |
| 253 |
|
| 254 |
let ft_now = st_now.to_file_time().unwrap(); |
| 255 |
let ft_next_year = st_next_year.to_file_time().unwrap(); |
| 256 |
assert!(ft_next_year > ft_now); |
| 257 |
} |
| 258 |
|
| 259 |
#[test] |
| 260 |
fn non_time_filetime() { |
| 261 |
let ft = FileTime(FILETIME { |
| 262 |
dwLowDateTime: 0xFFFF_FFFFu32, |
| 263 |
dwHighDateTime: 0xFFFF_FFFFu32, |
| 264 |
}); |
| 265 |
|
| 266 |
ft.to_system_time_utc().expect_err("should have failed"); |
| 267 |
} |
| 268 |
} |