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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use chrono::prelude::*;
#[cfg(feature = "json")]
use serde::Serialize;
use libtzfile::TzError;
#[cfg(feature = "json")]
mod offset_serializer {
use serde::Serialize;
fn offset_to_json(t: chrono::FixedOffset) -> String {
format!("{:?}", t)
}
pub fn serialize<S: serde::Serializer>(time: &chrono::FixedOffset, serializer: S) -> Result<S::Ok, S::Error> {
offset_to_json(time.clone()).serialize(serializer)
}
}
#[cfg(feature = "json")]
#[derive(Debug, Serialize)]
pub struct Tzinfo {
pub timezone: String,
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,
#[serde(with = "offset_serializer")]
pub utc_offset: FixedOffset,
pub abbreviation: String,
pub week_number: i32,
}
#[cfg(not(feature = "json"))]
#[derive(Debug)]
pub struct Tzinfo {
pub timezone: String,
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,
pub week_number: i32,
}
#[derive(Debug, PartialEq)]
pub struct Timechange {
pub time: DateTime<Utc>,
pub gmtoff: isize,
pub isdst: bool,
pub abbreviation: String,
}
#[cfg(feature = "json")]
impl Tzinfo {
pub fn to_json(&self) -> Result<String, serde_json::error::Error> {
serde_json::to_string(&self)
}
}
pub fn get_timechanges(requested_timezone: &str, y: Option<i32>) -> Result<Vec<Timechange>, TzError> {
let timezone = libtzfile::parse(requested_timezone)?;
let mut timechanges = Vec::new();
let mut nearest_timechange: usize = 0;
let mut parsedtimechanges = Vec::new();
if y.is_some() {
let d = Utc::now();
let y = y.unwrap();
let y = if y == 0 { d.format("%Y").to_string().parse()? } else { y };
let yearbeg = Utc.ymd(y, 1, 1).and_hms(0, 0, 0);
let yearend = Utc.ymd(y, 12, 31).and_hms(0, 0, 0);
for t in 0..timezone.tzh_timecnt_data.len() {
if timezone.tzh_timecnt_data[t] > yearbeg && timezone.tzh_timecnt_data[t] < yearend {
timechanges.push(t);
}
if timezone.tzh_timecnt_data[t] < yearbeg {
nearest_timechange = t;
};
}
} else {
for t in 0..timezone.tzh_timecnt_data.len() {
timechanges.push(t);
}
}
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);
}
Ok(parsedtimechanges)
}
pub fn get_zoneinfo(requested_timezone: &str) -> Result<Tzinfo, TzError> {
let parsedtimechanges = get_timechanges(requested_timezone, Some(0))?;
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)
};
Ok(Tzinfo {
timezone: requested_timezone.to_string(),
week_number: d.with_timezone(&utc_offset).format("%V").to_string().parse()?,
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);
Ok(Tzinfo {
timezone: requested_timezone.to_string(),
week_number: d.with_timezone(&utc_offset).format("%V").to_string().parse()?,
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 {
Err(TzError::NoData)
}
}
#[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);
}
}