/src/chrono/src/offset/local/mod.rs
Line | Count | Source |
1 | | // This is a part of Chrono. |
2 | | // See README.md and LICENSE.txt for details. |
3 | | |
4 | | //! The local (system) time zone. |
5 | | |
6 | | #[cfg(windows)] |
7 | | use std::cmp::Ordering; |
8 | | |
9 | | #[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))] |
10 | | use rkyv::{Archive, Deserialize, Serialize}; |
11 | | |
12 | | use super::fixed::FixedOffset; |
13 | | use super::{MappedLocalTime, TimeZone}; |
14 | | #[allow(deprecated)] |
15 | | use crate::Date; |
16 | | use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; |
17 | | use crate::{DateTime, Utc}; |
18 | | |
19 | | #[cfg(unix)] |
20 | | #[path = "unix.rs"] |
21 | | mod inner; |
22 | | |
23 | | #[cfg(windows)] |
24 | | #[path = "windows.rs"] |
25 | | mod inner; |
26 | | |
27 | | #[cfg(all(windows, feature = "clock"))] |
28 | | #[allow(unreachable_pub)] |
29 | | mod win_bindings; |
30 | | |
31 | | #[cfg(all(any(target_os = "android", target_env = "ohos", test), feature = "clock"))] |
32 | | mod tz_data; |
33 | | |
34 | | #[cfg(all( |
35 | | not(unix), |
36 | | not(windows), |
37 | | not(all( |
38 | | target_arch = "wasm32", |
39 | | feature = "wasmbind", |
40 | | not(any(target_os = "emscripten", target_os = "wasi")) |
41 | | )) |
42 | | ))] |
43 | | mod inner { |
44 | | use crate::{FixedOffset, MappedLocalTime, NaiveDateTime}; |
45 | | |
46 | | pub(super) fn offset_from_utc_datetime( |
47 | | _utc_time: &NaiveDateTime, |
48 | | ) -> MappedLocalTime<FixedOffset> { |
49 | | MappedLocalTime::Single(FixedOffset::east_opt(0).unwrap()) |
50 | | } |
51 | | |
52 | | pub(super) fn offset_from_local_datetime( |
53 | | _local_time: &NaiveDateTime, |
54 | | ) -> MappedLocalTime<FixedOffset> { |
55 | | MappedLocalTime::Single(FixedOffset::east_opt(0).unwrap()) |
56 | | } |
57 | | } |
58 | | |
59 | | #[cfg(all( |
60 | | target_arch = "wasm32", |
61 | | feature = "wasmbind", |
62 | | not(any(target_os = "emscripten", target_os = "wasi", target_os = "linux")) |
63 | | ))] |
64 | | mod inner { |
65 | | use crate::{Datelike, FixedOffset, MappedLocalTime, NaiveDateTime, Timelike}; |
66 | | |
67 | | pub(super) fn offset_from_utc_datetime(utc: &NaiveDateTime) -> MappedLocalTime<FixedOffset> { |
68 | | let offset = js_sys::Date::from(utc.and_utc()).get_timezone_offset(); |
69 | | MappedLocalTime::Single(FixedOffset::west_opt((offset as i32) * 60).unwrap()) |
70 | | } |
71 | | |
72 | | pub(super) fn offset_from_local_datetime( |
73 | | local: &NaiveDateTime, |
74 | | ) -> MappedLocalTime<FixedOffset> { |
75 | | let mut year = local.year(); |
76 | | if year < 100 { |
77 | | // The API in `js_sys` does not let us create a `Date` with negative years. |
78 | | // And values for years from `0` to `99` map to the years `1900` to `1999`. |
79 | | // Shift the value by a multiple of 400 years until it is `>= 100`. |
80 | | let shift_cycles = (year - 100).div_euclid(400); |
81 | | year -= shift_cycles * 400; |
82 | | } |
83 | | let js_date = js_sys::Date::new_with_year_month_day_hr_min_sec( |
84 | | year as u32, |
85 | | local.month0() as i32, |
86 | | local.day() as i32, |
87 | | local.hour() as i32, |
88 | | local.minute() as i32, |
89 | | local.second() as i32, |
90 | | // ignore milliseconds, our representation of leap seconds may be problematic |
91 | | ); |
92 | | let offset = js_date.get_timezone_offset(); |
93 | | // We always get a result, even if this time does not exist or is ambiguous. |
94 | | MappedLocalTime::Single(FixedOffset::west_opt((offset as i32) * 60).unwrap()) |
95 | | } |
96 | | } |
97 | | |
98 | | #[cfg(unix)] |
99 | | mod tz_info; |
100 | | |
101 | | /// The local timescale. |
102 | | /// |
103 | | /// Using the [`TimeZone`](./trait.TimeZone.html) methods |
104 | | /// on the Local struct is the preferred way to construct `DateTime<Local>` |
105 | | /// instances. |
106 | | /// |
107 | | /// # Example |
108 | | /// |
109 | | /// ``` |
110 | | /// use chrono::{DateTime, Local, TimeZone}; |
111 | | /// |
112 | | /// let dt1: DateTime<Local> = Local::now(); |
113 | | /// let dt2: DateTime<Local> = Local.timestamp_opt(0, 0).unwrap(); |
114 | | /// assert!(dt1 >= dt2); |
115 | | /// ``` |
116 | | #[derive(Copy, Clone, Debug)] |
117 | | #[cfg_attr( |
118 | | any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"), |
119 | | derive(Archive, Deserialize, Serialize), |
120 | | archive(compare(PartialEq)), |
121 | | archive_attr(derive(Clone, Copy, Debug)) |
122 | | )] |
123 | | #[cfg_attr(feature = "rkyv-validation", archive(check_bytes))] |
124 | | #[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] |
125 | | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
126 | | pub struct Local; |
127 | | |
128 | | impl Local { |
129 | | /// Returns a `Date` which corresponds to the current date. |
130 | | #[deprecated(since = "0.4.23", note = "use `Local::now()` instead")] |
131 | | #[allow(deprecated)] |
132 | | #[must_use] |
133 | 0 | pub fn today() -> Date<Local> { |
134 | 0 | Local::now().date() |
135 | 0 | } |
136 | | |
137 | | /// Returns a `DateTime<Local>` which corresponds to the current date, time and offset from |
138 | | /// UTC. |
139 | | /// |
140 | | /// See also the similar [`Utc::now()`] which returns `DateTime<Utc>`, i.e. without the local |
141 | | /// offset. |
142 | | /// |
143 | | /// # Example |
144 | | /// |
145 | | /// ``` |
146 | | /// # #![allow(unused_variables)] |
147 | | /// # use chrono::{DateTime, FixedOffset, Local}; |
148 | | /// // Current local time |
149 | | /// let now = Local::now(); |
150 | | /// |
151 | | /// // Current local date |
152 | | /// let today = now.date_naive(); |
153 | | /// |
154 | | /// // Current local time, converted to `DateTime<FixedOffset>` |
155 | | /// let now_fixed_offset = Local::now().fixed_offset(); |
156 | | /// // or |
157 | | /// let now_fixed_offset: DateTime<FixedOffset> = Local::now().into(); |
158 | | /// |
159 | | /// // Current time in some timezone (let's use +05:00) |
160 | | /// // Note that it is usually more efficient to use `Utc::now` for this use case. |
161 | | /// let offset = FixedOffset::east_opt(5 * 60 * 60).unwrap(); |
162 | | /// let now_with_offset = Local::now().with_timezone(&offset); |
163 | | /// ``` |
164 | 0 | pub fn now() -> DateTime<Local> { |
165 | 0 | Utc::now().with_timezone(&Local) |
166 | 0 | } |
167 | | } |
168 | | |
169 | | impl TimeZone for Local { |
170 | | type Offset = FixedOffset; |
171 | | |
172 | 0 | fn from_offset(_offset: &FixedOffset) -> Local { |
173 | 0 | Local |
174 | 0 | } |
175 | | |
176 | | #[allow(deprecated)] |
177 | 0 | fn offset_from_local_date(&self, local: &NaiveDate) -> MappedLocalTime<FixedOffset> { |
178 | | // Get the offset at local midnight. |
179 | 0 | self.offset_from_local_datetime(&local.and_time(NaiveTime::MIN)) |
180 | 0 | } |
181 | | |
182 | 0 | fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> MappedLocalTime<FixedOffset> { |
183 | 0 | inner::offset_from_local_datetime(local) |
184 | 0 | } |
185 | | |
186 | | #[allow(deprecated)] |
187 | 0 | fn offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset { |
188 | | // Get the offset at midnight. |
189 | 0 | self.offset_from_utc_datetime(&utc.and_time(NaiveTime::MIN)) |
190 | 0 | } |
191 | | |
192 | 0 | fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> FixedOffset { |
193 | 0 | inner::offset_from_utc_datetime(utc).unwrap() |
194 | 0 | } |
195 | | } |
196 | | |
197 | | #[cfg(windows)] |
198 | | #[derive(Copy, Clone, Eq, PartialEq)] |
199 | | struct Transition { |
200 | | transition_utc: NaiveDateTime, |
201 | | offset_before: FixedOffset, |
202 | | offset_after: FixedOffset, |
203 | | } |
204 | | |
205 | | #[cfg(windows)] |
206 | | impl Transition { |
207 | | fn new( |
208 | | transition_local: NaiveDateTime, |
209 | | offset_before: FixedOffset, |
210 | | offset_after: FixedOffset, |
211 | | ) -> Transition { |
212 | | // It is no problem if the transition time in UTC falls a couple of hours inside the buffer |
213 | | // space around the `NaiveDateTime` range (although it is very theoretical to have a |
214 | | // transition at midnight around `NaiveDate::(MIN|MAX)`. |
215 | | let transition_utc = transition_local.overflowing_sub_offset(offset_before); |
216 | | Transition { transition_utc, offset_before, offset_after } |
217 | | } |
218 | | } |
219 | | |
220 | | #[cfg(windows)] |
221 | | impl PartialOrd for Transition { |
222 | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> { |
223 | | Some(self.cmp(other)) |
224 | | } |
225 | | } |
226 | | |
227 | | #[cfg(windows)] |
228 | | impl Ord for Transition { |
229 | | fn cmp(&self, other: &Self) -> Ordering { |
230 | | self.transition_utc.cmp(&other.transition_utc) |
231 | | } |
232 | | } |
233 | | |
234 | | // Calculate the time in UTC given a local time and transitions. |
235 | | // `transitions` must be sorted. |
236 | | #[cfg(windows)] |
237 | | fn lookup_with_dst_transitions( |
238 | | transitions: &[Transition], |
239 | | dt: NaiveDateTime, |
240 | | ) -> MappedLocalTime<FixedOffset> { |
241 | | for t in transitions.iter() { |
242 | | // A transition can result in the wall clock time going forward (creating a gap) or going |
243 | | // backward (creating a fold). We are interested in the earliest and latest wall time of the |
244 | | // transition, as this are the times between which `dt` does may not exist or is ambiguous. |
245 | | // |
246 | | // It is no problem if the transition times falls a couple of hours inside the buffer |
247 | | // space around the `NaiveDateTime` range (although it is very theoretical to have a |
248 | | // transition at midnight around `NaiveDate::(MIN|MAX)`. |
249 | | let (offset_min, offset_max) = |
250 | | match t.offset_after.local_minus_utc() > t.offset_before.local_minus_utc() { |
251 | | true => (t.offset_before, t.offset_after), |
252 | | false => (t.offset_after, t.offset_before), |
253 | | }; |
254 | | let wall_earliest = t.transition_utc.overflowing_add_offset(offset_min); |
255 | | let wall_latest = t.transition_utc.overflowing_add_offset(offset_max); |
256 | | |
257 | | if dt < wall_earliest { |
258 | | return MappedLocalTime::Single(t.offset_before); |
259 | | } else if dt <= wall_latest { |
260 | | return match t.offset_after.local_minus_utc().cmp(&t.offset_before.local_minus_utc()) { |
261 | | Ordering::Equal => MappedLocalTime::Single(t.offset_before), |
262 | | Ordering::Less => MappedLocalTime::Ambiguous(t.offset_before, t.offset_after), |
263 | | Ordering::Greater => { |
264 | | if dt == wall_earliest { |
265 | | MappedLocalTime::Single(t.offset_before) |
266 | | } else if dt == wall_latest { |
267 | | MappedLocalTime::Single(t.offset_after) |
268 | | } else { |
269 | | MappedLocalTime::None |
270 | | } |
271 | | } |
272 | | }; |
273 | | } |
274 | | } |
275 | | MappedLocalTime::Single(transitions.last().unwrap().offset_after) |
276 | | } |
277 | | |
278 | | #[cfg(test)] |
279 | | mod tests { |
280 | | use super::Local; |
281 | | use crate::offset::TimeZone; |
282 | | #[cfg(windows)] |
283 | | use crate::offset::local::{Transition, lookup_with_dst_transitions}; |
284 | | use crate::{Datelike, Days, Utc}; |
285 | | #[cfg(windows)] |
286 | | use crate::{FixedOffset, MappedLocalTime, NaiveDate, NaiveDateTime}; |
287 | | |
288 | | #[test] |
289 | | fn verify_correct_offsets() { |
290 | | let now = Local::now(); |
291 | | let from_local = Local.from_local_datetime(&now.naive_local()).unwrap(); |
292 | | let from_utc = Local.from_utc_datetime(&now.naive_utc()); |
293 | | |
294 | | assert_eq!(now.offset().local_minus_utc(), from_local.offset().local_minus_utc()); |
295 | | assert_eq!(now.offset().local_minus_utc(), from_utc.offset().local_minus_utc()); |
296 | | |
297 | | assert_eq!(now, from_local); |
298 | | assert_eq!(now, from_utc); |
299 | | } |
300 | | |
301 | | #[test] |
302 | | fn verify_correct_offsets_distant_past() { |
303 | | let distant_past = Local::now() - Days::new(365 * 500); |
304 | | let from_local = Local.from_local_datetime(&distant_past.naive_local()).unwrap(); |
305 | | let from_utc = Local.from_utc_datetime(&distant_past.naive_utc()); |
306 | | |
307 | | assert_eq!(distant_past.offset().local_minus_utc(), from_local.offset().local_minus_utc()); |
308 | | assert_eq!(distant_past.offset().local_minus_utc(), from_utc.offset().local_minus_utc()); |
309 | | |
310 | | assert_eq!(distant_past, from_local); |
311 | | assert_eq!(distant_past, from_utc); |
312 | | } |
313 | | |
314 | | #[test] |
315 | | fn verify_correct_offsets_distant_future() { |
316 | | let distant_future = Local::now() + Days::new(365 * 35000); |
317 | | let from_local = Local.from_local_datetime(&distant_future.naive_local()).unwrap(); |
318 | | let from_utc = Local.from_utc_datetime(&distant_future.naive_utc()); |
319 | | |
320 | | assert_eq!( |
321 | | distant_future.offset().local_minus_utc(), |
322 | | from_local.offset().local_minus_utc() |
323 | | ); |
324 | | assert_eq!(distant_future.offset().local_minus_utc(), from_utc.offset().local_minus_utc()); |
325 | | |
326 | | assert_eq!(distant_future, from_local); |
327 | | assert_eq!(distant_future, from_utc); |
328 | | } |
329 | | |
330 | | #[test] |
331 | | fn test_local_date_sanity_check() { |
332 | | // issue #27 |
333 | | assert_eq!(Local.with_ymd_and_hms(2999, 12, 28, 0, 0, 0).unwrap().day(), 28); |
334 | | } |
335 | | |
336 | | #[test] |
337 | | fn test_leap_second() { |
338 | | // issue #123 |
339 | | let today = Utc::now().date_naive(); |
340 | | |
341 | | if let Some(dt) = today.and_hms_milli_opt(15, 2, 59, 1000) { |
342 | | let timestr = dt.time().to_string(); |
343 | | // the OS API may or may not support the leap second, |
344 | | // but there are only two sensible options. |
345 | | assert!( |
346 | | timestr == "15:02:60" || timestr == "15:03:00", |
347 | | "unexpected timestr {timestr:?}" |
348 | | ); |
349 | | } |
350 | | |
351 | | if let Some(dt) = today.and_hms_milli_opt(15, 2, 3, 1234) { |
352 | | let timestr = dt.time().to_string(); |
353 | | assert!( |
354 | | timestr == "15:02:03.234" || timestr == "15:02:04.234", |
355 | | "unexpected timestr {timestr:?}" |
356 | | ); |
357 | | } |
358 | | } |
359 | | |
360 | | #[test] |
361 | | #[cfg(windows)] |
362 | | fn test_lookup_with_dst_transitions() { |
363 | | let ymdhms = |y, m, d, h, n, s| { |
364 | | NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap() |
365 | | }; |
366 | | |
367 | | #[track_caller] |
368 | | #[allow(clippy::too_many_arguments)] |
369 | | fn compare_lookup( |
370 | | transitions: &[Transition], |
371 | | y: i32, |
372 | | m: u32, |
373 | | d: u32, |
374 | | h: u32, |
375 | | n: u32, |
376 | | s: u32, |
377 | | result: MappedLocalTime<FixedOffset>, |
378 | | ) { |
379 | | let dt = NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap(); |
380 | | assert_eq!(lookup_with_dst_transitions(transitions, dt), result); |
381 | | } |
382 | | |
383 | | // dst transition before std transition |
384 | | // dst offset > std offset |
385 | | let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
386 | | let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap(); |
387 | | let transitions = [ |
388 | | Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, dst), |
389 | | Transition::new(ymdhms(2023, 10, 29, 3, 0, 0), dst, std), |
390 | | ]; |
391 | | compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, MappedLocalTime::Single(std)); |
392 | | compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, MappedLocalTime::Single(std)); |
393 | | compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, MappedLocalTime::None); |
394 | | compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, MappedLocalTime::Single(dst)); |
395 | | compare_lookup(&transitions, 2023, 3, 26, 4, 0, 0, MappedLocalTime::Single(dst)); |
396 | | |
397 | | compare_lookup(&transitions, 2023, 10, 29, 1, 0, 0, MappedLocalTime::Single(dst)); |
398 | | compare_lookup(&transitions, 2023, 10, 29, 2, 0, 0, MappedLocalTime::Ambiguous(dst, std)); |
399 | | compare_lookup(&transitions, 2023, 10, 29, 2, 30, 0, MappedLocalTime::Ambiguous(dst, std)); |
400 | | compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, MappedLocalTime::Ambiguous(dst, std)); |
401 | | compare_lookup(&transitions, 2023, 10, 29, 4, 0, 0, MappedLocalTime::Single(std)); |
402 | | |
403 | | // std transition before dst transition |
404 | | // dst offset > std offset |
405 | | let std = FixedOffset::east_opt(-5 * 60 * 60).unwrap(); |
406 | | let dst = FixedOffset::east_opt(-4 * 60 * 60).unwrap(); |
407 | | let transitions = [ |
408 | | Transition::new(ymdhms(2023, 3, 24, 3, 0, 0), dst, std), |
409 | | Transition::new(ymdhms(2023, 10, 27, 2, 0, 0), std, dst), |
410 | | ]; |
411 | | compare_lookup(&transitions, 2023, 3, 24, 1, 0, 0, MappedLocalTime::Single(dst)); |
412 | | compare_lookup(&transitions, 2023, 3, 24, 2, 0, 0, MappedLocalTime::Ambiguous(dst, std)); |
413 | | compare_lookup(&transitions, 2023, 3, 24, 2, 30, 0, MappedLocalTime::Ambiguous(dst, std)); |
414 | | compare_lookup(&transitions, 2023, 3, 24, 3, 0, 0, MappedLocalTime::Ambiguous(dst, std)); |
415 | | compare_lookup(&transitions, 2023, 3, 24, 4, 0, 0, MappedLocalTime::Single(std)); |
416 | | |
417 | | compare_lookup(&transitions, 2023, 10, 27, 1, 0, 0, MappedLocalTime::Single(std)); |
418 | | compare_lookup(&transitions, 2023, 10, 27, 2, 0, 0, MappedLocalTime::Single(std)); |
419 | | compare_lookup(&transitions, 2023, 10, 27, 2, 30, 0, MappedLocalTime::None); |
420 | | compare_lookup(&transitions, 2023, 10, 27, 3, 0, 0, MappedLocalTime::Single(dst)); |
421 | | compare_lookup(&transitions, 2023, 10, 27, 4, 0, 0, MappedLocalTime::Single(dst)); |
422 | | |
423 | | // dst transition before std transition |
424 | | // dst offset < std offset |
425 | | let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
426 | | let dst = FixedOffset::east_opt((2 * 60 + 30) * 60).unwrap(); |
427 | | let transitions = [ |
428 | | Transition::new(ymdhms(2023, 3, 26, 2, 30, 0), std, dst), |
429 | | Transition::new(ymdhms(2023, 10, 29, 2, 0, 0), dst, std), |
430 | | ]; |
431 | | compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, MappedLocalTime::Single(std)); |
432 | | compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, MappedLocalTime::Ambiguous(std, dst)); |
433 | | compare_lookup(&transitions, 2023, 3, 26, 2, 15, 0, MappedLocalTime::Ambiguous(std, dst)); |
434 | | compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, MappedLocalTime::Ambiguous(std, dst)); |
435 | | compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, MappedLocalTime::Single(dst)); |
436 | | |
437 | | compare_lookup(&transitions, 2023, 10, 29, 1, 0, 0, MappedLocalTime::Single(dst)); |
438 | | compare_lookup(&transitions, 2023, 10, 29, 2, 0, 0, MappedLocalTime::Single(dst)); |
439 | | compare_lookup(&transitions, 2023, 10, 29, 2, 15, 0, MappedLocalTime::None); |
440 | | compare_lookup(&transitions, 2023, 10, 29, 2, 30, 0, MappedLocalTime::Single(std)); |
441 | | compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, MappedLocalTime::Single(std)); |
442 | | |
443 | | // std transition before dst transition |
444 | | // dst offset < std offset |
445 | | let std = FixedOffset::east_opt(-(4 * 60 + 30) * 60).unwrap(); |
446 | | let dst = FixedOffset::east_opt(-5 * 60 * 60).unwrap(); |
447 | | let transitions = [ |
448 | | Transition::new(ymdhms(2023, 3, 24, 2, 0, 0), dst, std), |
449 | | Transition::new(ymdhms(2023, 10, 27, 2, 30, 0), std, dst), |
450 | | ]; |
451 | | compare_lookup(&transitions, 2023, 3, 24, 1, 0, 0, MappedLocalTime::Single(dst)); |
452 | | compare_lookup(&transitions, 2023, 3, 24, 2, 0, 0, MappedLocalTime::Single(dst)); |
453 | | compare_lookup(&transitions, 2023, 3, 24, 2, 15, 0, MappedLocalTime::None); |
454 | | compare_lookup(&transitions, 2023, 3, 24, 2, 30, 0, MappedLocalTime::Single(std)); |
455 | | compare_lookup(&transitions, 2023, 3, 24, 3, 0, 0, MappedLocalTime::Single(std)); |
456 | | |
457 | | compare_lookup(&transitions, 2023, 10, 27, 1, 0, 0, MappedLocalTime::Single(std)); |
458 | | compare_lookup(&transitions, 2023, 10, 27, 2, 0, 0, MappedLocalTime::Ambiguous(std, dst)); |
459 | | compare_lookup(&transitions, 2023, 10, 27, 2, 15, 0, MappedLocalTime::Ambiguous(std, dst)); |
460 | | compare_lookup(&transitions, 2023, 10, 27, 2, 30, 0, MappedLocalTime::Ambiguous(std, dst)); |
461 | | compare_lookup(&transitions, 2023, 10, 27, 3, 0, 0, MappedLocalTime::Single(dst)); |
462 | | |
463 | | // offset stays the same |
464 | | let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
465 | | let transitions = [ |
466 | | Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, std), |
467 | | Transition::new(ymdhms(2023, 10, 29, 3, 0, 0), std, std), |
468 | | ]; |
469 | | compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, MappedLocalTime::Single(std)); |
470 | | compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, MappedLocalTime::Single(std)); |
471 | | |
472 | | // single transition |
473 | | let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
474 | | let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap(); |
475 | | let transitions = [Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, dst)]; |
476 | | compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, MappedLocalTime::Single(std)); |
477 | | compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, MappedLocalTime::Single(std)); |
478 | | compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, MappedLocalTime::None); |
479 | | compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, MappedLocalTime::Single(dst)); |
480 | | compare_lookup(&transitions, 2023, 3, 26, 4, 0, 0, MappedLocalTime::Single(dst)); |
481 | | } |
482 | | |
483 | | #[test] |
484 | | #[cfg(windows)] |
485 | | fn test_lookup_with_dst_transitions_limits() { |
486 | | // Transition beyond UTC year end doesn't panic in year of `NaiveDate::MAX` |
487 | | let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
488 | | let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap(); |
489 | | let transitions = [ |
490 | | Transition::new(NaiveDateTime::MAX.with_month(7).unwrap(), std, dst), |
491 | | Transition::new(NaiveDateTime::MAX, dst, std), |
492 | | ]; |
493 | | assert_eq!( |
494 | | lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX.with_month(3).unwrap()), |
495 | | MappedLocalTime::Single(std) |
496 | | ); |
497 | | assert_eq!( |
498 | | lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX.with_month(8).unwrap()), |
499 | | MappedLocalTime::Single(dst) |
500 | | ); |
501 | | // Doesn't panic with `NaiveDateTime::MAX` as argument (which would be out of range when |
502 | | // converted to UTC). |
503 | | assert_eq!( |
504 | | lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX), |
505 | | MappedLocalTime::Ambiguous(dst, std) |
506 | | ); |
507 | | |
508 | | // Transition before UTC year end doesn't panic in year of `NaiveDate::MIN` |
509 | | let std = FixedOffset::west_opt(3 * 60 * 60).unwrap(); |
510 | | let dst = FixedOffset::west_opt(4 * 60 * 60).unwrap(); |
511 | | let transitions = [ |
512 | | Transition::new(NaiveDateTime::MIN, std, dst), |
513 | | Transition::new(NaiveDateTime::MIN.with_month(6).unwrap(), dst, std), |
514 | | ]; |
515 | | assert_eq!( |
516 | | lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN.with_month(3).unwrap()), |
517 | | MappedLocalTime::Single(dst) |
518 | | ); |
519 | | assert_eq!( |
520 | | lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN.with_month(8).unwrap()), |
521 | | MappedLocalTime::Single(std) |
522 | | ); |
523 | | // Doesn't panic with `NaiveDateTime::MIN` as argument (which would be out of range when |
524 | | // converted to UTC). |
525 | | assert_eq!( |
526 | | lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN), |
527 | | MappedLocalTime::Ambiguous(std, dst) |
528 | | ); |
529 | | } |
530 | | |
531 | | #[test] |
532 | | #[cfg(feature = "rkyv-validation")] |
533 | | fn test_rkyv_validation() { |
534 | | let local = Local; |
535 | | // Local is a ZST and serializes to 0 bytes |
536 | | let bytes = rkyv::to_bytes::<_, 0>(&local).unwrap(); |
537 | | assert_eq!(bytes.len(), 0); |
538 | | |
539 | | // but is deserialized to an archived variant without a |
540 | | // wrapping object |
541 | | assert_eq!(rkyv::from_bytes::<Local>(&bytes).unwrap(), super::ArchivedLocal); |
542 | | } |
543 | | } |