11 个版本
| 0.2.7 | 2024 年 3 月 5 日 | 
|---|---|
| 0.2.6 | 2023 年 8 月 26 日 | 
| 0.2.5 | 2023 年 7 月 27 日 | 
| 0.2.4 | 2022 年 9 月 5 日 | 
| 0.1.2 | 2019 年 3 月 1 日 | 
#62 在 日期和时间
每月 586 次下载
44KB
551 行
easytime
提供对瞬间和持续时间进行安全检查算术的包装类型。
此 crate 提供以下两个数据结构。
- 
easytime::Instant--std::time::Instant的包装类型
- 
easytime::Duration--std::time::Duration的包装类型
用法
将此内容添加到您的 Cargo.toml
[dependencies]
easytime = "0.2"
编译器支持:需要 rustc 1.58+
示例
use easytime::{Duration, Instant};
use std::time::Duration as StdDuration;
fn foo(secs: u64, nanos: u32, instant: Instant) -> Option<StdDuration> {
    let now = Instant::now();
    let dur = Duration::new(secs, nanos);
    (now - instant - dur).into_inner()
}
如果您直接使用 std::time,您需要编写如下内容
use std::time::{Duration, Instant};
fn foo(secs: u64, nanos: u32, instant: Instant) -> Option<Duration> {
    let now = Instant::now();
    let secs = Duration::from_secs(secs);
    let nanos = Duration::from_nanos(nanos as u64);
    let dur = secs.checked_add(nanos)?;
    now.checked_duration_since(instant)?.checked_sub(dur)
}
可选功能
- std(默认启用)- 启用以使用 easytime::Instant。
- 如果禁用此功能,则可以在 no_std环境中使用easytime。
 
- 启用以使用 
许可证
根据您的选择,在 Apache 许可证,版本 2.0 或 MIT 许可证 下许可。
除非您明确说明,否则根据 Apache-2.0 许可证定义,您提交的任何旨在包含在作品中的贡献都应按上述方式双许可,而无需任何附加条款或条件。