如何将Unix时间/时间自时代转换为标准日期和时间?

我正在使用计时箱; 经过一番挖掘,我发现DateTime类型有一个函数timestamp() ,它可以产生类型i64纪元时间。 但是,我无法找到如何将其转换回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?
}

您首先需要创建一个NaiveDateTime ,然后再用它来创建一个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);
}

操场

链接地址: http://www.djcxy.com/p/95351.html

上一篇: How to convert Unix time / time since the epoch to standard date and time?

下一篇: Elastic search: find word parts in nested object