clockwork_cron/
queries.rs

1use chrono::offset::TimeZone;
2use chrono::{DateTime, Datelike, Duration, Timelike};
3
4use crate::ordinal::Ordinal;
5use crate::time_unit::{DaysOfMonth, Hours, Minutes, Months, Seconds, TimeUnitField};
6
7// TODO: Possibility of one query struct?
8
9pub struct NextAfterQuery<Z>
10where
11    Z: TimeZone,
12{
13    initial_datetime: DateTime<Z>,
14    first_month: bool,
15    first_day_of_month: bool,
16    first_hour: bool,
17    first_minute: bool,
18    first_second: bool,
19}
20
21impl<Z> NextAfterQuery<Z>
22where
23    Z: TimeZone,
24{
25    pub fn from(after: &DateTime<Z>) -> NextAfterQuery<Z> {
26        NextAfterQuery {
27            initial_datetime: after.clone() + Duration::seconds(1),
28            first_month: true,
29            first_day_of_month: true,
30            first_hour: true,
31            first_minute: true,
32            first_second: true,
33        }
34    }
35
36    pub fn year_lower_bound(&self) -> Ordinal {
37        // Unlike the other units, years will never wrap around.
38        self.initial_datetime.year() as u32
39    }
40
41    pub fn month_lower_bound(&mut self) -> Ordinal {
42        if self.first_month {
43            self.first_month = false;
44            return self.initial_datetime.month();
45        }
46        Months::inclusive_min()
47    }
48
49    pub fn reset_month(&mut self) {
50        self.first_month = false;
51        self.reset_day_of_month();
52    }
53
54    pub fn day_of_month_lower_bound(&mut self) -> Ordinal {
55        if self.first_day_of_month {
56            self.first_day_of_month = false;
57            return self.initial_datetime.day();
58        }
59        DaysOfMonth::inclusive_min()
60    }
61
62    pub fn reset_day_of_month(&mut self) {
63        self.first_day_of_month = false;
64        self.reset_hour();
65    }
66
67    pub fn hour_lower_bound(&mut self) -> Ordinal {
68        if self.first_hour {
69            self.first_hour = false;
70            return self.initial_datetime.hour();
71        }
72        Hours::inclusive_min()
73    }
74
75    pub fn reset_hour(&mut self) {
76        self.first_hour = false;
77        self.reset_minute();
78    }
79
80    pub fn minute_lower_bound(&mut self) -> Ordinal {
81        if self.first_minute {
82            self.first_minute = false;
83            return self.initial_datetime.minute();
84        }
85        Minutes::inclusive_min()
86    }
87
88    pub fn reset_minute(&mut self) {
89        self.first_minute = false;
90        self.reset_second();
91    }
92
93    pub fn second_lower_bound(&mut self) -> Ordinal {
94        if self.first_second {
95            self.first_second = false;
96            return self.initial_datetime.second();
97        }
98        Seconds::inclusive_min()
99    }
100
101    pub fn reset_second(&mut self) {
102        self.first_second = false;
103    }
104} // End of impl
105
106pub struct PrevFromQuery<Z>
107where
108    Z: TimeZone,
109{
110    initial_datetime: DateTime<Z>,
111    first_month: bool,
112    first_day_of_month: bool,
113    first_hour: bool,
114    first_minute: bool,
115    first_second: bool,
116}
117
118impl<Z> PrevFromQuery<Z>
119where
120    Z: TimeZone,
121{
122    pub fn from(before: &DateTime<Z>) -> PrevFromQuery<Z> {
123        PrevFromQuery {
124            initial_datetime: before.clone() - Duration::seconds(1),
125            first_month: true,
126            first_day_of_month: true,
127            first_hour: true,
128            first_minute: true,
129            first_second: true,
130        }
131    }
132
133    pub fn year_upper_bound(&self) -> Ordinal {
134        // Unlike the other units, years will never wrap around.
135        self.initial_datetime.year() as u32
136    }
137
138    pub fn month_upper_bound(&mut self) -> Ordinal {
139        if self.first_month {
140            self.first_month = false;
141            return self.initial_datetime.month();
142        }
143        Months::inclusive_max()
144    }
145
146    pub fn reset_month(&mut self) {
147        self.first_month = false;
148        self.reset_day_of_month();
149    }
150
151    pub fn day_of_month_upper_bound(&mut self) -> Ordinal {
152        if self.first_day_of_month {
153            self.first_day_of_month = false;
154            return self.initial_datetime.day();
155        }
156        DaysOfMonth::inclusive_max()
157    }
158
159    pub fn reset_day_of_month(&mut self) {
160        self.first_day_of_month = false;
161        self.reset_hour();
162    }
163
164    pub fn hour_upper_bound(&mut self) -> Ordinal {
165        if self.first_hour {
166            self.first_hour = false;
167            return self.initial_datetime.hour();
168        }
169        Hours::inclusive_max()
170    }
171
172    pub fn reset_hour(&mut self) {
173        self.first_hour = false;
174        self.reset_minute();
175    }
176
177    pub fn minute_upper_bound(&mut self) -> Ordinal {
178        if self.first_minute {
179            self.first_minute = false;
180            return self.initial_datetime.minute();
181        }
182        Minutes::inclusive_max()
183    }
184
185    pub fn reset_minute(&mut self) {
186        self.first_minute = false;
187        self.reset_second();
188    }
189
190    pub fn second_upper_bound(&mut self) -> Ordinal {
191        if self.first_second {
192            self.first_second = false;
193            return self.initial_datetime.second();
194        }
195        Seconds::inclusive_max()
196    }
197
198    pub fn reset_second(&mut self) {
199        self.first_second = false;
200    }
201}