https://github.com/taiki-e/easytime
Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations.
https://github.com/taiki-e/easytime
date no-std rust time
Last synced: 5 months ago
JSON representation
Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations.
- Host: GitHub
- URL: https://github.com/taiki-e/easytime
- Owner: taiki-e
- License: apache-2.0
- Created: 2019-02-19T12:36:11.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2025-03-09T09:12:20.000Z (over 1 year ago)
- Last Synced: 2025-03-09T10:19:40.376Z (over 1 year ago)
- Topics: date, no-std, rust, time
- Language: Rust
- Homepage: https://docs.rs/easytime
- Size: 468 KB
- Stars: 14
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# easytime
[](https://crates.io/crates/easytime)
[](https://docs.rs/easytime)
[](#license)
[](https://www.rust-lang.org)
[](https://github.com/taiki-e/easytime/actions)
Providing wrapper types for safely performing panic-free checked arithmetic
on instants and durations.
This crate provides the following two data structures.
- [`easytime::Instant`] -- A wrapper type for [`std::time::Instant`]
- [`easytime::Duration`] -- A wrapper type for [`std::time::Duration`]
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
easytime = "0.2"
```
## Examples
```rust
use easytime::{Duration, Instant};
use std::time::Duration as StdDuration;
fn foo(secs: u64, nanos: u32, instant: Instant) -> Option {
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:
```rust
use std::time::{Duration, Instant};
fn foo(secs: u64, nanos: u32, instant: Instant) -> Option {
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.
[`easytime::Instant`]: https://docs.rs/easytime/latest/easytime/struct.Instant.html
[`easytime::Duration`]: https://docs.rs/easytime/latest/easytime/struct.Duration.html
[`std::time`]: https://doc.rust-lang.org/std/time/index.html
[`std::time::Instant`]: https://doc.rust-lang.org/std/time/struct.Instant.html
[`std::time::Duration`]: https://doc.rust-lang.org/std/time/struct.Duration.html
## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
[MIT license](LICENSE-MIT) 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.