summaryrefslogtreecommitdiff
path: root/src/iter/poslist.rs
blob: ce627a4f411df5a29d026af2d05f474565e54cd1 (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::from_ordinal;
use crate::datetime::{DTime, Time};
use crate::iter::iterinfo::IterInfo;
use crate::utils::pymod;
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<DTime> {
    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];
            match val {
                Some(v) => tmp.push(v),
                None => (),
            }
        }

        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, tz);
        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,
        );

        if !poslist.iter().any(|&p| p == res) {
            poslist.push(res);
        }
    }

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

    poslist
}