#duration #instant #date-time #date #time #no-alloc

no-std easytime

Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations

11 releases

0.2.7 Mar 5, 2024
0.2.6 Aug 26, 2023
0.2.5 Jul 27, 2023
0.2.4 Sep 5, 2022
0.1.2 Mar 1, 2019

#68 in Date and time

Download history 88/week @ 2024-08-20 77/week @ 2024-08-27 73/week @ 2024-09-03 72/week @ 2024-09-10 79/week @ 2024-09-17 107/week @ 2024-09-24 79/week @ 2024-10-01 83/week @ 2024-10-08 92/week @ 2024-10-15 144/week @ 2024-10-22 125/week @ 2024-10-29 78/week @ 2024-11-05 80/week @ 2024-11-12 109/week @ 2024-11-19 71/week @ 2024-11-26 85/week @ 2024-12-03

354 downloads per month

Apache-2.0 OR MIT

44KB
551 lines

easytime

crates.io docs.rs license rust version github actions

Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations.

This crate provides the following two data structures.

Usage

Add this to your Cargo.toml:

[dependencies]
easytime = "0.2"

Compiler support: requires rustc 1.58+

Examples

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

If you use std::time directly, you need to write as follows:

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)
}

Optional features

  • std (enabled by default)
    • Enable to use easytime::Instant.
    • If disabled this feature, easytime can be used in no_std environments.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps

Features