rust时间相关方法

一.rust获取时间戳

use std::time::{SystemTime, UNIX_EPOCH};

fn get_current_unix() -> u64 {
    let unix = SystemTime::now().duration_since(UNIX_EPOCH).expect("get_current_unix_err");
    unix.as_secs()
}

使用time包获取时间戳
Cargo.toml


image.png
// 精确到秒
fn get_unix()->i64{
    time::OffsetDateTime::now_utc().unix_timestamp()
}
// 精确到纳秒
fn get_unix_nano()->i128{
    time::OffsetDateTime::now_utc().unix_timestamp_nanos()
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容