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