fix: preserve legacy DST gap scheduling (#8)

This commit is contained in:
2026-11-20 13:22:13 +09:00
parent 619ca9e33e
commit 1d01c501ca
2 changed files with 52 additions and 22 deletions
+47 -19
View File
@@ -1,4 +1,7 @@
use jiff::{tz::TimeZone, Timestamp, ZonedDateTime};
use jiff::{
tz::{AmbiguousOffset, TimeZone},
Timestamp, ZonedDateTime,
};
use log::{debug, error, info, trace, warn};
use serde::{Deserialize, Serialize};
use std::{
@@ -878,28 +881,45 @@ fn now_in_time_zone(time_zone: &TimeZone) -> ZonedDateTime {
Timestamp::now().to_zoned_datetime(time_zone.clone())
}
/// Calculates the next trigger timestamp in milliseconds for an alarm. Jiff's
/// compatible resolution chooses the earlier instant in a fold and the later
/// valid wall time in a gap.
/// Calculates the next trigger timestamp in milliseconds for an alarm while
/// preserving the legacy local-time ambiguity policy. A fold selects the
/// earlier occurrence. A gap interprets the nonexistent civil time as UTC
/// before converting it back to the local zone.
fn resolve_next_trigger(hour: u8, minute: u8, now_local: &ZonedDateTime) -> i64 {
let time_zone = now_local.time_zone().clone();
let mut candidate = now_local
.date()
.at(hour as i8, minute as i8, 0, 0)
.to_zoned_datetime(time_zone.clone())
.expect("alarm date is representable");
let mut candidate = combine_local(now_local.date(), hour, minute, &time_zone);
if candidate.timestamp().as_millisecond() <= now_local.timestamp().as_millisecond() {
candidate = now_local
.date()
.tomorrow()
.expect("tomorrow is representable")
.at(hour as i8, minute as i8, 0, 0)
.to_zoned_datetime(time_zone)
.expect("next alarm date is representable");
candidate = combine_local(
now_local
.date()
.tomorrow()
.expect("tomorrow is representable"),
hour,
minute,
&time_zone,
);
}
candidate.timestamp().as_millisecond()
}
fn combine_local(
date: jiff::civil::Date,
hour: u8,
minute: u8,
time_zone: &TimeZone,
) -> ZonedDateTime {
let civil = date.at(hour as i8, minute as i8, 0, 0);
let ambiguous = time_zone.to_ambiguous_zoned_datetime(civil);
if matches!(ambiguous.offset(), AmbiguousOffset::Gap { .. }) {
return civil
.to_zoned_datetime(TimeZone::UTC)
.expect("alarm date is representable in UTC")
.timestamp()
.to_zoned_datetime(time_zone.clone());
}
ambiguous.compatible().expect("alarm date is representable")
}
#[cfg(test)]
mod tests {
use super::*;
@@ -962,12 +982,20 @@ mod tests {
}
#[test]
fn new_york_gap_uses_jiff_compatible_later_time() {
fn new_york_gap_preserves_the_legacy_utc_fallback() {
let new_york = named_zone("America/New_York");
let now = zoned("2024-03-10T00:00:00Z", new_york);
let gap_date: jiff::civil::Date = "2024-03-10".parse().expect("valid date");
assert_eq!(
combine_local(gap_date, 2, 30, &new_york)
.timestamp()
.as_millisecond(),
milliseconds("2024-03-10T02:30:00Z")
);
let now = zoned("2024-03-10T05:00:00Z", new_york);
assert_eq!(
resolve_next_trigger(2, 30, &now),
milliseconds("2024-03-10T07:30:00Z")
milliseconds("2024-03-11T06:30:00Z")
);
}