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 SystemTime2 which takes a little extra work to turn into an integer. This is what that looks like:

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.

~ fin ~

References