やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Rust自習(日時 7 chrono)

 エポックと日付型の相互変換。

成果物

日付→エポック

コード

fn main() {
    let utc = chrono::Utc::now();
    println!("{}", utc.timestamp());   // エポック(UNIX)時刻

    let local = chrono::Local::now();
    println!("{}", local.timestamp()); // エポック(UNIX)時刻
}

実行結果

$ cargo run
...`
2019-07-26 07:41:48.706650482 +09:00
1564094508
1564094508

エポック→日付

コード

use chrono::{Utc, Local, TimeZone};
fn main() {
    let utc = Utc::now();
    println!("{}", utc.timestamp());
    println!("{}", Utc.timestamp(utc.timestamp(), 0));

    let local = Local::now();
    println!("{}", local.timestamp());
    println!("{}", Local.timestamp(local.timestamp(), 0));
}

実行結果

$ cargo run
...`
1564095292
2019-07-25 22:54:52 UTC
1564095292
2019-07-26 07:54:52 +09:00

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1219 SMP Tue May 14 21:20:58 BST 2019 armv7l GNU/Linux

前回まで