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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
use crate::datetime::*;
use crate::iterinfo::*;
use crate::options::*;
use crate::poslist::*;
use crate::yearinfo::*;
use chrono::prelude::*;
use chrono::Duration;
use chrono_tz::{Tz, UTC};
pub enum QueryMethodTypes {
ALL,
BETWEEN,
BEFORE,
AFTER,
}
pub struct IterArgs {
pub inc: bool,
pub before: DateTime<Tz>,
pub after: DateTime<Tz>,
pub dt: DateTime<Tz>
}
pub struct IterResult {
pub method: QueryMethodTypes,
pub args: IterArgs,
pub min_date: Option<DateTime<Tz>>,
pub max_date: Option<DateTime<Tz>>,
pub result: Vec<DateTime<Tz>>,
pub total: usize,
}
impl IterResult {
pub fn new(method: QueryMethodTypes, args: IterArgs) -> Self {
let (max_date, min_date) = match method {
QueryMethodTypes::BETWEEN if args.inc => (Some(args.before), Some(args.after)),
QueryMethodTypes::BETWEEN => (
Some(args.before - Duration::milliseconds(1)),
Some(args.after + Duration::milliseconds(1)),
),
QueryMethodTypes::BEFORE if args.inc => (Some(args.dt), None),
QueryMethodTypes::BEFORE => (Some(args.dt - Duration::milliseconds(1)), None),
QueryMethodTypes::AFTER if args.inc => (None, Some(args.dt)),
QueryMethodTypes::AFTER => (None, Some(args.dt + Duration::milliseconds(1))),
_ => (None, None),
};
Self {
method,
args,
min_date,
max_date,
total: 0,
result: vec![],
}
}
pub fn accept(&mut self, date: DateTime<Tz>) -> bool {
self.total += 1;
let too_early = self.min_date.is_some() && date < self.min_date.unwrap();
let too_late = self.max_date.is_some() && date > self.max_date.unwrap();
match self.method {
QueryMethodTypes::BETWEEN if too_early => true,
QueryMethodTypes::BETWEEN if too_late => false,
QueryMethodTypes::BEFORE if too_late => false,
QueryMethodTypes::AFTER if too_early => true,
QueryMethodTypes::AFTER => {
self.add(date);
false
}
_ => self.add(date),
}
}
pub fn add(&mut self, date: DateTime<Tz>) -> bool {
self.result.push(date);
true
}
pub fn get_value(&self) -> Vec<DateTime<Tz>> {
self.result.clone()
//match self.method {
//QueryMethodTypes::BETWEEN => Some(self._result.clone()),
//_ => {
//if self._result.is_empty() {
//return None;
//}
//Some(vec![self._result[self._result.len() - 1].clone()])
//}
//}
}
}
pub fn iter(iter_result: &mut IterResult, options: &mut ParsedOptions) -> Vec<DateTime<Tz>> {
if (options.count.is_some() && options.count.unwrap() == 0) || options.interval == 0 {
return iter_result.get_value();
}
let mut counter_date = options.dtstart;
let mut ii = IterInfo::new(options);
ii.rebuild(counter_date.year() as isize, counter_date.month() as usize);
let mut timeset = make_timeset(&ii, &counter_date, options);
let mut count = match options.count {
Some(count) => count,
_ => 0,
};
loop {
let (dayset, start, end) = ii.getdayset(
&options.freq,
counter_date.year() as isize,
counter_date.month() as usize,
counter_date.day() as usize,
);
let mut dayset = dayset
.into_iter()
.map(|s| Some(s as isize))
.collect::<Vec<Option<isize>>>();
let filtered = remove_filtered_days(&mut dayset, start, end, &ii, options);
if not_empty(&options.bysetpos) {
let poslist = build_poslist(&options.bysetpos, ×et, start, end, &ii, &dayset);
for j in 0..poslist.len() {
let res = poslist[j];
if options.until.is_some() && res > options.until.unwrap() {
return iter_result.get_value();
}
if res >= options.dtstart {
//let rezoned_date = rezone_if_needed(&res, &options);
let rezoned_date = res;
if !iter_result.accept(UTC.timestamp(rezoned_date.timestamp(), 0)) {
return iter_result.get_value();
}
if count > 0 {
count -= 1;
if count == 0 {
return iter_result.get_value();
}
}
}
}
} else {
for j in start..end {
let current_day = dayset[j];
if current_day.is_none() {
continue;
}
let current_day = current_day.unwrap();
let date = from_ordinal(ii.yearordinal().unwrap() + current_day);
for k in 0..timeset.len() {
let res = Utc.ymd(date.year(), date.month(), date.day()).and_hms(
timeset[k].hour as u32,
timeset[k].minute as u32,
timeset[k].second as u32,
);
if options.until.is_some() && res > options.until.unwrap() {
return iter_result.get_value();
}
if res >= options.dtstart {
//let rezoned_date = rezone_if_needed(&res, &options);
let rezoned_date = res;
if !iter_result.accept(UTC.timestamp(rezoned_date.timestamp(), 0)) {
return iter_result.get_value();
}
if count > 0 {
count -= 1;
if count == 0 {
return iter_result.get_value();
}
}
}
}
}
}
if options.interval == 0 {
return iter_result.get_value();
}
// Handle frequency and interval
counter_date = increment_counter_date(counter_date, options, filtered);
if counter_date.year() > 2200 {
return iter_result.get_value();
}
if options.freq == Frequenzy::HOURLY
|| options.freq == Frequenzy::MINUTELY
|| options.freq == Frequenzy::SECONDLY
{
timeset = ii.gettimeset(
&options.freq,
counter_date.hour() as usize,
counter_date.minute() as usize,
counter_date.second() as usize,
0,
);
}
ii.rebuild(counter_date.year() as isize, counter_date.month() as usize);
}
}
pub fn increment_counter_date(
counter_date: DateTime<Utc>,
options: &ParsedOptions,
filtered: bool,
) -> DateTime<Utc> {
match options.freq {
Frequenzy::YEARLY => counter_date
.with_year(counter_date.year() + options.interval as i32)
.unwrap(),
Frequenzy::MONTHLY => {
let new_month = counter_date.month() + options.interval as u32;
if new_month > 12 {
let mut year_div = new_month / 12;
let mut new_month = new_month % 12;
if new_month == 0 {
new_month = 12;
year_div -= 1;
}
let new_year = counter_date.year() + year_div as i32;
counter_date
.with_month(new_month)
.unwrap()
.with_year(new_year)
.unwrap()
} else {
counter_date.with_month(new_month).unwrap()
}
}
Frequenzy::WEEKLY => {
let mut day_delta = 0;
let weekday = get_weekday_val(&counter_date.weekday());
if options.wkst > weekday {
day_delta += -((weekday + 1 + (6 - options.wkst)) as isize)
+ (options.interval as isize) * 7;
} else {
day_delta += -((weekday - options.wkst) as isize) + (options.interval as isize) * 7;
}
counter_date + Duration::days(day_delta as i64)
}
Frequenzy::DAILY => counter_date + Duration::days(options.interval as i64),
Frequenzy::HOURLY => {
let mut new_hours = counter_date.hour() as usize;
if filtered {
new_hours += ((23 - new_hours) as f32 / options.interval as f32).floor() as usize
* options.interval;
}
loop {
new_hours += options.interval;
if options.byhour.is_empty()
|| options
.byhour
.iter()
.any(|bh| *bh == (new_hours % 24) as usize)
{
break;
}
}
counter_date.with_hour(0).unwrap() + Duration::hours(new_hours as i64)
}
Frequenzy::MINUTELY => counter_date + Duration::minutes(options.interval as i64),
Frequenzy::SECONDLY => counter_date + Duration::seconds(options.interval as i64),
}
}
pub fn includes<T>(v: &Vec<T>, el: &T) -> bool
where
T: PartialEq,
{
v.iter().any(|ve| ve == el)
}
pub fn not_empty<T>(v: &Vec<T>) -> bool {
!v.is_empty()
}
pub fn is_filtered(ii: &IterInfo, current_day: usize, options: &ParsedOptions) -> bool {
return (not_empty(&options.bymonth)
&& !includes(&options.bymonth, &ii.mmask().unwrap()[current_day]))
|| (not_empty(&options.byweekno) && (ii.wnomask().unwrap()[current_day]) == 0)
|| (not_empty(&options.byweekday)
&& !includes(&options.byweekday, &ii.wdaymask().unwrap()[current_day]))
|| (ii.nwdaymask().is_some()
&& not_empty(ii.nwdaymask().unwrap())
&& (ii.nwdaymask().unwrap()[current_day]) == 0)
|| (options.byeaster.is_some()
&& !(includes(ii.eastermask().unwrap(), &(current_day as isize))))
|| ((not_empty(&options.bymonthday) || not_empty(&options.bynmonthday))
&& !includes(&options.bymonthday, &ii.mdaymask().unwrap()[current_day])
&& !includes(&options.bynmonthday, &ii.nmdaymask().unwrap()[current_day]))
|| (not_empty(&options.byyearday)
&& ((current_day < ii.yearlen().unwrap()
&& !includes(&options.byyearday, &(current_day as isize + 1))
&& !includes(
&options.byyearday.iter().map(|v| *v as isize).collect(),
&(-(ii.yearlen().unwrap() as isize) + current_day as isize),
))
|| (current_day >= ii.yearlen().unwrap()
&& !includes(
&options.byyearday,
&((current_day + 1 - ii.yearlen().unwrap()) as isize),
)
&& !includes(
&options.byyearday.iter().map(|v| *v as isize).collect(),
&(-(ii.nextyearlen().unwrap() as isize) + current_day as isize
- ii.yearlen().unwrap() as isize),
))));
}
pub fn remove_filtered_days(
dayset: &mut Vec<Option<isize>>,
start: usize,
end: usize,
ii: &IterInfo,
options: &ParsedOptions,
) -> bool {
let mut filtered = false;
for daycounter in start..end {
let current_day = dayset[daycounter];
if current_day.is_none() {
continue;
}
filtered = is_filtered(ii, current_day.unwrap() as usize, options);
if filtered {
dayset[daycounter] = None;
}
}
filtered
}
pub fn build_timeset(options: &ParsedOptions) -> Vec<Time> {
let millisecond_mod = (options.dtstart.timestamp_millis() & 1000) as usize;
if !(options.freq == Frequenzy::DAILY
|| options.freq == Frequenzy::MONTHLY
|| options.freq == Frequenzy::WEEKLY
|| options.freq == Frequenzy::YEARLY)
{
return vec![];
}
let mut timeset = vec![];
for hour in &options.byhour {
for minute in &options.byminute {
for second in &options.bysecond {
timeset.push(Time::new(*hour, *minute, *second, millisecond_mod));
}
}
}
timeset
}
pub fn make_timeset(
ii: &IterInfo,
counter_date: &DateTime<Utc>,
options: &ParsedOptions,
) -> Vec<Time> {
if options.freq == Frequenzy::DAILY
|| options.freq == Frequenzy::MONTHLY
|| options.freq == Frequenzy::WEEKLY
|| options.freq == Frequenzy::YEARLY
{
return build_timeset(options);
}
if (options.freq >= Frequenzy::HOURLY
&& !options.byhour.is_empty()
&& !options
.byhour
.iter()
.any(|&h| h == counter_date.hour() as usize))
|| (options.freq >= Frequenzy::MINUTELY
&& !options.byminute.is_empty()
&& !options
.byminute
.iter()
.any(|&m| m == counter_date.minute() as usize))
|| (options.freq >= Frequenzy::SECONDLY
&& !options.bysecond.is_empty()
&& !options
.bysecond
.iter()
.any(|&s| s == counter_date.second() as usize))
{
return vec![];
}
ii.gettimeset(
&options.freq,
counter_date.hour() as usize,
counter_date.minute() as usize,
counter_date.second() as usize,
counter_date.timestamp_subsec_millis() as usize,
)
}
|