1月 06, 2024 LaGee
time::OffsetDateTime數字轉成時間
time::OffsetDateTime[1]主要作用就是顯示日期
他可以將數字轉換成日期
也可以將日期轉換為數字
最多可有奈米精度
數字指的是Unix Time[2]
以1970年1月1日0:00:00為起始
常用的方法
pub const UNIX_EPOCH: Self = _
UNIX_EPOCH就是最初始值,也就是1970年1月1號0點
use time::OffsetDateTime;
fn main() {
println!("{:?}", OffsetDateTime::UNIX_EPOCH);
}
1970-01-01 0:00:00.0 +00:00:00
pub fn now_utc() -> Self
now_utc就是直接輸出現在的時間
如果要改成台灣時間記得UTC+8
use time::OffsetDateTime;
fn main() {
println!("{:?}", OffsetDateTime::now_utc());
}
2024-01-06 5:18:38.2221338 +00:00:00
pub const fn from_unix_timestamp(timestamp: i64) -> Result<Self, ComponentRange>
from_unix_timestamp可以將unix_timestamp轉換為日期
最後輸出是Result要unwrap
例如
use time::OffsetDateTime;
fn main() {
let time = OffsetDateTime::from_unix_timestamp(1758634950);
println!("{:?}", time.unwrap());
}
2025-09-23 13:42:30.0 +00:00:00也可以使用from_unix_timestamp_nanos[6]達到奈秒精度
pub const fn unix_timestamp(self) -> i64
而unix_timestamp則是反過來
可以將日期轉換為unix timestamp
use time::OffsetDateTime;
fn main() {
let time = OffsetDateTime::now_utc();
let timestamp = time.unix_timestamp();
println!("{}", timestamp);
}
1704519287也有unix_timestamp_nanos[8]取得奈米精度
pub const fn second(self) -> u8
second可以獲得time的時間
例如
use time::OffsetDateTime;
fn main() {
let time = OffsetDateTime::now_utc();
let time_sec = time.second();
println!("{}", time);
println!("{}", time_sec);
}
2024-01-06 5:46:18.4763776 +00:00:00
18
同時也有year、month、day、hour、minute、millisecond、microsecond、nanosecond
use time::OffsetDateTime;
fn main() {
let time = OffsetDateTime::now_utc();
let time_year = time.year();
let time_month = time.month();
let time_day = time.day();
let time_hour = time.hour();
let time_minute = time.minute();
let time_sec = time.second();
let time_millisec = time.millisecond();
let time_microsec = time.microsecond();
let time_nanosec = time.nanosecond();
println!("{}", time);
println!("{}", time_year);
println!("{}", time_month);
println!("{}", time_day);
println!("{}", time_hour);
println!("{}", time_minute);
println!("{}", time_sec);
println!("{}", time_millisec);
println!("{}", time_microsec);
println!("{}", time_nanosec);
}
2024-01-06 5:48:54.7823054 +00:00:00
2024
January
6
5
48
54
782
782305
782305400
加減法
OffsetDateTime也可以加減
舉個例子
use time::{OffsetDateTime, Duration};
fn main() {
let timestamp_start = OffsetDateTime::from_unix_timestamp(1704520785).unwrap();
let timestamp_end = OffsetDateTime::from_unix_timestamp(1704520845).unwrap();
let duration: Duration = timestamp_end - timestamp_start;
println!("{}", duration);
}
1m
但要注意剪完會變成Duration[10]
參考資料
[1] https://docs.rs/time/latest/time/struct.OffsetDateTime.html#
[2] https://zh.wikipedia.org/zh-tw/UNIX%E6%97%B6%E9%97%B4
[3] https://docs.rs/time/latest/time/struct.OffsetDateTime.html#associatedconstant.UNIX_EPOCH
[4] https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.now_utc
[5] https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.from_unix_timestamp
[6] https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.from_unix_timestamp_nanos
[7] https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.unix_timestamp
[8] https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.unix_timestamp_nanos
[9] https://docs.rs/time/latest/time/struct.OffsetDateTime.html#method.second
[10] https://lageeblog.blogspot.com/2024/01/rust-timeduration.html
0 comments:
張貼留言