1#[cfg(windows)]
7use std::cmp::Ordering;
8
9#[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
10use rkyv::{Archive, Deserialize, Serialize};
11
12use super::fixed::FixedOffset;
13use super::{MappedLocalTime, TimeZone};
14#[allow(deprecated)]
15use crate::Date;
16use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime};
17use crate::{DateTime, Utc};
18
19#[cfg(unix)]
20#[path = "unix.rs"]
21mod inner;
22
23#[cfg(windows)]
24#[path = "windows.rs"]
25mod inner;
26
27#[cfg(all(windows, feature = "clock"))]
28#[allow(unreachable_pub)]
29mod win_bindings;
30
31#[cfg(all(any(target_os = "android", target_env = "ohos", test), feature = "clock"))]
32mod 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))]
43mod 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))]
64mod 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 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 );
92 let offset = js_date.get_timezone_offset();
93 MappedLocalTime::Single(FixedOffset::west_opt((offset as i32) * 60).unwrap())
95 }
96}
97
98#[cfg(unix)]
99mod tz_info;
100
101#[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))]
126pub struct Local;
127
128impl Local {
129 #[deprecated(since = "0.4.23", note = "use `Local::now()` instead")]
131 #[allow(deprecated)]
132 #[must_use]
133 pub fn today() -> Date<Local> {
134 Local::now().date()
135 }
136
137 pub fn now() -> DateTime<Local> {
165 Utc::now().with_timezone(&Local)
166 }
167}
168
169impl TimeZone for Local {
170 type Offset = FixedOffset;
171
172 fn from_offset(_offset: &FixedOffset) -> Local {
173 Local
174 }
175
176 #[allow(deprecated)]
177 fn offset_from_local_date(&self, local: &NaiveDate) -> MappedLocalTime<FixedOffset> {
178 self.offset_from_local_datetime(&local.and_time(NaiveTime::MIN))
180 }
181
182 fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> MappedLocalTime<FixedOffset> {
183 inner::offset_from_local_datetime(local)
184 }
185
186 #[allow(deprecated)]
187 fn offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset {
188 self.offset_from_utc_datetime(&utc.and_time(NaiveTime::MIN))
190 }
191
192 fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> FixedOffset {
193 inner::offset_from_utc_datetime(utc).unwrap()
194 }
195}
196
197#[cfg(windows)]
198#[derive(Copy, Clone, Eq, PartialEq)]
199struct Transition {
200 transition_utc: NaiveDateTime,
201 offset_before: FixedOffset,
202 offset_after: FixedOffset,
203}
204
205#[cfg(windows)]
206impl Transition {
207 fn new(
208 transition_local: NaiveDateTime,
209 offset_before: FixedOffset,
210 offset_after: FixedOffset,
211 ) -> Transition {
212 let transition_utc = transition_local.overflowing_sub_offset(offset_before);
216 Transition { transition_utc, offset_before, offset_after }
217 }
218}
219
220#[cfg(windows)]
221impl PartialOrd for Transition {
222 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
223 Some(self.cmp(other))
224 }
225}
226
227#[cfg(windows)]
228impl Ord for Transition {
229 fn cmp(&self, other: &Self) -> Ordering {
230 self.transition_utc.cmp(&other.transition_utc)
231 }
232}
233
234#[cfg(windows)]
237fn lookup_with_dst_transitions(
238 transitions: &[Transition],
239 dt: NaiveDateTime,
240) -> MappedLocalTime<FixedOffset> {
241 for t in transitions.iter() {
242 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)]
279mod 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 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 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 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 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 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 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 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 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 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 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 assert_eq!(
504 lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX),
505 MappedLocalTime::Ambiguous(dst, std)
506 );
507
508 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 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 let bytes = rkyv::to_bytes::<_, 0>(&local).unwrap();
537 assert_eq!(bytes.len(), 0);
538
539 assert_eq!(rkyv::from_bytes::<Local>(&bytes).unwrap(), super::ArchivedLocal);
542 }
543}