tokio_easy_timer/interval.rs
1#[derive(Eq, PartialEq, Debug, Copy, Clone)]
2pub enum Interval {
3 /// The next multiple of `n` seconds since the start of the Unix epoch
4 Seconds(u32),
5 /// The next multiple of `n` minutes since the start of the day
6 Minutes(u32),
7 /// The next multiple of `n` hours since the start of the day
8 Hours(u32),
9 /// The next multiple of `n` days since the start of the start of the era
10 Days(u32),
11 /// The next multiple of `n` months since the start of the start of the era
12 Months(u32),
13 /// The next multiple of `n` weeks since the start of the start of the era
14 Weeks(u32),
15 /// The next multiple of `n` years since the start of the start of the era
16 Years(u32),
17 /// Every Monday
18 Monday,
19 /// Every Tuesday
20 Tuesday,
21 /// Every Wednesday
22 Wednesday,
23 /// Every Thursday
24 Thursday,
25 /// Every Friday
26 Friday,
27 /// Every Saturday
28 Saturday,
29 /// Every Sunday
30 Sunday,
31 /// Every weekday (Monday through Friday)
32 Weekday,
33}
34
35impl Interval {
36 pub(crate) fn to_sec(&self) -> u64 {
37 match self {
38 Interval::Seconds(x) => *x as u64,
39 Interval::Minutes(x) => *x as u64 * 60,
40 Interval::Hours(x) => *x as u64 * 3600,
41 Interval::Days(x) => *x as u64 * 3600 * 24,
42 Interval::Weeks(x) => *x as u64 * 3600 * 24 * 7,
43 _ => unimplemented!(),
44 }
45 }
46}
47
48pub trait TimeUnits: Sized {
49 fn seconds(self) -> Interval;
50 fn minutes(self) -> Interval;
51 fn hours(self) -> Interval;
52 fn days(self) -> Interval;
53 fn months(self) -> Interval;
54 fn weeks(self) -> Interval;
55 fn years(self) -> Interval;
56
57 fn second(self) -> Interval {
58 self.seconds()
59 }
60 fn minute(self) -> Interval {
61 self.minutes()
62 }
63 fn hour(self) -> Interval {
64 self.hours()
65 }
66 fn day(self) -> Interval {
67 self.days()
68 }
69 fn month(self) -> Interval {
70 self.months()
71 }
72 fn year(self) -> Interval {
73 self.years()
74 }
75 fn week(self) -> Interval {
76 self.weeks()
77 }
78}
79
80impl TimeUnits for u32 {
81 /// Turn u32 to Interval::Seconds(u32)
82 ///
83 /// # Example
84 /// ```rust
85 /// let a = 1.seconds();
86 /// assert_eq!(a, Interval::Seconds(1));
87 /// ```
88 fn seconds(self) -> Interval {
89 assert!(self < 60);
90 Interval::Seconds(self)
91 }
92
93 /// Turn u32 to Interval::Minutes(u32)
94 ///
95 /// # Example
96 /// ```rust
97 /// let a = 1.minutes();
98 /// assert_eq!(a, Interval::Minutes(1));
99 /// ```
100 fn minutes(self) -> Interval {
101 assert!(self < 60);
102 Interval::Minutes(self)
103 }
104
105 /// Turn u32 to Interval::Hours(u32)
106 ///
107 /// # Example
108 /// ```rust
109 /// let a = 1.hours();
110 /// assert_eq!(a, Interval::Hours(1));
111 /// ```
112 fn hours(self) -> Interval {
113 assert!(self < 24);
114 Interval::Hours(self)
115 }
116
117 /// Turn u32 to Interval::Days(u32)
118 ///
119 /// # Example
120 /// ```rust
121 /// let a = 1.days();
122 /// assert_eq!(a, Interval::Days(1));
123 /// ```
124 fn days(self) -> Interval {
125 assert!(self <= 31);
126 Interval::Days(self)
127 }
128
129 /// Turn u32 to Interval::Months(u32)
130 ///
131 /// # Example
132 /// ```rust
133 /// let a = 1.months();
134 /// assert_eq!(a, Interval::Months(1));
135 /// ```
136 fn months(self) -> Interval {
137 assert!(self <= 12);
138 Interval::Months(self)
139 }
140
141 /// Turn u32 to Interval::Weeks(u32)
142 ///
143 /// # Example
144 /// ```rust
145 /// let a = 1.weeks();
146 /// assert_eq!(a, Interval::Weeks(1));
147 /// ```
148 fn weeks(self) -> Interval {
149 assert!(self <= 7);
150 Interval::Weeks(self)
151 }
152
153 /// Turn u32 to Interval::Years(u32)
154 ///
155 /// # Example
156 /// ```rust
157 /// let a = 1.years();
158 /// assert_eq!(a, Interval::Years(1));
159 /// ```
160 fn years(self) -> Interval {
161 assert!(self <= 2100);
162 Interval::Years(self)
163 }
164}