Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

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^md^^ that provides access to the time via a `.modified()`rust` call. That returns a SystemTime^st^^ which takes a little extra work to turn into an integer. This is what that looks like:

Code

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 get you there.

References