home ~ projects ~ socials

Serialize A File's Modification Time To Store It In A Database In Rust

This is how you can get a float of the file modified (or created) time to store in a database.

use std::fs;
use std::time::UNIX_EPOCH;
use std::path::PathBuf;

fn main() {
  let path = PathBuf::from("alfa.txt");
  let ts = get_timestamp(&path);
  dbg!(ts);
}

fn get_timestamp(path: &PathBuf) -> f64 {
  fs::metadata(path)
    .unwrap()
    .modified()
    .unwrap()
    .duration_since(UNIX_EPOCH)
    .unwrap()
    .as_secs_f64()
}
Output:
[_active_nvim_run:8:3] ts = 1717642181.0231614
-- end of line --