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

use std::time::Duration;

pub trait NumTimeDuration: num_traits::PrimInt {
    /// Creates a new Duration from the specified number of nanoseconds.
    ///
    /// ```rust
    /// use num_time_duration::NumTimeDuration;
    /// use std::time::Duration;
    ///
    /// assert_eq!(1.nanoseconds(), Duration::from_nanos(1));
    /// ```
    fn nanoseconds(&self) -> Duration;

    /// Creates a new Duration from the specified number of microseconds.
    ///
    /// ```rust
    /// use num_time_duration::NumTimeDuration;
    /// use std::time::Duration;
    ///
    /// assert_eq!(1.microseconds(), Duration::from_micros(1));
    /// ```
    fn microseconds(&self) -> Duration;

    /// Creates a new Duration from the specified number of milliseconds.
    ///
    /// ```rust
    /// use num_time_duration::NumTimeDuration;
    /// use std::time::Duration;
    ///
    /// assert_eq!(1.milliseconds(), Duration::from_millis(1));
    /// ```
    fn milliseconds(&self) -> Duration;

    /// Creates a new Duration from the specified number of seconds.
    ///
    /// ```rust
    /// use num_time_duration::NumTimeDuration;
    /// use std::time::Duration;
    ///
    /// assert_eq!(1.seconds(), Duration::from_secs(1));
    /// ```
    fn seconds(&self) -> Duration;

    /// Creates a new Duration from the specified number of minutes.
    ///
    /// ```rust
    /// use num_time_duration::NumTimeDuration;
    /// use std::time::Duration;
    ///
    /// assert_eq!(1.minutes(), Duration::from_secs(60));
    /// ```
    fn minutes(&self) -> Duration;

    /// Creates a new Duration from the specified number of hours.
    ///
    /// ```rust
    /// use num_time_duration::NumTimeDuration;
    /// use std::time::Duration;
    ///
    /// assert_eq!(1.hours(), Duration::from_secs(3600));
    /// ```
    fn hours(&self) -> Duration;

    /// Creates a new Duration from the specified number of days.
    ///
    /// ```rust
    /// use num_time_duration::NumTimeDuration;
    /// use std::time::Duration;
    ///
    /// assert_eq!(1.days(), Duration::from_secs(86400));
    /// ```
    fn days(&self) -> Duration;

    /// Creates a new Duration from the specified number of weeks.
    ///
    /// ```rust
    /// use num_time_duration::NumTimeDuration;
    /// use std::time::Duration;
    ///
    /// assert_eq!(1.weeks(), Duration::from_secs(604800));
    /// ```
    fn weeks(&self) -> Duration;
}

impl NumTimeDuration for i32 {
    fn nanoseconds(&self) -> Duration {
        Duration::from_nanos(*self as u64)
    }

    fn microseconds(&self) -> Duration {
        Duration::from_micros(*self as u64)
    }

    fn milliseconds(&self) -> Duration {
        Duration::from_millis(*self as u64)
    }

    fn seconds(&self) -> Duration {
        Duration::from_secs(*self as u64)
    }

    fn minutes(&self) -> Duration {
        let seconds: Duration = self.seconds();
        60 * seconds
    }

    fn hours(&self) -> Duration {
        let minutes: Duration = self.minutes();
        60 * minutes
    }

    fn days(&self) -> Duration {
        let hours: Duration = self.hours();
        24 * hours
    }

    fn weeks(&self) -> Duration {
        let days: Duration = self.days();
        7 * days
    }
}