summaryrefslogtreecommitdiff
path: root/src/iter_set.rs
blob: 9f251d9909a6abb7526efde980a6f66011686bf6 (plain)
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
use crate::iter::*;
use crate::iterinfo::*;
use crate::options::*;
use crate::poslist::*;
use crate::rrule::*;
use chrono::offset::TimeZone;
use chrono::prelude::*;
use chrono::{DateTime, Duration, Utc};
use chrono_tz::*;
use std::collections::HashMap;



pub trait TIterResult {
    fn accept(&mut self, date: DateTime<Tz>) -> bool;
    fn get_value(&self) -> Vec<DateTime<Tz>>;
}



pub fn iter_v2<T: TIterResult>(
    iter_result: &mut T,
    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, &timeset, 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 = UTC.timestamp(res.timestamp(), 0);

                    if !iter_result.accept(rezoned_date) {
                        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 = UTC.timestamp(res.timestamp(), 0);

                        if !iter_result.accept(rezoned_date) {
                            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);
    }
}