From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id D38641FF0BC for ; Wed, 02 Dec 2026 10:42:33 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id C9BB1216F7; Wed, 02 Dec 2026 10:42:14 +0100 (CET) From: Dominik Csapak To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox 2/2] time: calendar event: fix skipped events after a field wraps around Date: Wed, 2 Dec 2026 10:41:08 +0100 Message-ID: <20261202094209.1345186-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261202094209.1345186-1-d.csapak@proxmox.com> References: <20261202094209.1345186-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.240 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: DBTEBZLO3SEY7OQWR335ZZATMNPJ5GR6 X-Message-ID-Hash: DBTEBZLO3SEY7OQWR335ZZATMNPJ5GR6 X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: For repetitions without an end (e.g. '1/5'), `find_next` returned the next multiple even if that was not a valid value for the field anymore. libc then carried the overflow into the next larger unit and kept the remainder as an offset, so the first values there were skipped. For example, '1/5-5/10' (the 5th, 15th and 25th of January, June and November) never triggered in January: in December, the next month was calculated as 16, which is April of the next year, and the next match from there is in June. The same happened for e.g. '0/5:00', which skipped 00:00 every day. This only worked when the overflow happened to land exactly on the start of the next unit, like for '*/5' minutes or '*/3' months. Days had the same problem even without a repetition, since not all months have 31 days. E.g. '*-1,31' skipped the 1st of March, since the 31st of February is normalized to the 3rd of March (in non-leap years). To fix that, let `find_next` take the maximum valid value for the field and return `None` for anything above it. The callers already continue with the start of the next larger unit in that case. Signed-off-by: Dominik Csapak --- proxmox-time/src/calendar_event.rs | 13 +++++++------ proxmox-time/src/date_time_value.rs | 10 +++++++--- proxmox-time/src/test.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/proxmox-time/src/calendar_event.rs b/proxmox-time/src/calendar_event.rs index 1cc0eae9..4e38fa53 100644 --- a/proxmox-time/src/calendar_event.rs +++ b/proxmox-time/src/calendar_event.rs @@ -94,7 +94,7 @@ impl CalendarEvent { if !self.year.is_empty() { let year: u32 = t.year().try_into()?; if !DateTimeValue::list_contains(&self.year, year) { - if let Some(n) = DateTimeValue::find_next(&self.year, year) { + if let Some(n) = DateTimeValue::find_next(&self.year, year, u32::MAX) { t.add_years((n - year).try_into()?)?; continue; } else { @@ -107,7 +107,7 @@ impl CalendarEvent { if !self.month.is_empty() { let month: u32 = t.month().try_into()?; if !DateTimeValue::list_contains(&self.month, month) { - if let Some(n) = DateTimeValue::find_next(&self.month, month) { + if let Some(n) = DateTimeValue::find_next(&self.month, month, 12) { t.add_months((n - month).try_into()?)?; } else { // if we could not find valid month, retry next year @@ -120,7 +120,8 @@ impl CalendarEvent { if !self.day.is_empty() { let day: u32 = t.day().try_into()?; if !DateTimeValue::list_contains(&self.day, day) { - if let Some(n) = DateTimeValue::find_next(&self.day, day) { + let last_day = t.days_in_month().try_into()?; + if let Some(n) = DateTimeValue::find_next(&self.day, day, last_day) { t.add_days((n - day).try_into()?)?; } else { // if we could not find valid mday, retry next month @@ -152,7 +153,7 @@ impl CalendarEvent { if !self.hour.is_empty() { let hour = t.hour().try_into()?; if !DateTimeValue::list_contains(&self.hour, hour) { - if let Some(n) = DateTimeValue::find_next(&self.hour, hour) { + if let Some(n) = DateTimeValue::find_next(&self.hour, hour, 23) { // test next hour t.set_time(n.try_into()?, 0, 0)?; } else { @@ -167,7 +168,7 @@ impl CalendarEvent { if !self.minute.is_empty() { let minute = t.min().try_into()?; if !DateTimeValue::list_contains(&self.minute, minute) { - if let Some(n) = DateTimeValue::find_next(&self.minute, minute) { + if let Some(n) = DateTimeValue::find_next(&self.minute, minute, 59) { // test next minute t.set_min_sec(n.try_into()?, 0)?; } else { @@ -182,7 +183,7 @@ impl CalendarEvent { if !self.second.is_empty() { let second = t.sec().try_into()?; if !DateTimeValue::list_contains(&self.second, second) { - if let Some(n) = DateTimeValue::find_next(&self.second, second) { + if let Some(n) = DateTimeValue::find_next(&self.second, second, 59) { // test next second t.set_sec(n.try_into()?)?; } else { diff --git a/proxmox-time/src/date_time_value.rs b/proxmox-time/src/date_time_value.rs index 711ad205..7eeb7bf1 100644 --- a/proxmox-time/src/date_time_value.rs +++ b/proxmox-time/src/date_time_value.rs @@ -36,8 +36,8 @@ impl DateTimeValue { list.iter().any(|spec| spec.contains(value)) } - // Find an return an entry greater than value - pub fn find_next(list: &[DateTimeValue], value: u32) -> Option { + // Find and return the smallest entry greater than value, but not greater than max + pub fn find_next(list: &[DateTimeValue], value: u32, max: u32) -> Option { let mut next: Option = None; let mut set_next = |v: u32| { if let Some(n) = next { @@ -82,6 +82,10 @@ impl DateTimeValue { } } - next + match next { + // values past max would overflow into the next larger unit and skip its first entries + Some(n) if n > max => None, + other => other, + } } } diff --git a/proxmox-time/src/test.rs b/proxmox-time/src/test.rs index 306409f8..ec065b30 100644 --- a/proxmox-time/src/test.rs +++ b/proxmox-time/src/test.rs @@ -184,6 +184,32 @@ fn test_compute_next_event() -> Result<(), Error> { test_value("2020-7..9-2/2", JUL_31_2020, JUL_31_2020 + 2 * DAY)?; test_value("2020,2021-12-31", JUL_31_2020, DEC_31_2020)?; + // test that repetitions restart with the next larger unit + test_value("*:*:0/50", THURSDAY_00_00 + 50, THURSDAY_00_00 + MIN)?; + test_value("*:0/7", THURSDAY_00_00 + 56 * MIN, THURSDAY_00_00 + HOUR)?; + test_value("0/5:0", THURSDAY_00_00 + 20 * HOUR, THURSDAY_00_00 + DAY)?; + test_value("*/5-1", DEC_31_2020, DEC_31_2020 + DAY)?; + + let n = test_value("1/5-5/10", DEC_31_2020, DEC_31_2020 + 5 * DAY)?; + let n = test_value("1/5-5/10", n, DEC_31_2020 + 15 * DAY)?; + let n = test_value("1/5-5/10", n, DEC_31_2020 + 25 * DAY)?; + // 2021-06-05 + test_value( + "1/5-5/10", + n, + DEC_31_2020 + (31 + 28 + 31 + 30 + 31 + 5) * DAY, + )?; + + // test days that do not exist in shorter months + test_value("*-*/10", (31 + 21) * DAY, (31 + 28) * DAY)?; + test_value("*-1,31", (31 + 1) * DAY, (31 + 28) * DAY)?; + // 1972-02-22 => 1972-03-01 + test_value( + "*-*/10", + 2 * 365 * DAY + (31 + 21) * DAY, + 2 * 365 * DAY + (31 + 29) * DAY, + )?; + // keyword shortcuts test_value("minutely", THURSDAY_00_00, THURSDAY_00_00 + MIN)?; test_value("hourly", THURSDAY_00_00, THURSDAY_00_00 + HOUR)?; -- 2.47.3