How to convert an era to a standard date and time?

I use a drawer chronograph; after some digging, I found that the DateTime type has a timestamp() function that can generate an era time of type i64 . However, I could not find out how to convert it back to DateTime .

 extern crate chrono; use chrono::*; fn main() { let date = chrono::UTC.ymd(2020, 1, 1).and_hms(0, 0, 0); println!("{}", start_date.timestamp()); // ...how to convert it back? } 
+7
time rust rust-chrono
source share
1 answer

First you need to create a NaiveDateTime and then use it again to create a DateTime :

 extern crate chrono; use chrono::prelude::*; fn main() { let datetime = Utc.ymd(2020, 1, 1).and_hms(0, 0, 0); let timestamp = datetime.timestamp(); let naive_datetime = NaiveDateTime::from_timestamp(timestamp, 0); let datetime_again: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc); println!("{}", datetime_again); } 

Playground

+4
source share

All Articles