time_now/
lib.rs

1//! # time-now
2//!
3//! Get _current time_ as seconds or milli/micro/nano seconds, since unix epoch time, using _single function_ call.
4//!
5//! Pure Rust `std` library based. No dependencies on any other crate.
6//!
7//! Safe and Lightweight (~2 kb).
8//!
9//! # Examples:
10//! ```
11//!use time_now;
12//!
13//!fn main() {
14//!    println!("time_now::now_as_secs    : {:?}", time_now::now_as_secs());
15//!    println!(
16//!        "time_now::now_as_secs_f32: {:?}",
17//!        time_now::now_as_secs_f32()
18//!    );
19//!    println!(
20//!        "time_now::now_as_secs_f64: {:?}",
21//!        time_now::now_as_secs_f64()
22//!    );
23//!    println!("time_now::now_as_millis  : {:?}", time_now::now_as_millis());
24//!    println!("time_now::now_as_micros  : {:?}", time_now::now_as_micros());
25//!    println!("time_now::now_as_nanos   : {:?}", time_now::now_as_nanos());
26//!    println!(
27//!        "time_now::duration_since_epoch: {:?}",
28//!        time_now::duration_since_epoch()
29//!    );
30//!
31//!    println!("NOTE: Each function calls std::time::SystemTime::now(),
32//!      hence each subsequent call will be ahead of time compare to previous call");
33//!}
34//! ```
35//! # Stability
36//! Only stable functions have been included.
37//! The _Nightly-only experimental APIs_ (`Duration::as_millis_f32()` and `Duration::as_millis_f64()`
38//! have not been included. They'll be included in future, once they become stable.
39//!
40
41use std::time::{Duration, SystemTime, UNIX_EPOCH};
42
43/// Returns [`std::time::Duration`] since [`UNIX_EPOCH`]. Uses: [`std::time`].
44/// The returned `Duration` is _safe_ unwrap of `Result` returned by `SystemTime::now().duration_since(UNIX_EPOCH)`. The _unwrap is safe_ because the `UNIX_EPOCH` is _earlier than now_.
45pub fn duration_since_epoch() -> Duration {
46    SystemTime::now()
47        .duration_since(UNIX_EPOCH)
48        .expect("This shouldn't happen since UNIX_EPOCH is EARLIER than NOW")
49}
50
51/// Returns the number of _whole_ microseconds since [`UNIX_EPOCH`]. Uses: [`std::time`].
52pub fn now_as_micros() -> u128 {
53    duration_since_epoch().as_micros()
54}
55
56/// Returns the number of _whole_ milliseconds since [`UNIX_EPOCH`]. Uses: [`std::time`].
57pub fn now_as_millis() -> u128 {
58    duration_since_epoch().as_millis()
59}
60
61/// Returns the number of nanoseconds since [`UNIX_EPOCH`]. Uses: [`std::time`].
62pub fn now_as_nanos() -> u128 {
63    duration_since_epoch().as_nanos()
64}
65
66/// Returns the number of _whole_ seconds since [`UNIX_EPOCH`]. Uses: [`std::time`].
67///
68/// As per [std docs](https://doc.rust-lang.org/std/time/struct.Duration.html#method.as_secs): _The returned value does not include the fractional (nanosecond) part of the duration, which can be obtained using subsec_nanos_.
69pub fn now_as_secs() -> u64 {
70    duration_since_epoch().as_secs()
71}
72
73/// Returns the number of seconds since [`UNIX_EPOCH`], as `f32`. Uses: [`std::time`].
74///
75/// As per [std docs](https://doc.rust-lang.org/1.88.0/core/time/struct.Duration.html#method.as_secs_f32): _The returned value includes the fractional (nanosecond) part of the duration_.
76pub fn now_as_secs_f32() -> f32 {
77    duration_since_epoch().as_secs_f32()
78}
79
80/// Returns the number of seconds since [`UNIX_EPOCH`], as `f64`. Uses: [`std::time`].
81///
82/// As per [std docs](https://doc.rust-lang.org/1.88.0/core/time/struct.Duration.html#method.as_secs_f64): _The returned value includes the fractional (nanosecond) part of the duration_.
83pub fn now_as_secs_f64() -> f64 {
84    duration_since_epoch().as_secs_f64()
85}
86
87#[cfg(test)]
88mod tests {
89    use super::*;
90
91    #[test]
92    fn duration_since_epoch_works() {
93        let _result = duration_since_epoch();
94    }
95
96    #[test]
97    fn now_as_micros_works() {
98        let _result = now_as_micros();
99    }
100
101    #[test]
102    fn now_as_millis_works() {
103        let _result = now_as_millis();
104    }
105
106    #[test]
107    fn now_as_nanos_works() {
108        let _result = now_as_nanos();
109    }
110
111    #[test]
112    fn now_as_secs_works() {
113        let _result = now_as_secs();
114    }
115
116    #[test]
117    fn now_as_secs_f32_works() {
118        let _result = now_as_secs_f32();
119    }
120
121    #[test]
122    fn now_as_secs_f64_works() {
123        let _result = now_as_secs_f64();
124    }
125}