num_time_duration/
lib.rs

1//! This crate provides a convenient way to create `std::time::Duration` from numbers.
2//!
3//! Example:
4//!
5//! ```rust
6//! use num_time_duration::NumTimeDuration;
7//!
8//! let now = std::time::SystemTime::now();
9//! assert_eq!(now + 1.hours(), now + std::time::Duration::from_secs(3600));
10//! assert_eq!(now + 1.days(), now + std::time::Duration::from_secs(86400));
11//! assert_eq!(now - 1.weeks(), now - std::time::Duration::from_secs(604800));
12//! ```
13
14use std::time::Duration;
15
16pub trait NumTimeDuration: num_traits::PrimInt {
17    /// Creates a new Duration from the specified number of nanoseconds.
18    ///
19    /// ```rust
20    /// use num_time_duration::NumTimeDuration;
21    /// use std::time::Duration;
22    ///
23    /// assert_eq!(1.nanoseconds(), Duration::from_nanos(1));
24    /// ```
25    fn nanoseconds(&self) -> Duration;
26
27    /// Creates a new Duration from the specified number of microseconds.
28    ///
29    /// ```rust
30    /// use num_time_duration::NumTimeDuration;
31    /// use std::time::Duration;
32    ///
33    /// assert_eq!(1.microseconds(), Duration::from_micros(1));
34    /// ```
35    fn microseconds(&self) -> Duration;
36
37    /// Creates a new Duration from the specified number of milliseconds.
38    ///
39    /// ```rust
40    /// use num_time_duration::NumTimeDuration;
41    /// use std::time::Duration;
42    ///
43    /// assert_eq!(1.milliseconds(), Duration::from_millis(1));
44    /// ```
45    fn milliseconds(&self) -> Duration;
46
47    /// Creates a new Duration from the specified number of seconds.
48    ///
49    /// ```rust
50    /// use num_time_duration::NumTimeDuration;
51    /// use std::time::Duration;
52    ///
53    /// assert_eq!(1.seconds(), Duration::from_secs(1));
54    /// ```
55    fn seconds(&self) -> Duration;
56
57    /// Creates a new Duration from the specified number of minutes.
58    ///
59    /// ```rust
60    /// use num_time_duration::NumTimeDuration;
61    /// use std::time::Duration;
62    ///
63    /// assert_eq!(1.minutes(), Duration::from_secs(60));
64    /// ```
65    fn minutes(&self) -> Duration;
66
67    /// Creates a new Duration from the specified number of hours.
68    ///
69    /// ```rust
70    /// use num_time_duration::NumTimeDuration;
71    /// use std::time::Duration;
72    ///
73    /// assert_eq!(1.hours(), Duration::from_secs(3600));
74    /// ```
75    fn hours(&self) -> Duration;
76
77    /// Creates a new Duration from the specified number of days.
78    ///
79    /// ```rust
80    /// use num_time_duration::NumTimeDuration;
81    /// use std::time::Duration;
82    ///
83    /// assert_eq!(1.days(), Duration::from_secs(86400));
84    /// ```
85    fn days(&self) -> Duration;
86
87    /// Creates a new Duration from the specified number of weeks.
88    ///
89    /// ```rust
90    /// use num_time_duration::NumTimeDuration;
91    /// use std::time::Duration;
92    ///
93    /// assert_eq!(1.weeks(), Duration::from_secs(604800));
94    /// ```
95    fn weeks(&self) -> Duration;
96}
97
98impl NumTimeDuration for i32 {
99    fn nanoseconds(&self) -> Duration {
100        Duration::from_nanos(*self as u64)
101    }
102
103    fn microseconds(&self) -> Duration {
104        Duration::from_micros(*self as u64)
105    }
106
107    fn milliseconds(&self) -> Duration {
108        Duration::from_millis(*self as u64)
109    }
110
111    fn seconds(&self) -> Duration {
112        Duration::from_secs(*self as u64)
113    }
114
115    fn minutes(&self) -> Duration {
116        let seconds: Duration = self.seconds();
117        60 * seconds
118    }
119
120    fn hours(&self) -> Duration {
121        let minutes: Duration = self.minutes();
122        60 * minutes
123    }
124
125    fn days(&self) -> Duration {
126        let hours: Duration = self.hours();
127        24 * hours
128    }
129
130    fn weeks(&self) -> Duration {
131        let days: Duration = self.days();
132        7 * days
133    }
134}