Migrate date/time handling from Chrono to Jiff (#8) #10

Open
agent wants to merge 3 commits from agent/tauclock:issue-8-migrate-chrono-jiff into main
2 changed files with 52 additions and 22 deletions
Showing only changes of commit 1d01c501ca - Show all commits
+5 -3
View File
@@ -68,8 +68,9 @@ cargo tauri build
### 日付時刻ライブラリと依存監査 ### 日付時刻ライブラリと依存監査
日時処理は、RUSTSEC-2026-0543で保守停止が告知されたChronoではなくJiff 1.xを使用します。 日時処理は、RUSTSEC-2026-0543で保守停止が告知されたChronoではなくJiff 1.xを使用します。
日常のローカル時刻、UTC表示、アラームの暦日計算はJiffが担当します。夏時間の重複時刻は 日常のローカル時刻、UTC表示、アラームの暦日計算はJiffが担当します。互換性維持のため、
早い方、存在しない時刻は後の有効な時刻へ解決します。保存済みアラームJSONの形式は変更しません。 夏時間の重複時刻は早い方を選び、存在しない時刻は従来どおりcivil timeをUTCとして解釈する
fallbackを使います。保存済みアラームJSONの形式は変更しません。
```bash ```bash
cd src-tauri cd src-tauri
@@ -79,7 +80,8 @@ cargo audit --deny warnings
Tauriが利用するcrateの非有効なoptional dependencyとして、`Cargo.lock`にChronoが残る場合があります。 Tauriが利用するcrateの非有効なoptional dependencyとして、`Cargo.lock`にChronoが残る場合があります。
`cargo tree`でactive dependencyではないことを確認してください。`cargo audit`はlockfile単位で検査するため、 `cargo tree`でactive dependencyではないことを確認してください。`cargo audit`はlockfile単位で検査するため、
この非有効なentryもRUSTSEC-2026-0543として報告します。 この非有効なentryもRUSTSEC-2026-0543として報告します。Tauri/Linux GUI由来のその他の
warning-class advisoryは [Issue #9](https://git.rumginger.org/kamat/tauclock/issues/9) で追跡します。
## ライセンス ## ライセンス
+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 log::{debug, error, info, trace, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
@@ -878,28 +881,45 @@ fn now_in_time_zone(time_zone: &TimeZone) -> ZonedDateTime {
Timestamp::now().to_zoned_datetime(time_zone.clone()) Timestamp::now().to_zoned_datetime(time_zone.clone())
} }
/// Calculates the next trigger timestamp in milliseconds for an alarm. Jiff's /// Calculates the next trigger timestamp in milliseconds for an alarm while
/// compatible resolution chooses the earlier instant in a fold and the later /// preserving the legacy local-time ambiguity policy. A fold selects the
/// valid wall time in a gap. /// 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 { fn resolve_next_trigger(hour: u8, minute: u8, now_local: &ZonedDateTime) -> i64 {
let time_zone = now_local.time_zone().clone(); let time_zone = now_local.time_zone().clone();
let mut candidate = now_local let mut candidate = combine_local(now_local.date(), hour, minute, &time_zone);
.date()
.at(hour as i8, minute as i8, 0, 0)
.to_zoned_datetime(time_zone.clone())
.expect("alarm date is representable");
if candidate.timestamp().as_millisecond() <= now_local.timestamp().as_millisecond() { if candidate.timestamp().as_millisecond() <= now_local.timestamp().as_millisecond() {
candidate = now_local candidate = combine_local(
.date() now_local
.tomorrow() .date()
.expect("tomorrow is representable") .tomorrow()
.at(hour as i8, minute as i8, 0, 0) .expect("tomorrow is representable"),
.to_zoned_datetime(time_zone) hour,
.expect("next alarm date is representable"); minute,
&time_zone,
);
} }
candidate.timestamp().as_millisecond() 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)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -962,12 +982,20 @@ mod tests {
} }
#[test] #[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 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!( assert_eq!(
resolve_next_trigger(2, 30, &now), resolve_next_trigger(2, 30, &now),
milliseconds("2024-03-10T07:30:00Z") milliseconds("2024-03-11T06:30:00Z")
); );
} }