美文网首页
rust时间相关方法

rust时间相关方法

作者: lesterhnu | 来源:发表于2021-06-23 16:17 被阅读0次

一.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()
}

相关文章

网友评论

      本文标题:rust时间相关方法

      本文链接:https://www.haomeiwen.com/subject/zzmbyltx.html