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))]
125pub struct Local;
126
127impl Local {
128 #[deprecated(since = "0.4.23", note = "use `Local::now()` instead")]
130 #[allow(deprecated)]
131 #[must_use]
132 pub fn today() -> Date<Local> {
133 Local::now().date()
134 }
135
136 pub fn now() -> DateTime<Local> {
164 Utc::now().with_timezone(&Local)
165 }
166}
167
168impl TimeZone for Local {
169 type Offset = FixedOffset;
170
171 fn from_offset(_offset: &FixedOffset) -> Local {
172 Local
173 }
174
175 #[allow(deprecated)]
176 fn offset_from_local_date(&self, local: &NaiveDate) -> MappedLocalTime<FixedOffset> {
177 self.offset_from_local_datetime(&local.and_time(NaiveTime::MIN))
179 }
180
181 fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> MappedLocalTime<FixedOffset> {
182 inner::offset_from_local_datetime(local)
183 }
184
185 #[allow(deprecated)]
186 fn offset_from_utc_date(&self, utc: &NaiveDate) -> FixedOffset {
187 self.offset_from_utc_datetime(&utc.and_time(NaiveTime::MIN))
189 }
190
191 fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> FixedOffset {
192 inner::offset_from_utc_datetime(utc).unwrap()
193 }
194}
195
196#[cfg(windows)]
197#[derive(Copy, Clone, Eq, PartialEq)]
198struct Transition {
199 transition_utc: NaiveDateTime,
200 offset_before: FixedOffset,
201 offset_after: FixedOffset,
202}
203
204#[cfg(windows)]
205impl Transition {
206 fn new(
207 transition_local: NaiveDateTime,
208 offset_before: FixedOffset,
209 offset_after: FixedOffset,
210 ) -> Transition {
211 let transition_utc = transition_local.overflowing_sub_offset(offset_before);
215 Transition { transition_utc, offset_before, offset_after }
216 }
217}
218
219#[cfg(windows)]
220impl PartialOrd for Transition {
221 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
222 Some(self.cmp(other))
223 }
224}
225
226#[cfg(windows)]
227impl Ord for Transition {
228 fn cmp(&self, other: &Self) -> Ordering {
229 self.transition_utc.cmp(&other.transition_utc)
230 }
231}
232
233#[cfg(windows)]
236fn lookup_with_dst_transitions(
237 transitions: &[Transition],
238 dt: NaiveDateTime,
239) -> MappedLocalTime<FixedOffset> {
240 for t in transitions.iter() {
241 let (offset_min, offset_max) =
249 match t.offset_after.local_minus_utc() > t.offset_before.local_minus_utc() {
250 true => (t.offset_before, t.offset_after),
251 false => (t.offset_after, t.offset_before),
252 };
253 let wall_earliest = t.transition_utc.overflowing_add_offset(offset_min);
254 let wall_latest = t.transition_utc.overflowing_add_offset(offset_max);
255
256 if dt < wall_earliest {
257 return MappedLocalTime::Single(t.offset_before);
258 } else if dt <= wall_latest {
259 return match t.offset_after.local_minus_utc().cmp(&t.offset_before.local_minus_utc()) {
260 Ordering::Equal => MappedLocalTime::Single(t.offset_before),
261 Ordering::Less => MappedLocalTime::Ambiguous(t.offset_before, t.offset_after),
262 Ordering::Greater => {
263 if dt == wall_earliest {
264 MappedLocalTime::Single(t.offset_before)
265 } else if dt == wall_latest {
266 MappedLocalTime::Single(t.offset_after)
267 } else {
268 MappedLocalTime::None
269 }
270 }
271 };
272 }
273 }
274 MappedLocalTime::Single(transitions.last().unwrap().offset_after)
275}
276
277#[cfg(test)]
278mod tests {
279 use super::Local;
280 use crate::offset::TimeZone;
281 #[cfg(windows)]
282 use crate::offset::local::{Transition, lookup_with_dst_transitions};
283 use crate::{Datelike, Days, Utc};
284 #[cfg(windows)]
285 use crate::{FixedOffset, MappedLocalTime, NaiveDate, NaiveDateTime};
286
287 #[test]
288 fn verify_correct_offsets() {
289 let now = Local::now();
290 let from_local = Local.from_local_datetime(&now.naive_local()).unwrap();
291 let from_utc = Local.from_utc_datetime(&now.naive_utc());
292
293 assert_eq!(now.offset().local_minus_utc(), from_local.offset().local_minus_utc());
294 assert_eq!(now.offset().local_minus_utc(), from_utc.offset().local_minus_utc());
295
296 assert_eq!(now, from_local);
297 assert_eq!(now, from_utc);
298 }
299
300 #[test]
301 fn verify_correct_offsets_distant_past() {
302 let distant_past = Local::now() - Days::new(365 * 500);
303 let from_local = Local.from_local_datetime(&distant_past.naive_local()).unwrap();
304 let from_utc = Local.from_utc_datetime(&distant_past.naive_utc());
305
306 assert_eq!(distant_past.offset().local_minus_utc(), from_local.offset().local_minus_utc());
307 assert_eq!(distant_past.offset().local_minus_utc(), from_utc.offset().local_minus_utc());
308
309 assert_eq!(distant_past, from_local);
310 assert_eq!(distant_past, from_utc);
311 }
312
313 #[test]
314 fn verify_correct_offsets_distant_future() {
315 let distant_future = Local::now() + Days::new(365 * 35000);
316 let from_local = Local.from_local_datetime(&distant_future.naive_local()).unwrap();
317 let from_utc = Local.from_utc_datetime(&distant_future.naive_utc());
318
319 assert_eq!(
320 distant_future.offset().local_minus_utc(),
321 from_local.offset().local_minus_utc()
322 );
323 assert_eq!(distant_future.offset().local_minus_utc(), from_utc.offset().local_minus_utc());
324
325 assert_eq!(distant_future, from_local);
326 assert_eq!(distant_future, from_utc);
327 }
328
329 #[test]
330 fn test_local_date_sanity_check() {
331 assert_eq!(Local.with_ymd_and_hms(2999, 12, 28, 0, 0, 0).unwrap().day(), 28);
333 }
334
335 #[test]
336 fn test_leap_second() {
337 let today = Utc::now().date_naive();
339
340 if let Some(dt) = today.and_hms_milli_opt(15, 2, 59, 1000) {
341 let timestr = dt.time().to_string();
342 assert!(
345 timestr == "15:02:60" || timestr == "15:03:00",
346 "unexpected timestr {timestr:?}"
347 );
348 }
349
350 if let Some(dt) = today.and_hms_milli_opt(15, 2, 3, 1234) {
351 let timestr = dt.time().to_string();
352 assert!(
353 timestr == "15:02:03.234" || timestr == "15:02:04.234",
354 "unexpected timestr {timestr:?}"
355 );
356 }
357 }
358
359 #[test]
360 #[cfg(windows)]
361 fn test_lookup_with_dst_transitions() {
362 let ymdhms = |y, m, d, h, n, s| {
363 NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap()
364 };
365
366 #[track_caller]
367 #[allow(clippy::too_many_arguments)]
368 fn compare_lookup(
369 transitions: &[Transition],
370 y: i32,
371 m: u32,
372 d: u32,
373 h: u32,
374 n: u32,
375 s: u32,
376 result: MappedLocalTime<FixedOffset>,
377 ) {
378 let dt = NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap();
379 assert_eq!(lookup_with_dst_transitions(transitions, dt), result);
380 }
381
382 let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
385 let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap();
386 let transitions = [
387 Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, dst),
388 Transition::new(ymdhms(2023, 10, 29, 3, 0, 0), dst, std),
389 ];
390 compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, MappedLocalTime::Single(std));
391 compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, MappedLocalTime::Single(std));
392 compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, MappedLocalTime::None);
393 compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, MappedLocalTime::Single(dst));
394 compare_lookup(&transitions, 2023, 3, 26, 4, 0, 0, MappedLocalTime::Single(dst));
395
396 compare_lookup(&transitions, 2023, 10, 29, 1, 0, 0, MappedLocalTime::Single(dst));
397 compare_lookup(&transitions, 2023, 10, 29, 2, 0, 0, MappedLocalTime::Ambiguous(dst, std));
398 compare_lookup(&transitions, 2023, 10, 29, 2, 30, 0, MappedLocalTime::Ambiguous(dst, std));
399 compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, MappedLocalTime::Ambiguous(dst, std));
400 compare_lookup(&transitions, 2023, 10, 29, 4, 0, 0, MappedLocalTime::Single(std));
401
402 let std = FixedOffset::east_opt(-5 * 60 * 60).unwrap();
405 let dst = FixedOffset::east_opt(-4 * 60 * 60).unwrap();
406 let transitions = [
407 Transition::new(ymdhms(2023, 3, 24, 3, 0, 0), dst, std),
408 Transition::new(ymdhms(2023, 10, 27, 2, 0, 0), std, dst),
409 ];
410 compare_lookup(&transitions, 2023, 3, 24, 1, 0, 0, MappedLocalTime::Single(dst));
411 compare_lookup(&transitions, 2023, 3, 24, 2, 0, 0, MappedLocalTime::Ambiguous(dst, std));
412 compare_lookup(&transitions, 2023, 3, 24, 2, 30, 0, MappedLocalTime::Ambiguous(dst, std));
413 compare_lookup(&transitions, 2023, 3, 24, 3, 0, 0, MappedLocalTime::Ambiguous(dst, std));
414 compare_lookup(&transitions, 2023, 3, 24, 4, 0, 0, MappedLocalTime::Single(std));
415
416 compare_lookup(&transitions, 2023, 10, 27, 1, 0, 0, MappedLocalTime::Single(std));
417 compare_lookup(&transitions, 2023, 10, 27, 2, 0, 0, MappedLocalTime::Single(std));
418 compare_lookup(&transitions, 2023, 10, 27, 2, 30, 0, MappedLocalTime::None);
419 compare_lookup(&transitions, 2023, 10, 27, 3, 0, 0, MappedLocalTime::Single(dst));
420 compare_lookup(&transitions, 2023, 10, 27, 4, 0, 0, MappedLocalTime::Single(dst));
421
422 let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
425 let dst = FixedOffset::east_opt((2 * 60 + 30) * 60).unwrap();
426 let transitions = [
427 Transition::new(ymdhms(2023, 3, 26, 2, 30, 0), std, dst),
428 Transition::new(ymdhms(2023, 10, 29, 2, 0, 0), dst, std),
429 ];
430 compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, MappedLocalTime::Single(std));
431 compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, MappedLocalTime::Ambiguous(std, dst));
432 compare_lookup(&transitions, 2023, 3, 26, 2, 15, 0, MappedLocalTime::Ambiguous(std, dst));
433 compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, MappedLocalTime::Ambiguous(std, dst));
434 compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, MappedLocalTime::Single(dst));
435
436 compare_lookup(&transitions, 2023, 10, 29, 1, 0, 0, MappedLocalTime::Single(dst));
437 compare_lookup(&transitions, 2023, 10, 29, 2, 0, 0, MappedLocalTime::Single(dst));
438 compare_lookup(&transitions, 2023, 10, 29, 2, 15, 0, MappedLocalTime::None);
439 compare_lookup(&transitions, 2023, 10, 29, 2, 30, 0, MappedLocalTime::Single(std));
440 compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, MappedLocalTime::Single(std));
441
442 let std = FixedOffset::east_opt(-(4 * 60 + 30) * 60).unwrap();
445 let dst = FixedOffset::east_opt(-5 * 60 * 60).unwrap();
446 let transitions = [
447 Transition::new(ymdhms(2023, 3, 24, 2, 0, 0), dst, std),
448 Transition::new(ymdhms(2023, 10, 27, 2, 30, 0), std, dst),
449 ];
450 compare_lookup(&transitions, 2023, 3, 24, 1, 0, 0, MappedLocalTime::Single(dst));
451 compare_lookup(&transitions, 2023, 3, 24, 2, 0, 0, MappedLocalTime::Single(dst));
452 compare_lookup(&transitions, 2023, 3, 24, 2, 15, 0, MappedLocalTime::None);
453 compare_lookup(&transitions, 2023, 3, 24, 2, 30, 0, MappedLocalTime::Single(std));
454 compare_lookup(&transitions, 2023, 3, 24, 3, 0, 0, MappedLocalTime::Single(std));
455
456 compare_lookup(&transitions, 2023, 10, 27, 1, 0, 0, MappedLocalTime::Single(std));
457 compare_lookup(&transitions, 2023, 10, 27, 2, 0, 0, MappedLocalTime::Ambiguous(std, dst));
458 compare_lookup(&transitions, 2023, 10, 27, 2, 15, 0, MappedLocalTime::Ambiguous(std, dst));
459 compare_lookup(&transitions, 2023, 10, 27, 2, 30, 0, MappedLocalTime::Ambiguous(std, dst));
460 compare_lookup(&transitions, 2023, 10, 27, 3, 0, 0, MappedLocalTime::Single(dst));
461
462 let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
464 let transitions = [
465 Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, std),
466 Transition::new(ymdhms(2023, 10, 29, 3, 0, 0), std, std),
467 ];
468 compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, MappedLocalTime::Single(std));
469 compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, MappedLocalTime::Single(std));
470
471 let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
473 let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap();
474 let transitions = [Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, dst)];
475 compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, MappedLocalTime::Single(std));
476 compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, MappedLocalTime::Single(std));
477 compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, MappedLocalTime::None);
478 compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, MappedLocalTime::Single(dst));
479 compare_lookup(&transitions, 2023, 3, 26, 4, 0, 0, MappedLocalTime::Single(dst));
480 }
481
482 #[test]
483 #[cfg(windows)]
484 fn test_lookup_with_dst_transitions_limits() {
485 let std = FixedOffset::east_opt(3 * 60 * 60).unwrap();
487 let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap();
488 let transitions = [
489 Transition::new(NaiveDateTime::MAX.with_month(7).unwrap(), std, dst),
490 Transition::new(NaiveDateTime::MAX, dst, std),
491 ];
492 assert_eq!(
493 lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX.with_month(3).unwrap()),
494 MappedLocalTime::Single(std)
495 );
496 assert_eq!(
497 lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX.with_month(8).unwrap()),
498 MappedLocalTime::Single(dst)
499 );
500 assert_eq!(
503 lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX),
504 MappedLocalTime::Ambiguous(dst, std)
505 );
506
507 let std = FixedOffset::west_opt(3 * 60 * 60).unwrap();
509 let dst = FixedOffset::west_opt(4 * 60 * 60).unwrap();
510 let transitions = [
511 Transition::new(NaiveDateTime::MIN, std, dst),
512 Transition::new(NaiveDateTime::MIN.with_month(6).unwrap(), dst, std),
513 ];
514 assert_eq!(
515 lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN.with_month(3).unwrap()),
516 MappedLocalTime::Single(dst)
517 );
518 assert_eq!(
519 lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN.with_month(8).unwrap()),
520 MappedLocalTime::Single(std)
521 );
522 assert_eq!(
525 lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN),
526 MappedLocalTime::Ambiguous(std, dst)
527 );
528 }
529
530 #[test]
531 #[cfg(feature = "rkyv-validation")]
532 fn test_rkyv_validation() {
533 let local = Local;
534 let bytes = rkyv::to_bytes::<_, 0>(&local).unwrap();
536 assert_eq!(bytes.len(), 0);
537
538 assert_eq!(rkyv::from_bytes::<Local>(&bytes).unwrap(), super::ArchivedLocal);
541 }
542}