summaryrefslogtreecommitdiff
path: root/src/iter/poslist.rs
blob: 421d3526dfb1f8fb8311f58a54c8f6106b08dfae (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
use crate::datetime::*;
use crate::iter::iterinfo::IterInfo;
use crate::iter::utils::pymod;
use crate::datetime::from_ordinal;
use chrono::prelude::*;
use chrono_tz::Tz;

pub fn build_poslist(
    bysetpost: &Vec<isize>,
    timeset: &Vec<Time>,
    start: usize,
    end: usize,
    ii: &IterInfo,
    dayset: &Vec<Option<isize>>,
    tz: &Tz
) -> Vec<DateTime<Tz>> {
    let mut poslist = vec![];

    for j in 0..bysetpost.len() {
        let daypos;
        let timepos;
        let pos = bysetpost[j];
        if pos < 0 {
            daypos = (pos as f32 / timeset.len() as f32).floor() as isize;
            timepos = pymod(pos as isize, timeset.len() as isize);
        } else {
            daypos = ((pos - 1) as f32 / timeset.len() as f32) as isize;
            timepos = pymod(pos as isize - 1, timeset.len() as isize);
        }

        let mut tmp = vec![];
        for k in start..end {
            let val = dayset[k];
            if val.is_some() {
                tmp.push(val.unwrap());
            }
        }

        let i;
        if daypos < 0 {
            let index = tmp.len() as isize + daypos;
            i = &tmp[index as usize];
        } else {
            i = &tmp[daypos as usize];
        }

        let date = from_ordinal(ii.yearordinal().unwrap() + i);
        let res = tz.ymd(date.year(), date.month(), date.day()).and_hms(
            timeset[timepos as usize].hour as u32,
            timeset[timepos as usize].minute as u32,
            timeset[timepos as usize].second as u32,
        );
        // XXX: can this ever be in the array?
        // - compare the actual date instead?
        if !poslist.iter().any(|&p| p == res) {
            poslist.push(res);
        }
    }

    poslist.sort_by_key(|a| a.timestamp());

    poslist
}