-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslator.rs
207 lines (179 loc) · 8.06 KB
/
translator.rs
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
pub(super) mod translation_error;
use crate::command::validated_options::ambiguous_time_strategy::AmbiguousTimeStrategy;
use chrono::{DateTime, LocalResult, MappedLocalTime, NaiveDateTime, TimeZone};
use chrono_tz::Tz;
use translation_error::TranslationError;
pub(crate) struct TimezoneTranslator {
time: NaiveDateTime,
from_tz: Tz,
to_tz: Tz,
ambiguous_time_strategy: AmbiguousTimeStrategy,
}
impl TimezoneTranslator {
pub(crate) fn new(
time: NaiveDateTime,
from_tz: Tz,
to_tz: Tz,
ambiguous_time_strategy: AmbiguousTimeStrategy,
) -> Self {
Self {
time,
from_tz,
to_tz,
ambiguous_time_strategy,
}
}
pub(crate) fn convert(&self) -> Result<DateTime<Tz>, TranslationError> {
// Extract the time from the `time` field with `from_tz` field
let mapped: MappedLocalTime<DateTime<Tz>> = self.from_tz.from_local_datetime(&self.time);
match mapped {
LocalResult::Single(time) => Ok(time.with_timezone(&self.to_tz)),
LocalResult::Ambiguous(time_earliest, time_latest) => {
Ok(select_time_with_ambiguous_time_strategy(
self.ambiguous_time_strategy,
self.to_tz,
time_earliest,
time_latest,
))
}
LocalResult::None => {
let error = TranslationError::TranslationError {
time: self.time,
from_tz: self.from_tz,
to_tz: self.to_tz,
};
Err(error)
}
}
}
}
fn select_time_with_ambiguous_time_strategy(
strategy: AmbiguousTimeStrategy,
timezone: Tz,
time_earliest: DateTime<Tz>,
time_latest: DateTime<Tz>,
) -> DateTime<Tz> {
match strategy {
AmbiguousTimeStrategy::Earliest => time_earliest.with_timezone(&timezone),
AmbiguousTimeStrategy::Latest => time_latest.with_timezone(&timezone),
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::{NaiveDate, Utc};
use chrono_tz::Tz;
/// This test checks if the `TimezoneTranslator` struct is created correctly.
/// It checks if the `time`, `from_tz`, and `to_tz` fields are set correctly.
/// expected: `TimezoneTranslator`
#[test]
fn test_new() {
let date: NaiveDate = NaiveDate::from_ymd_opt(2024, 6, 27).unwrap();
let time: NaiveDateTime = date.and_hms_opt(12, 0, 0).unwrap();
let from_tz: Tz = "America/New_York".parse().unwrap();
let to_tz: Tz = "Europe/London".parse().unwrap();
let ambiguous_time_strategy: AmbiguousTimeStrategy = AmbiguousTimeStrategy::Earliest;
let translator: TimezoneTranslator =
TimezoneTranslator::new(time, from_tz, to_tz, ambiguous_time_strategy);
assert_eq!(translator.time, time);
assert_eq!(translator.from_tz, from_tz);
assert_eq!(translator.to_tz, to_tz);
assert_eq!(translator.ambiguous_time_strategy, ambiguous_time_strategy);
}
/// This test checks if the `convert` method works correctly.
/// It checks if the method returns a `DateTime<Tz>` object.
/// expected: `DateTime<Tz>`
#[test]
fn test_convert() {
// input for the test
let date: NaiveDate = NaiveDate::from_ymd_opt(2024, 6, 27).unwrap();
let time: NaiveDateTime = date.and_hms_opt(12, 0, 0).unwrap();
let from_tz: Tz = "America/New_York".parse().unwrap();
let to_tz: Tz = "UTC".parse().unwrap();
let ambiguous_time_strategy: AmbiguousTimeStrategy = AmbiguousTimeStrategy::Earliest;
// expected result
// +4 hours from America/New_York to UTC
let expected_time = Utc
.with_ymd_and_hms(2024, 6, 27, 16, 0, 0)
.unwrap()
.with_timezone(&to_tz);
// calculate the actual result
let translator: TimezoneTranslator =
TimezoneTranslator::new(time, from_tz, to_tz, ambiguous_time_strategy);
let actual_converted_time: Result<DateTime<Tz>, TranslationError> = translator.convert();
assert!(actual_converted_time.is_ok());
// confirm if the actual result is the same as the expected result
assert_eq!(actual_converted_time.unwrap(), expected_time);
}
/// This test check option `AmbiguousTimeStrategy::Earliest` works correctly.
/// American/New_York is UTC-4 but `2024-11-03 01:30:00` is ambiguous. (after DST ends)
/// `2024-11-03 05:30:00` is the earliest time.
/// expected: `DateTime<Tz>`
#[test]
fn test_earliest_ambiguous_time_strategy() {
// input for the test
let date: NaiveDate = NaiveDate::from_ymd_opt(2024, 11, 03).unwrap();
let time: NaiveDateTime = date.and_hms_opt(01, 30, 0).unwrap();
let from_tz: Tz = "America/New_York".parse().unwrap();
let to_tz: Tz = "UTC".parse().unwrap();
let ambiguous_time_strategy: AmbiguousTimeStrategy = AmbiguousTimeStrategy::Earliest;
// expected result
// +4, +5 hours from America/New_York to UTC (DST ends)
// in this case, the earliest time is 5:30
let expected_time = Utc
.with_ymd_and_hms(2024, 11, 03, 5, 30, 0)
.unwrap()
.with_timezone(&to_tz);
// calculate the actual result
let translator: TimezoneTranslator =
TimezoneTranslator::new(time, from_tz, to_tz, ambiguous_time_strategy);
let actual_converted_time: Result<DateTime<Tz>, TranslationError> = translator.convert();
assert!(actual_converted_time.is_ok());
// confirm if the actual result is the same as the expected result
assert_eq!(actual_converted_time.unwrap(), expected_time);
}
/// This test check option `AmbiguousTimeStrategy::Latest` works correctly.
/// American/New_York is UTC-4 but `2024-11-03 01:30:00` is ambiguous. (after DST ends)
/// `2024-11-03 06:30:00` is the latest time.
/// expected: `DateTime<Tz>`
#[test]
fn test_latest_ambiguous_time_strategy() {
// input for the test
let date: NaiveDate = NaiveDate::from_ymd_opt(2024, 11, 03).unwrap();
let time: NaiveDateTime = date.and_hms_opt(01, 30, 0).unwrap();
let from_tz: Tz = "America/New_York".parse().unwrap();
let to_tz: Tz = "UTC".parse().unwrap();
let ambiguous_time_strategy: AmbiguousTimeStrategy = AmbiguousTimeStrategy::Latest;
// expected result
// +4, +5 hours from America/New_York to UTC (DST ends)
// in this case, the latest time is 6:30
let expected_time = Utc
.with_ymd_and_hms(2024, 11, 03, 6, 30, 0)
.unwrap()
.with_timezone(&to_tz);
// calculate the actual result
let translator: TimezoneTranslator =
TimezoneTranslator::new(time, from_tz, to_tz, ambiguous_time_strategy);
let actual_converted_time: Result<DateTime<Tz>, TranslationError> = translator.convert();
assert!(actual_converted_time.is_ok());
// confirm if the actual result is the same as the expected result
assert_eq!(actual_converted_time.unwrap(), expected_time);
}
/// This test checks if the `convert` method returns an error when the output time does not exist.
/// expected: `TranslationError`
#[test]
fn test_output_timestamp_does_not_exist() {
// input for the test
let date: NaiveDate = NaiveDate::from_ymd_opt(2024, 03, 10).unwrap();
let time: NaiveDateTime = date.and_hms_opt(02, 30, 0).unwrap();
let from_tz: Tz = "America/New_York".parse().unwrap();
let to_tz: Tz = "America/Los_Angeles".parse().unwrap();
let ambiguous_time_strategy: AmbiguousTimeStrategy = AmbiguousTimeStrategy::Latest;
// calculate the actual result
let translator: TimezoneTranslator =
TimezoneTranslator::new(time, from_tz, to_tz, ambiguous_time_strategy);
let actual_converted_time: Result<DateTime<Tz>, TranslationError> = translator.convert();
// check result is error
assert!(actual_converted_time.is_err());
}
}