1月 04, 2024 LaGee
time::Duration
本篇介紹time::Duration[1]
和std::time::Duration[2]有一點不一樣
雖然功能有很多很相似的
但time::Duration的功能比較多我也用比較習慣
time::Duration用來表示一個持續持間
使用Unix timestamp表示
精度是奈米等級的
可以輸入日期、小時、分鐘、秒等等
並對其作加減
舉個簡單的例子
use time::Duration;
fn main() {
let time_start = Duration::new(0, 111111111);
let time_end = Duration::nanoseconds(66123456789);
let duration = time_end - time_start;
println!("minute: {:?}", duration.whole_minutes());
println!("second: {:?}", duration.whole_seconds());
println!("millisecond: {:?}", duration.subsec_milliseconds());
println!("microsecond: {:?}", duration.subsec_microseconds());
println!("nanosecond: {:?}", duration.subsec_nanoseconds());
let min = duration.whole_minutes();
let sec = duration.whole_seconds() % 60;
let nanoseconds = duration.subsec_nanoseconds();
println!("duration: {:02}:{:02}.{:09}", min, sec, nanoseconds);
}
minute: 1
second: 66
millisecond: 12
microsecond: 12345
nanosecond: 12345678 duration: 01:06.012345678
一個一個介紹常用到的幾個方法
pub const fn new(seconds: i64, nanoseconds: i32) -> Self
輸入兩個參數
一個是起始的秒鐘
另一個是後續的奈秒
例如程式範例就是time_start就是0.111111111秒
(weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds)
pub const fn weeks(weeks: i64) -> Self
pub const fn days(days: i64) -> Self
pub const fn hours(hours: i64) -> Self
pub const fn minutes(minutes: i64) -> Self
pub const fn seconds(seconds: i64) -> Self
pub const fn milliseconds(milliseconds: i64) -> Self
pub const fn microseconds(microseconds: i64) -> Self
pub const fn nanoseconds(nanoseconds: i64) -> Self
這幾個做法都是把數字轉為指定時間
例如
assert_eq!(Duration::minutes(1), 60.seconds());
就是1分鐘和60秒比較
以程式例子來說
就是66123456789轉成奈秒
pub const fn whole_weeks(self) -> i64
pub const fn whole_days(self) -> i64
pub const fn whole_hours(self) -> i64
pub const fn whole_minutes(self) -> i64
pub const fn whole_seconds(self) -> i64
pub const fn whole_milliseconds(self) -> i128
pub const fn whole_microseconds(self) -> i128
pub const fn whole_nanoseconds(self) -> i128
以上方程式例子來說
whole_minutes可以獲得分鐘
whole_seconds則是可以獲得秒鐘
但是會獲得所有秒鐘
也就是如果是1分16秒
使用whole_minutes會出現1,whole_seconds則是出現76
pub const fn subsec_milliseconds(self) -> i16
pub const fn subsec_microseconds(self) -> i32
pub const fn subsec_nanoseconds(self) -> i32
subsec系列有三個
用途為取得超過秒鐘的時間
例如程式的subsec_nanoseconds就是獲得奈秒
只是跟whole有些不同,已經先把秒鐘去掉了
其他補充
Duration是有基本運算的trait
也可以對OffsetDateTime加減
而下方程式為將時間轉成分鐘:秒鐘.奈秒
let min = duration.whole_minutes();
let sec = duration.whole_seconds() % 60;
let nanoseconds = duration.subsec_nanoseconds();
println!("duration: {:02}:{:02}.{:09}", min, sec, nanoseconds);
因為秒鐘會獲得全部秒鐘,要%60取餘數
奈秒則是使用減去秒鐘後的數值
{:02}表示至少出現兩位數,低於兩位數會補0
Duration常與OffsetDateTime配合
更多資訊可以看一下另外一篇[7]
參考資料
[1] https://docs.rs/time/latest/time/struct.Duration.html
[2] https://doc.rust-lang.org/std/time/struct.Duration.html
[3] https://docs.rs/time/latest/time/struct.Duration.html#method.new
[4] https://docs.rs/time/latest/time/struct.Duration.html#method.seconds
[5] https://docs.rs/time/latest/time/struct.Duration.html#method.whole_seconds
[6] https://docs.rs/time/latest/time/struct.Duration.html#method.subsec_milliseconds
[7] https://lageeblog.blogspot.com/2024/01/rust-timeoffsetdatetime.html
0 comments:
張貼留言