Created
August 30, 2023 12:49
-
-
Save leiless/5a31fe7ab394e586e8320e3cb4ee4435 to your computer and use it in GitHub Desktop.
Windows FILETIME to UNIX time epoch.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::os::windows::fs::MetadataExt; | |
// Nanoseconds between [Jan 1 1601, Jan 1 1970] | |
const UNIX_EPOCH_OFFSET: u64 = 0x019d_b1de_d53e_8000; | |
// Panic if file_time is below Jan 1 1970 | |
fn filetime_to_unix_ns(file_time: u64) -> u64 { | |
(file_time - UNIX_EPOCH_OFFSET) * 100 | |
} | |
fn main() -> Result<(), Box<dyn std::error::Error>> { | |
let md = std::fs::symlink_metadata(r#"C:\Users\nutstore\Desktop\hfs.exe"#)?; | |
let last_access_filetime = md.last_access_time(); | |
println!("FILETIME: {}", last_access_filetime); | |
let year1970 = windows::Win32::Foundation::SYSTEMTIME { | |
wYear: 1970, | |
wMonth: 1, | |
/* Jan 1, 1970 is Thursday */ | |
wDayOfWeek: 4, | |
wDay: 1, | |
wHour: 0, | |
wMinute: 0, | |
wSecond: 0, | |
wMilliseconds: 0, | |
}; | |
let mut file_time = windows::Win32::Foundation::FILETIME::default(); | |
unsafe { | |
windows::Win32::System::Time::SystemTimeToFileTime( | |
std::ptr::addr_of!(year1970), | |
std::ptr::addr_of_mut!(file_time), | |
)?; | |
} | |
let t = (file_time.dwHighDateTime as u64) << 32 | file_time.dwLowDateTime as u64; | |
println!("Year 1970 FILETIME: {:?}", file_time); | |
println!("Year 1970 FILETIME: {:#x}", t); | |
println!("FILETIME to unix epoch ns: {}", filetime_to_unix_ns(last_access_filetime)); | |
Ok(()) | |
} |
Author
leiless
commented
Aug 30, 2023
1693399407360322700
GMT: Wednesday, August 30, 2023 12:43:27.360 PM
Your time zone: Wednesday, August 30, 2023 8:43:27.360 PM GMT+08:00
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment