summaryrefslogtreecommitdiff
path: root/src/iterinfo.rs
blob: 98edd4558960d5dcf6cfde38298c919f98a1e1a1 (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
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
use crate::monthinfo::*;
use crate::yearinfo::*;
use chrono::prelude::*;
use chrono::*;

struct IterInfo<'a> {
    pub yearinfo: Option<YearInfo>,
    pub monthinfo: Option<MonthInfo>,
    options: &'a ParsedOptions,
}

impl<'a> IterInfo<'a> {
    pub fn new(options: &'a ParsedOptions) -> Self {
        Self {
            options,
            yearinfo: None,
            monthinfo: None,
        }
    }

    pub fn rebuild(&mut self, year: isize, month: usize) {
        if self.monthinfo.is_none() || year != self.monthinfo.as_ref().unwrap().lastyear {
            self.yearinfo = Some(rebuild_year(year as i32, self.options));
        }

        if !self.options.bynweekday.is_empty()
            && ((self.monthinfo.is_none() || month != self.monthinfo.as_ref().unwrap().lastmonth)
                || (self.monthinfo.is_none() || year != self.monthinfo.as_ref().unwrap().lastyear))
        {
            if let Some(yearinfo) = &self.yearinfo {
                self.monthinfo = Some(rebuild_month(
                    year,
                    month,
                    yearinfo.yearlen,
                    &yearinfo.mrange,
                    &yearinfo.wdaymask,
                    self.options,
                ));
            }
        }
    }

    pub fn lastyear(&self) -> Option<isize> {
        match &self.monthinfo {
            Some(info) => Some(info.lastyear),
            None => None,
        }
    }
    pub fn lastmonth(&self) -> Option<usize> {
        match &self.monthinfo {
            Some(info) => Some(info.lastmonth),
            None => None,
        }
    }
    pub fn yearlen(&self) -> Option<usize> {
        match &self.yearinfo {
            Some(info) => Some(info.yearlen),
            None => None,
        }
    }
    pub fn yearordinal(&self) -> Option<isize> {
        match &self.yearinfo {
            Some(info) => Some(info.yearordinal),
            None => None,
        }
    }
    pub fn mrange(&self) -> Option<&Vec<usize>> {
        match &self.yearinfo {
            Some(info) => Some(&info.mrange),
            None => None,
        }
    }
    pub fn wdaymask(&self) -> Option<&Vec<usize>> {
        match &self.yearinfo {
            Some(info) => Some(&info.wdaymask),
            None => None,
        }
    }

    pub fn mmask(&self) -> Option<&Vec<usize>> {
        match &self.yearinfo {
            Some(info) => Some(&info.mmask),
            None => None,
        }
    }

    pub fn wnomask(&self) -> Option<&Vec<usize>> {
        match &self.yearinfo {
            Some(info) => match &info.wnomask {
                Some(mask) => Some(mask),
                None => None,
            },
            None => None,
        }
    }
    pub fn nwdaymask(&self) -> Option<&Vec<isize>> {
        match &self.monthinfo {
            Some(info) => Some(&info.nwdaymask),
            None => None,
        }
    }
    pub fn nextyearlen(&self) -> Option<usize> {
        match &self.yearinfo {
            Some(info) => Some(info.nextyearlen),
            None => None,
        }
    }
    pub fn mdaymask(&self) -> Option<&Vec<usize>> {
        match &self.yearinfo {
            Some(info) => Some(&info.mdaymask),
            None => None,
        }
    }
    pub fn nmdaymask(&self) -> Option<&Vec<isize>> {
        match &self.yearinfo {
            Some(info) => Some(&info.nmdaymask),
            None => None,
        }
    }

    pub fn ydayset(&self) -> (Vec<usize>, usize, usize) {
        let yearlen = self.yearlen().unwrap();
        let mut v = Vec::with_capacity(yearlen);
        for i in 0..yearlen {
            v.push(i);
        }
        (v, 0, yearlen)
    }

    pub fn mdayset(&self, month: usize) -> (Vec<usize>, usize, usize) {
        let mrange = self.mrange().unwrap();
        let start = mrange[month - 1];
        let end = mrange[month];
        let mut set = vec![0; self.yearlen().unwrap()];
        for i in start..end {
            set[i] = 1;
        }
        (set, start, end)
    }

    pub fn wdayset(&self, year: isize, month: usize, day: usize) -> (Vec<usize>, usize, usize) {
        let mut set = vec![0; self.yearlen().unwrap()];

        // should it be month - 1 ??????
        let mut i = (to_ordinal(
            &Utc.ymd(year as i32, month as u32 - 1, day as u32)
                .and_hms(0, 0, 0),
        ) - self.yearordinal().unwrap()) as usize;

        let start = i;
        for _ in 0..7 {
            set[i] = i;
            i += 1;
            if self.wdaymask().unwrap()[i] == self.options.wkst {
                break;
            }
        }
        (set, start, i)
    }

    pub fn ddayset(&self, year: isize, month: usize, day: usize) -> (Vec<usize>, usize, usize) {
        let mut set = vec![0; self.yearlen().unwrap()];

        // should it be month - 1 ??????
        let mut i = (to_ordinal(
            &Utc.ymd(year as i32, month as u32 - 1, day as u32)
                .and_hms(0, 0, 0),
        ) - self.yearordinal().unwrap()) as usize;

        (set, i, i + 1)
    }

    pub fn getdayset(
        &self,
        freq: Frequenzy,
        year: isize,
        month: usize,
        day: usize,
    ) -> (Vec<usize>, usize, usize) {
        match freq {
            Frequenzy::YEARLY => self.ydayset(),
            Frequenzy::MONTHLY => self.mdayset(month),
            Frequenzy::WEEKLY => self.wdayset(year, month, day),
            Frequenzy::DAILY => self.ddayset(year, month, day),
            _ => self.ydayset(),
        }
    }
}