#date #time #duration #instant

no-std easytime

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

5 releases

0.2.1 Apr 6, 2021
0.2.0 Jan 3, 2021
0.1.2 Mar 1, 2019
0.1.1 Feb 23, 2019
0.1.0 Feb 19, 2019

#30 in Date and time

Download history 108/week @ 2020-12-24 120/week @ 2020-12-31 67/week @ 2021-01-07 280/week @ 2021-01-14 82/week @ 2021-01-21 108/week @ 2021-01-28 72/week @ 2021-02-04 93/week @ 2021-02-11 76/week @ 2021-02-18 245/week @ 2021-02-25 63/week @ 2021-03-04 63/week @ 2021-03-11 91/week @ 2021-03-18 109/week @ 2021-03-25 140/week @ 2021-04-01 83/week @ 2021-04-08

485 downloads per month

Apache-2.0 OR MIT

39KB
548 lines

easytime

crates.io docs.rs license rustc build status

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.34+

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.

Dependencies