簡體   English   中英

如何在 Rust 中獲取當前日期和時間的時間戳

[英]How to get Timestamp of the current Date and time in Rust

我只想檢索當前的時間和日期並將其存儲在一個變量中。 為此,我嘗試使用 chrono::DateTime。

在文檔中,我發現了這一點:

use chrono::{DateTime, TimeZone, NaiveDateTime, Utc};

let dt = DateTime::<Utc>::from_utc(NaiveDate::from_ymd(2016, 7, 8).and_hms(9, 10, 11), Utc);    

這讓我可以存儲特定的日期和時間,但我無法弄清楚如何檢索實際的當前日期和時間並將其放入我的日期時間變量中。

使用生銹,使用它now

use chrono;

fn main() {
    println!("{:?}", chrono::offset::Local::now());
    println!("{:?}", chrono::offset::Utc::now());
}

要回答標題中如何獲取當前時間戳的問題:

use chrono::Utc;

let dt = Utc::now();
let timestamp: i64 = dt.timestamp();

println!("Current timestamp is {}", timestamp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM