forge
Explore
Log in

Don't you dare go Hollow

iz

Clone
★ 0

0 watching0 forksRustAGPL-3.0567.3 KiB3950aceTahinli27 days ago34 commits

clock.rs160 lines, 5.9 KiBHistoryRawDownload
1//! Deadline and meeting-time rules (docs/reference/old-inventory.md 2.4), on real IANA zones through
2//! `zones.rs`. A task has a deadline day, a meeting instant, or both. The user types a day and an
3//! optional `HH:MM` in their own zone; this module turns that into the values the store keeps.
4//!
5//! Rules: a time needs a day; hour and minute are both given or neither; a meeting sets the deadline
6//! day to the meeting's local day. A local time that does not exist (DST gap) or happens twice
7//! (DST overlap) is refused, because the user has to choose.
8
9use jiff::civil::Date;
10use jiff::tz::TimeZone;
11use jiff::{SignedDuration, Timestamp};
12use time::OffsetDateTime;
13
14use crate::error::{Error, Result};
15use crate::time::format_stamp;
16use crate::zones::{local_to_utc, utc_to_local};
17
18/// How long before a meeting its reminder is due, in minutes, when the setting was never saved.
19pub const REMINDER_LEAD_MINUTES: i64 = 15;
20
21/// What the user typed for a deadline and meeting time. Every part is optional.
22#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
23pub struct MeetingInput {
24 /// The day (a deadline, or the day of the meeting).
25 pub day: Option<Date>,
26 /// Hour of the meeting in the user's zone, 0 to 23.
27 pub hour: Option<u8>,
28 /// Minute of the meeting in the user's zone, 0 to 59.
29 pub minute: Option<u8>,
30}
31
32/// Validates the input and resolves it to the stored values: the deadline day and the meeting as a
33/// UTC stamp (the fixed-width format of `time::format_stamp`). Errors are `Invalid` and name the rule.
34pub fn resolve_meeting(
35 input: MeetingInput,
36 zone: &TimeZone,
37) -> Result<(Option<Date>, Option<String>)> {
38 match (input.day, input.hour, input.minute) {
39 (day, None, None) => Ok((day, None)),
40 (None, _, _) => Err(Error::Invalid("a meeting time needs a day".into())),
41 (Some(day), Some(hour), Some(minute)) => {
42 let hour = i8::try_from(hour)
43 .map_err(|_| Error::Invalid(format!("hour {hour} is out of range")))?;
44 let minute = i8::try_from(minute)
45 .map_err(|_| Error::Invalid(format!("minute {minute} is out of range")))?;
46 let at = local_to_utc(zone, day, hour, minute)?;
47 let local_day = utc_to_local(zone, at).date();
48 Ok((Some(local_day), Some(stamp_of(at)?)))
49 }
50 _ => Err(Error::Invalid(
51 "set both the hour and the minute, or neither".into(),
52 )),
53 }
54}
55
56/// The stored stamp of an instant. Fails only for instants outside the range of `time`.
57pub fn stamp_of(at: Timestamp) -> Result<String> {
58 OffsetDateTime::from_unix_timestamp_nanos(at.as_nanosecond())
59 .map(format_stamp)
60 .map_err(|e| Error::Invalid(format!("meeting time out of range: {e}")))
61}
62
63/// Parses a stored stamp back into an instant for comparisons.
64pub fn timestamp_of(stamp: &str) -> Result<Timestamp> {
65 stamp
66 .parse::<Timestamp>()
67 .map_err(|e| Error::Database(format!("stored meeting time is not a stamp: {e}")))
68}
69
70/// The moment a reminder for a meeting at `meeting` is due: `lead_minutes` before it. The lead comes from
71/// the `reminder_minutes` setting (0 turns reminders off, so the caller never asks for a due time then).
72pub fn reminder_due(meeting: Timestamp, lead_minutes: i64) -> Result<String> {
73 let due = meeting
74 .checked_sub(SignedDuration::from_mins(lead_minutes))
75 .map_err(|e| Error::Invalid(format!("reminder time out of range: {e}")))?;
76 stamp_of(due)
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82 use crate::zones::parse_zone;
83 use jiff::civil::date;
84
85 #[test]
86 fn meeting_rules_truth_table() {
87 let berlin = parse_zone("Europe/Berlin").unwrap();
88 let d = Some(date(2026, 1, 15));
89 // No time: the day stands alone.
90 let plain = MeetingInput {
91 day: d,
92 hour: None,
93 minute: None,
94 };
95 assert_eq!(resolve_meeting(plain, &berlin).unwrap(), (d, None));
96 // Nothing at all clears both.
97 assert_eq!(
98 resolve_meeting(MeetingInput::default(), &berlin).unwrap(),
99 (None, None)
100 );
101 // Day and full time: 09:30 Berlin winter is 08:30Z, and the deadline day is kept.
102 let full = MeetingInput {
103 day: d,
104 hour: Some(9),
105 minute: Some(30),
106 };
107 assert_eq!(
108 resolve_meeting(full, &berlin).unwrap(),
109 (d, Some("2026-01-15T08:30:00.000000000Z".into()))
110 );
111 // A time without a day, or only half a time, is refused.
112 let no_day = MeetingInput {
113 day: None,
114 hour: Some(9),
115 minute: Some(30),
116 };
117 assert!(matches!(
118 resolve_meeting(no_day, &berlin),
119 Err(Error::Invalid(_))
120 ));
121 let half = MeetingInput {
122 day: d,
123 hour: Some(9),
124 minute: None,
125 };
126 assert!(matches!(
127 resolve_meeting(half, &berlin),
128 Err(Error::Invalid(_))
129 ));
130 // A DST gap is refused (02:30 does not exist on 2026-03-29 in Berlin).
131 let gap = MeetingInput {
132 day: Some(date(2026, 3, 29)),
133 hour: Some(2),
134 minute: Some(30),
135 };
136 assert!(matches!(
137 resolve_meeting(gap, &berlin),
138 Err(Error::Invalid(_))
139 ));
140 // Out-of-range clock values are refused.
141 let late = MeetingInput {
142 day: d,
143 hour: Some(24),
144 minute: Some(0),
145 };
146 assert!(matches!(
147 resolve_meeting(late, &berlin),
148 Err(Error::Invalid(_))
149 ));
150 }
151
152 #[test]
153 fn reminder_is_lead_time_before_meeting() {
154 let meeting: Timestamp = "2026-01-15T08:30:00Z".parse().unwrap();
155 assert_eq!(
156 reminder_due(meeting, REMINDER_LEAD_MINUTES).unwrap(),
157 "2026-01-15T08:15:00.000000000Z"
158 );
159 }
160}