The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

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

TODO: Pull subtitle into page object
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.

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

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
-- 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()
}


-- p

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

-- ref
-- title: std::fs::Metadata Struct docs
-- url: https://doc.rust-lang.org/std/fs/struct.Metadata.html
-- id: md

-- ref
-- title: std::time::SystemTime Struct docs
-- url: https://doc.rust-lang.org/std/time/struct.SystemTime.html
-- id: st 

-- ref
-- title: Answer: How can you get the modification time of a file in a cross platform manner?
-- url: https://stackoverflow.com/a/74914003/102401

After kicking around on the docs for an hour this is where 
I finally found the full answer for how to pull this off


-- categories
-- Rust 

-- metadata
-- date: 2023-04-30 18:52:38
-- id: 2palu64j
-- site: aws
-- type: post
-- status: published