How to use System Constants : std::sys::time::UNIX_EPOCH


How to use System Constants : std::sys::time::UNIX_EPOCH

And how to calculate human readable date time using
std::time::SystemTime (which is a counter from epoch 1970 Jan 1st, stored in std::sys::time::UNIX_EPOCH)

some code example would be great!




thank you for the tip, i am aware of the library chrono. But since i am learning i want to take the hard way of using features available. example in this case using system global constant and to write or convert systemtime into human readability.

it would be great if u can give a simple example of using the UNIX_EPOCH and std::time::SystemTime

And parsing the chrono library for me would be too complicated for me at my current state of Rusting!



There is nothing in the stdlib to help you with converting epoch to something readable.
Best thing you can get without using libraris is to get amount of seconds from epoch and work from there,
which means a lot of work that’s very easy to get wrong. I’d just use chrono, or see how it does it.

use std::time::{SystemTime, UNIX_EPOCH};
// use std::thread::sleep;

// pub const UNIX_EPOCH: SystemTime = SystemTime(time::UNIX_EPOCH);

fn main() {
   let now = SystemTime::now();
   let du = now.duration_since(UNIX_EPOCH);
   println!("SystemTime Now: {:?}", now.duration_since(UNIX_EPOCH));

   let d1 = match du {
       Ok(d) =>  d
       , Err(e) => panic!("Error Occurred processing Duration: {}!", e)

   println!("Duration Since Epoch: {:?} secs", d1.as_secs());


If you are interested in the duration since UNIX_EPOCH, you can skip SystemTime::now() and do this:

let du = UNIX_EPOCH.elapsed();