I am currently using Rust in Linux to examine nearly a billion files. One of the things I need is the file modified date/time in a particular format, which can be conveniently be obtained using Crate chrono. I therefore extract a file's Metadata from its directory entry. While in practice error handling is very important (so the routine never crashes out and abandons hours of successful results), for the purposes of this question I will by-pass all that by using .unwrap() for clarity. so I have:
let file_metadata=fs::metadata(file_path).unwrap();
let file_last_modified=file_metadata.modified().unwrap();
If I now look at the format of this:
print!("{:?} : ",file_last_modified);
I get, for example:- 'SystemTime { tv_sec: 1594033168, tv_nsec: 848710897 }' which I feel I need to somehow feed into crate chrono. I am aware of:-
let file_datestamp=Utc.timestamp(1594033168, 848710897); //(crate chrono)
which then, for example, be could followed by:
println!("{}", file_datestamp.format("%Y-%m-%d@%H:%M:%S"));
to give what I asked for:- 2020-07-06@10:59:28. Hopefully NEVER 'summer time corrected', am/pm involved, timezone ambiguous, or anything else similar to confuse exactly 'when' this means. Simple sorting by this parameter should put a list of results into the order the files were last modified as real time progressed. Yet it is human understandable, and no spaces to cause future record parsing problems.
However, in rust I am not used to having to parse out the two numbers out of the 'SystemTime' type myself, with all the format/error variations it could get up to. To say nothing of my inferior unoptimized coding skills with rust. I feel sure there would be something (probably) obvious that I am missing to do this much more concisely, quickly and efficiently.
I have tried to implement:-
pub fn timestamp(&self) -> i64
(Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp").) and
pub fn timestamp_nanos(&self) -> i64
(Returns the number of non-leap-nanoseconds since January 1, 1970 UTC)found in: chrono::DateTime - Rust
with every 'use' at the top I can think of:-
let file_timestamp_sec=file_last_modified.timestamp();
let file_timestamp_nanos=file_last_modified.timestamp_nanos();
but I get:-
error[E0599]: no method named timestamp
found for type std::time::SystemTime
in the current scope
Thank you very much in advance. (I never get on with or really understand up-voting or down-voting social implication systems.)
Yogi39