summaryrefslogtreecommitdiff
path: root/src/web_frontend/util.rs
blob: 208b5b919567299ad56366123920228651f3182d (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
use chrono::{NaiveDate, Utc};
use chrono::Datelike;

#[derive(Debug)]
pub struct DateRange {
	start_year : i32,
	start_month : u32,
	end_year : i32,
	end_month : u32,
	last : bool,
}

impl DateRange {
	pub fn new(start : chrono::NaiveDate, end : chrono::NaiveDate) -> DateRange {
		DateRange {
			start_year : start.year(),
			start_month : start.month(),
			end_year : end.year(),
			end_month : end.month(),
			last : false,
		}
	}
}

impl Iterator for DateRange {
	type Item = chrono::NaiveDate;

	fn next(&mut self) -> Option<Self::Item> {
		if self.last {
			return None
		}
		if self.start_year == self.end_year && self.start_month == self.end_month {
			self.last = true;	
		}
		let mut tmp = self.start_year.to_string();
		if self.start_month < 10 {
			tmp.push_str("-0");
		} else {
			tmp.push_str("-");
		}
		tmp.push_str(&self.start_month.to_string());
		tmp.push_str("-01");
		if self.start_month < 12 {
			self.start_month = self.start_month + 1;
		} else {
			self.start_month = 1;
			self.start_year = self.start_year + 1;
		}
		return Some(chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap())
	}
}