//! Deadline and meeting-time rules (docs/reference/old-inventory.md 2.4), on real IANA zones through //! `zones.rs`. A task has a deadline day, a meeting instant, or both. The user types a day and an //! optional `HH:MM` in their own zone; this module turns that into the values the store keeps. //! //! Rules: a time needs a day; hour and minute are both given or neither; a meeting sets the deadline //! day to the meeting's local day. A local time that does not exist (DST gap) or happens twice //! (DST overlap) is refused, because the user has to choose. use jiff::civil::Date; use jiff::tz::TimeZone; use jiff::{SignedDuration, Timestamp}; use time::OffsetDateTime; use crate::error::{Error, Result}; use crate::time::format_stamp; use crate::zones::{local_to_utc, utc_to_local}; /// How long before a meeting its reminder is due, in minutes, when the setting was never saved. pub const REMINDER_LEAD_MINUTES: i64 = 15; /// What the user typed for a deadline and meeting time. Every part is optional. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct MeetingInput { /// The day (a deadline, or the day of the meeting). pub day: Option, /// Hour of the meeting in the user's zone, 0 to 23. pub hour: Option, /// Minute of the meeting in the user's zone, 0 to 59. pub minute: Option, } /// Validates the input and resolves it to the stored values: the deadline day and the meeting as a /// UTC stamp (the fixed-width format of `time::format_stamp`). Errors are `Invalid` and name the rule. pub fn resolve_meeting( input: MeetingInput, zone: &TimeZone, ) -> Result<(Option, Option)> { match (input.day, input.hour, input.minute) { (day, None, None) => Ok((day, None)), (None, _, _) => Err(Error::Invalid("a meeting time needs a day".into())), (Some(day), Some(hour), Some(minute)) => { let hour = i8::try_from(hour) .map_err(|_| Error::Invalid(format!("hour {hour} is out of range")))?; let minute = i8::try_from(minute) .map_err(|_| Error::Invalid(format!("minute {minute} is out of range")))?; let at = local_to_utc(zone, day, hour, minute)?; let local_day = utc_to_local(zone, at).date(); Ok((Some(local_day), Some(stamp_of(at)?))) } _ => Err(Error::Invalid( "set both the hour and the minute, or neither".into(), )), } } /// The stored stamp of an instant. Fails only for instants outside the range of `time`. pub fn stamp_of(at: Timestamp) -> Result { OffsetDateTime::from_unix_timestamp_nanos(at.as_nanosecond()) .map(format_stamp) .map_err(|e| Error::Invalid(format!("meeting time out of range: {e}"))) } /// Parses a stored stamp back into an instant for comparisons. pub fn timestamp_of(stamp: &str) -> Result { stamp .parse::() .map_err(|e| Error::Database(format!("stored meeting time is not a stamp: {e}"))) } /// The moment a reminder for a meeting at `meeting` is due: `lead_minutes` before it. The lead comes from /// the `reminder_minutes` setting (0 turns reminders off, so the caller never asks for a due time then). pub fn reminder_due(meeting: Timestamp, lead_minutes: i64) -> Result { let due = meeting .checked_sub(SignedDuration::from_mins(lead_minutes)) .map_err(|e| Error::Invalid(format!("reminder time out of range: {e}")))?; stamp_of(due) } #[cfg(test)] mod tests { use super::*; use crate::zones::parse_zone; use jiff::civil::date; #[test] fn meeting_rules_truth_table() { let berlin = parse_zone("Europe/Berlin").unwrap(); let d = Some(date(2026, 1, 15)); // No time: the day stands alone. let plain = MeetingInput { day: d, hour: None, minute: None, }; assert_eq!(resolve_meeting(plain, &berlin).unwrap(), (d, None)); // Nothing at all clears both. assert_eq!( resolve_meeting(MeetingInput::default(), &berlin).unwrap(), (None, None) ); // Day and full time: 09:30 Berlin winter is 08:30Z, and the deadline day is kept. let full = MeetingInput { day: d, hour: Some(9), minute: Some(30), }; assert_eq!( resolve_meeting(full, &berlin).unwrap(), (d, Some("2026-01-15T08:30:00.000000000Z".into())) ); // A time without a day, or only half a time, is refused. let no_day = MeetingInput { day: None, hour: Some(9), minute: Some(30), }; assert!(matches!( resolve_meeting(no_day, &berlin), Err(Error::Invalid(_)) )); let half = MeetingInput { day: d, hour: Some(9), minute: None, }; assert!(matches!( resolve_meeting(half, &berlin), Err(Error::Invalid(_)) )); // A DST gap is refused (02:30 does not exist on 2026-03-29 in Berlin). let gap = MeetingInput { day: Some(date(2026, 3, 29)), hour: Some(2), minute: Some(30), }; assert!(matches!( resolve_meeting(gap, &berlin), Err(Error::Invalid(_)) )); // Out-of-range clock values are refused. let late = MeetingInput { day: d, hour: Some(24), minute: Some(0), }; assert!(matches!( resolve_meeting(late, &berlin), Err(Error::Invalid(_)) )); } #[test] fn reminder_is_lead_time_before_meeting() { let meeting: Timestamp = "2026-01-15T08:30:00Z".parse().unwrap(); assert_eq!( reminder_due(meeting, REMINDER_LEAD_MINUTES).unwrap(), "2026-01-15T08:15:00.000000000Z" ); } }