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
extern crate libtzfile;
use chrono::prelude::*;
use std::convert::TryInto;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Tzinfo {
pub utc_datetime: DateTime<Utc>,
pub datetime: DateTime<FixedOffset>,
pub dst_from: Option<DateTime<Utc>>,
pub dst_until: Option<DateTime<Utc>>,
pub dst_period: bool,
pub raw_offset: isize,
pub dst_offset: isize,
pub utc_offset: FixedOffset,
pub abbreviation: String,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Timechange {
pub time: DateTime<Utc>,
pub gmtoff: isize,
pub isdst: bool,
pub abbreviation: String
}
pub fn get_timechanges(requested_timezone: &str, y: Option<i32>) -> Option<Vec<Timechange>> {
let timezone = match libtzfile::parse(requested_timezone) {
Ok(tz) => tz,
Err(_) => return None
};
let mut timechanges = Vec::new();
let mut nearest_timechange: usize = 0;
let mut parsedtimechanges = Vec::new();
let year: i32 = match y {
Some(y) => y,
None => {
let d = Utc::now();
d.format("%Y").to_string().parse().unwrap()
}
};
let currentyearbeg = Utc.ymd(year, 1, 1).and_hms(0, 0, 0);
let currentyearend = Utc.ymd(year, 12, 31).and_hms(0, 0, 0);
for t in 0..timezone.tzh_timecnt_data.len() {
if timezone.tzh_timecnt_data[t] > currentyearbeg
&& timezone.tzh_timecnt_data[t] < currentyearend
{
timechanges.push(t);
}
if timezone.tzh_timecnt_data[t] < currentyearbeg {
nearest_timechange = t.try_into().unwrap();
};
}
if timechanges.len() != 0 {
for t in 0..timechanges.len() {
let tc = Timechange {
time: timezone.tzh_timecnt_data[timechanges[t]],
gmtoff: timezone.tzh_typecnt[timezone.tzh_timecnt_indices[timechanges[t]] as usize].tt_gmtoff,
isdst: timezone.tzh_typecnt[timezone.tzh_timecnt_indices[timechanges[t]] as usize].tt_isdst == 1,
abbreviation: timezone.tz_abbr[timezone.tzh_typecnt[timezone.tzh_timecnt_indices[timechanges[t]] as usize].tt_abbrind as usize].to_string(),
};
parsedtimechanges.push(tc);
}
} else {
let tc = Timechange {
time: timezone.tzh_timecnt_data[nearest_timechange],
gmtoff: timezone.tzh_typecnt[timezone.tzh_timecnt_indices[nearest_timechange] as usize].tt_gmtoff,
isdst: timezone.tzh_typecnt[timezone.tzh_timecnt_indices[nearest_timechange] as usize].tt_isdst == 1,
abbreviation: timezone.tz_abbr[timezone.tzh_typecnt[timezone.tzh_timecnt_indices[nearest_timechange] as usize].tt_abbrind as usize].to_string(),
};
parsedtimechanges.push(tc);
}
Some(parsedtimechanges)
}
pub fn get_zoneinfo(parsedtimechanges: &Vec<Timechange>) -> Option<Tzinfo> {
let d = Utc::now();
if parsedtimechanges.len() == 2 {
let dst = d > parsedtimechanges[0].time
&& d < parsedtimechanges[1].time;
let utc_offset = if dst == true { FixedOffset::east(parsedtimechanges[0].gmtoff as i32) } else { FixedOffset::east(parsedtimechanges[1].gmtoff as i32) };
Some(Tzinfo{
utc_datetime: d,
datetime: d.with_timezone(&utc_offset),
dst_from: Some(parsedtimechanges[0].time),
dst_until: Some(parsedtimechanges[1].time),
dst_period: dst,
raw_offset: parsedtimechanges[1].gmtoff,
dst_offset: parsedtimechanges[0].gmtoff,
utc_offset: utc_offset,
abbreviation: if dst == true { parsedtimechanges[0].abbreviation.clone() } else { parsedtimechanges[1].abbreviation.clone() },
})
} else if parsedtimechanges.len()==1 {
let utc_offset = FixedOffset::east(parsedtimechanges[0].gmtoff as i32);
Some(Tzinfo {
utc_datetime: d,
datetime: d.with_timezone(&utc_offset),
dst_from: None,
dst_until: None,
dst_period: false,
raw_offset: parsedtimechanges[0].gmtoff,
dst_offset: 0,
utc_offset: utc_offset,
abbreviation: parsedtimechanges[0].abbreviation.clone(),
})
} else { None }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zoneinfo() {
let tz = vec!(Timechange{time: Utc.ymd(2019, 3, 31).and_hms(1, 0, 0), gmtoff: 7200, isdst: true, abbreviation: "CEST".to_string()},
Timechange{time: Utc.ymd(2019, 10, 27).and_hms(1, 0, 0), gmtoff: 3600, isdst: false, abbreviation: "CET".to_string()});
assert_eq!(get_timechanges("Europe/Paris", Some(2019)).unwrap(), tz);
}
}