|
3 | 3 |
|
4 | 4 | //! The local (system) time zone.
|
5 | 5 |
|
| 6 | +#[cfg(windows)] |
| 7 | +use std::cmp::Ordering; |
| 8 | + |
6 | 9 | #[cfg(any(feature = "rkyv", feature = "rkyv-16", feature = "rkyv-32", feature = "rkyv-64"))]
|
7 | 10 | use rkyv::{Archive, Deserialize, Serialize};
|
8 | 11 |
|
@@ -183,11 +186,96 @@ impl TimeZone for Local {
|
183 | 186 | }
|
184 | 187 | }
|
185 | 188 |
|
| 189 | +#[cfg(windows)] |
| 190 | +#[derive(Copy, Clone, Eq, PartialEq)] |
| 191 | +struct Transition { |
| 192 | + transition_utc: NaiveDateTime, |
| 193 | + offset_before: FixedOffset, |
| 194 | + offset_after: FixedOffset, |
| 195 | +} |
| 196 | + |
| 197 | +#[cfg(windows)] |
| 198 | +impl 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)] |
| 213 | +impl 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)] |
| 220 | +impl 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)] |
| 229 | +fn 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 | + |
186 | 270 | #[cfg(test)]
|
187 | 271 | mod tests {
|
188 | 272 | use super::Local;
|
| 273 | + #[cfg(windows)] |
| 274 | + use crate::offset::local::{lookup_with_dst_transitions, Transition}; |
189 | 275 | use crate::offset::TimeZone;
|
190 | 276 | use crate::{Datelike, TimeDelta, Utc};
|
| 277 | + #[cfg(windows)] |
| 278 | + use crate::{FixedOffset, LocalResult, NaiveDate, NaiveDateTime}; |
191 | 279 |
|
192 | 280 | #[test]
|
193 | 281 | fn verify_correct_offsets() {
|
@@ -264,6 +352,177 @@ mod tests {
|
264 | 352 | }
|
265 | 353 | }
|
266 | 354 |
|
| 355 | + #[test] |
| 356 | + #[cfg(windows)] |
| 357 | + fn test_lookup_with_dst_transitions() { |
| 358 | + let ymdhms = |y, m, d, h, n, s| { |
| 359 | + NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap() |
| 360 | + }; |
| 361 | + |
| 362 | + #[track_caller] |
| 363 | + #[allow(clippy::too_many_arguments)] |
| 364 | + fn compare_lookup( |
| 365 | + transitions: &[Transition], |
| 366 | + y: i32, |
| 367 | + m: u32, |
| 368 | + d: u32, |
| 369 | + h: u32, |
| 370 | + n: u32, |
| 371 | + s: u32, |
| 372 | + result: LocalResult<FixedOffset>, |
| 373 | + ) { |
| 374 | + let dt = NaiveDate::from_ymd_opt(y, m, d).unwrap().and_hms_opt(h, n, s).unwrap(); |
| 375 | + assert_eq!(lookup_with_dst_transitions(transitions, dt), result); |
| 376 | + } |
| 377 | + |
| 378 | + // dst transition before std transition |
| 379 | + // dst offset > std offset |
| 380 | + let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
| 381 | + let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap(); |
| 382 | + let transitions = [ |
| 383 | + Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, dst), |
| 384 | + Transition::new(ymdhms(2023, 10, 29, 3, 0, 0), dst, std), |
| 385 | + ]; |
| 386 | + compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, LocalResult::Single(std)); |
| 387 | + compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, LocalResult::Single(std)); |
| 388 | + compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, LocalResult::None); |
| 389 | + compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, LocalResult::Single(dst)); |
| 390 | + compare_lookup(&transitions, 2023, 3, 26, 4, 0, 0, LocalResult::Single(dst)); |
| 391 | + |
| 392 | + compare_lookup(&transitions, 2023, 10, 29, 1, 0, 0, LocalResult::Single(dst)); |
| 393 | + compare_lookup(&transitions, 2023, 10, 29, 2, 0, 0, LocalResult::Ambiguous(dst, std)); |
| 394 | + compare_lookup(&transitions, 2023, 10, 29, 2, 30, 0, LocalResult::Ambiguous(dst, std)); |
| 395 | + compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, LocalResult::Ambiguous(dst, std)); |
| 396 | + compare_lookup(&transitions, 2023, 10, 29, 4, 0, 0, LocalResult::Single(std)); |
| 397 | + |
| 398 | + // std transition before dst transition |
| 399 | + // dst offset > std offset |
| 400 | + let std = FixedOffset::east_opt(-5 * 60 * 60).unwrap(); |
| 401 | + let dst = FixedOffset::east_opt(-4 * 60 * 60).unwrap(); |
| 402 | + let transitions = [ |
| 403 | + Transition::new(ymdhms(2023, 3, 24, 3, 0, 0), dst, std), |
| 404 | + Transition::new(ymdhms(2023, 10, 27, 2, 0, 0), std, dst), |
| 405 | + ]; |
| 406 | + compare_lookup(&transitions, 2023, 3, 24, 1, 0, 0, LocalResult::Single(dst)); |
| 407 | + compare_lookup(&transitions, 2023, 3, 24, 2, 0, 0, LocalResult::Ambiguous(dst, std)); |
| 408 | + compare_lookup(&transitions, 2023, 3, 24, 2, 30, 0, LocalResult::Ambiguous(dst, std)); |
| 409 | + compare_lookup(&transitions, 2023, 3, 24, 3, 0, 0, LocalResult::Ambiguous(dst, std)); |
| 410 | + compare_lookup(&transitions, 2023, 3, 24, 4, 0, 0, LocalResult::Single(std)); |
| 411 | + |
| 412 | + compare_lookup(&transitions, 2023, 10, 27, 1, 0, 0, LocalResult::Single(std)); |
| 413 | + compare_lookup(&transitions, 2023, 10, 27, 2, 0, 0, LocalResult::Single(std)); |
| 414 | + compare_lookup(&transitions, 2023, 10, 27, 2, 30, 0, LocalResult::None); |
| 415 | + compare_lookup(&transitions, 2023, 10, 27, 3, 0, 0, LocalResult::Single(dst)); |
| 416 | + compare_lookup(&transitions, 2023, 10, 27, 4, 0, 0, LocalResult::Single(dst)); |
| 417 | + |
| 418 | + // dst transition before std transition |
| 419 | + // dst offset < std offset |
| 420 | + let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
| 421 | + let dst = FixedOffset::east_opt((2 * 60 + 30) * 60).unwrap(); |
| 422 | + let transitions = [ |
| 423 | + Transition::new(ymdhms(2023, 3, 26, 2, 30, 0), std, dst), |
| 424 | + Transition::new(ymdhms(2023, 10, 29, 2, 0, 0), dst, std), |
| 425 | + ]; |
| 426 | + compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, LocalResult::Single(std)); |
| 427 | + compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, LocalResult::Ambiguous(std, dst)); |
| 428 | + compare_lookup(&transitions, 2023, 3, 26, 2, 15, 0, LocalResult::Ambiguous(std, dst)); |
| 429 | + compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, LocalResult::Ambiguous(std, dst)); |
| 430 | + compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, LocalResult::Single(dst)); |
| 431 | + |
| 432 | + compare_lookup(&transitions, 2023, 10, 29, 1, 0, 0, LocalResult::Single(dst)); |
| 433 | + compare_lookup(&transitions, 2023, 10, 29, 2, 0, 0, LocalResult::Single(dst)); |
| 434 | + compare_lookup(&transitions, 2023, 10, 29, 2, 15, 0, LocalResult::None); |
| 435 | + compare_lookup(&transitions, 2023, 10, 29, 2, 30, 0, LocalResult::Single(std)); |
| 436 | + compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, LocalResult::Single(std)); |
| 437 | + |
| 438 | + // std transition before dst transition |
| 439 | + // dst offset < std offset |
| 440 | + let std = FixedOffset::east_opt(-(4 * 60 + 30) * 60).unwrap(); |
| 441 | + let dst = FixedOffset::east_opt(-5 * 60 * 60).unwrap(); |
| 442 | + let transitions = [ |
| 443 | + Transition::new(ymdhms(2023, 3, 24, 2, 0, 0), dst, std), |
| 444 | + Transition::new(ymdhms(2023, 10, 27, 2, 30, 0), std, dst), |
| 445 | + ]; |
| 446 | + compare_lookup(&transitions, 2023, 3, 24, 1, 0, 0, LocalResult::Single(dst)); |
| 447 | + compare_lookup(&transitions, 2023, 3, 24, 2, 0, 0, LocalResult::Single(dst)); |
| 448 | + compare_lookup(&transitions, 2023, 3, 24, 2, 15, 0, LocalResult::None); |
| 449 | + compare_lookup(&transitions, 2023, 3, 24, 2, 30, 0, LocalResult::Single(std)); |
| 450 | + compare_lookup(&transitions, 2023, 3, 24, 3, 0, 0, LocalResult::Single(std)); |
| 451 | + |
| 452 | + compare_lookup(&transitions, 2023, 10, 27, 1, 0, 0, LocalResult::Single(std)); |
| 453 | + compare_lookup(&transitions, 2023, 10, 27, 2, 0, 0, LocalResult::Ambiguous(std, dst)); |
| 454 | + compare_lookup(&transitions, 2023, 10, 27, 2, 15, 0, LocalResult::Ambiguous(std, dst)); |
| 455 | + compare_lookup(&transitions, 2023, 10, 27, 2, 30, 0, LocalResult::Ambiguous(std, dst)); |
| 456 | + compare_lookup(&transitions, 2023, 10, 27, 3, 0, 0, LocalResult::Single(dst)); |
| 457 | + |
| 458 | + // offset stays the same |
| 459 | + let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
| 460 | + let transitions = [ |
| 461 | + Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, std), |
| 462 | + Transition::new(ymdhms(2023, 10, 29, 3, 0, 0), std, std), |
| 463 | + ]; |
| 464 | + compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, LocalResult::Single(std)); |
| 465 | + compare_lookup(&transitions, 2023, 10, 29, 3, 0, 0, LocalResult::Single(std)); |
| 466 | + |
| 467 | + // single transition |
| 468 | + let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
| 469 | + let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap(); |
| 470 | + let transitions = [Transition::new(ymdhms(2023, 3, 26, 2, 0, 0), std, dst)]; |
| 471 | + compare_lookup(&transitions, 2023, 3, 26, 1, 0, 0, LocalResult::Single(std)); |
| 472 | + compare_lookup(&transitions, 2023, 3, 26, 2, 0, 0, LocalResult::Single(std)); |
| 473 | + compare_lookup(&transitions, 2023, 3, 26, 2, 30, 0, LocalResult::None); |
| 474 | + compare_lookup(&transitions, 2023, 3, 26, 3, 0, 0, LocalResult::Single(dst)); |
| 475 | + compare_lookup(&transitions, 2023, 3, 26, 4, 0, 0, LocalResult::Single(dst)); |
| 476 | + } |
| 477 | + |
| 478 | + #[test] |
| 479 | + #[cfg(windows)] |
| 480 | + fn test_lookup_with_dst_transitions_limits() { |
| 481 | + // Transition beyond UTC year end doesn't panic in year of `NaiveDate::MAX` |
| 482 | + let std = FixedOffset::east_opt(3 * 60 * 60).unwrap(); |
| 483 | + let dst = FixedOffset::east_opt(4 * 60 * 60).unwrap(); |
| 484 | + let transitions = [ |
| 485 | + Transition::new(NaiveDateTime::MAX.with_month(7).unwrap(), std, dst), |
| 486 | + Transition::new(NaiveDateTime::MAX, dst, std), |
| 487 | + ]; |
| 488 | + assert_eq!( |
| 489 | + lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX.with_month(3).unwrap()), |
| 490 | + LocalResult::Single(std) |
| 491 | + ); |
| 492 | + assert_eq!( |
| 493 | + lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX.with_month(8).unwrap()), |
| 494 | + LocalResult::Single(dst) |
| 495 | + ); |
| 496 | + // Doesn't panic with `NaiveDateTime::MAX` as argument (which would be out of range when |
| 497 | + // converted to UTC). |
| 498 | + assert_eq!( |
| 499 | + lookup_with_dst_transitions(&transitions, NaiveDateTime::MAX), |
| 500 | + LocalResult::Ambiguous(dst, std) |
| 501 | + ); |
| 502 | + |
| 503 | + // Transition before UTC year end doesn't panic in year of `NaiveDate::MIN` |
| 504 | + let std = FixedOffset::west_opt(3 * 60 * 60).unwrap(); |
| 505 | + let dst = FixedOffset::west_opt(4 * 60 * 60).unwrap(); |
| 506 | + let transitions = [ |
| 507 | + Transition::new(NaiveDateTime::MIN, std, dst), |
| 508 | + Transition::new(NaiveDateTime::MIN.with_month(6).unwrap(), dst, std), |
| 509 | + ]; |
| 510 | + assert_eq!( |
| 511 | + lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN.with_month(3).unwrap()), |
| 512 | + LocalResult::Single(dst) |
| 513 | + ); |
| 514 | + assert_eq!( |
| 515 | + lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN.with_month(8).unwrap()), |
| 516 | + LocalResult::Single(std) |
| 517 | + ); |
| 518 | + // Doesn't panic with `NaiveDateTime::MIN` as argument (which would be out of range when |
| 519 | + // converted to UTC). |
| 520 | + assert_eq!( |
| 521 | + lookup_with_dst_transitions(&transitions, NaiveDateTime::MIN), |
| 522 | + LocalResult::Ambiguous(std, dst) |
| 523 | + ); |
| 524 | + } |
| 525 | + |
267 | 526 | #[test]
|
268 | 527 | #[cfg(feature = "rkyv-validation")]
|
269 | 528 | fn test_rkyv_validation() {
|
|
0 commit comments