Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Get The Modification Time Of A File In Epoch Seconds In Rust

I use epoch seconds for tracking when files change in my blog engine. There's a built - in std : : fs : : Metadata struct ` that provides access to the time via a [TODO: Code shorthand span ] call. That returns a SystemTime 2 which takes a little extra work to turn into an integer. This is what that looks like :

rust
use std::fs;
use std::time::UNIX_EPOCH;

fn main() {
    let mod_time = file_epoch_mod_time("./test.txt");
    dbg!(mod_time);
}

pub fn file_epoch_mod_time(path: &str) -> u64 {
    fs::metadata(path)
        .unwrap()
        .modified()
        .unwrap()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs()
}

It's a few hoops but it gets you there.

Footnotes And References