* [PATCH proxmox 0/2] fix open ended repetitions in calendar events
@ 2026-12-02 9:41 Dominik Csapak
2026-12-02 9:41 ` [PATCH proxmox 1/2] time: tm editor: add `days_in_month` helper Dominik Csapak
2026-12-02 9:41 ` [PATCH proxmox 2/2] time: calendar event: fix skipped events after a field wraps around Dominik Csapak
0 siblings, 2 replies; 3+ messages in thread
From: Dominik Csapak @ 2026-12-02 9:41 UTC (permalink / raw)
To: pve-devel
Finding the next suitable time sometimes skipped values, e.g. for
open ended repetitions like "*-1/5-5/10", see the commit message of 2/2
for details.
Dominik Csapak (2):
time: tm editor: add `days_in_month` helper
time: calendar event: fix skipped events after a field wraps around
proxmox-time/src/calendar_event.rs | 13 ++++----
proxmox-time/src/date_time_value.rs | 10 ++++--
proxmox-time/src/test.rs | 50 +++++++++++++++++++++++++++++
proxmox-time/src/tm_editor.rs | 10 ++++++
4 files changed, 74 insertions(+), 9 deletions(-)
--
2.47.3
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH proxmox 1/2] time: tm editor: add `days_in_month` helper
2026-12-02 9:41 [PATCH proxmox 0/2] fix open ended repetitions in calendar events Dominik Csapak
@ 2026-12-02 9:41 ` Dominik Csapak
2026-12-02 9:41 ` [PATCH proxmox 2/2] time: calendar event: fix skipped events after a field wraps around Dominik Csapak
1 sibling, 0 replies; 3+ messages in thread
From: Dominik Csapak @ 2026-12-02 9:41 UTC (permalink / raw)
To: pve-devel
that returns the number of days of the current month, with the
correct value for February in leap years.
This will be used to limit the days of a calendar event to the ones
that actually exist in a month.
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
proxmox-time/src/test.rs | 24 ++++++++++++++++++++++++
proxmox-time/src/tm_editor.rs | 10 ++++++++++
2 files changed, 34 insertions(+)
diff --git a/proxmox-time/src/test.rs b/proxmox-time/src/test.rs
index d5fc7a73..306409f8 100644
--- a/proxmox-time/src/test.rs
+++ b/proxmox-time/src/test.rs
@@ -201,6 +201,30 @@ fn test_compute_next_event() -> Result<(), Error> {
Ok(())
}
+#[test]
+fn test_tm_editor_days_in_month() -> Result<(), Error> {
+ let test_value = |year: i32, month: i32, expect: i32| -> Result<(), Error> {
+ let mut t = TmEditor::new(true);
+ t.set_year(year)?;
+ t.set_mon(month)?;
+ assert_eq!(t.days_in_month(), expect, "{year}-{month:02}");
+ Ok(())
+ };
+
+ let days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+ for (month, expect) in (1..).zip(days) {
+ test_value(2023, month, expect)?;
+ }
+
+ test_value(2024, 2, 29)?;
+ // only centuries divisible by 400 are leap years
+ test_value(1900, 2, 28)?;
+ test_value(2000, 2, 29)?;
+ test_value(2100, 2, 28)?;
+
+ Ok(())
+}
+
#[test]
fn test_calendar_event_weekday() -> Result<(), Error> {
test_event("mon,wed..fri")?;
diff --git a/proxmox-time/src/tm_editor.rs b/proxmox-time/src/tm_editor.rs
index 42c88c4a..75732c9f 100644
--- a/proxmox-time/src/tm_editor.rs
+++ b/proxmox-time/src/tm_editor.rs
@@ -96,6 +96,16 @@ impl TmEditor {
pub fn day(&self) -> libc::c_int {
self.t.tm_mday
}
+ /// Returns the number of days of the current month
+ pub fn days_in_month(&self) -> libc::c_int {
+ let year = self.year();
+ match self.month() {
+ 2 if (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 => 29,
+ 2 => 28,
+ 4 | 6 | 9 | 11 => 30,
+ _ => 31,
+ }
+ }
pub fn hour(&self) -> libc::c_int {
self.t.tm_hour
}
--
2.47.3
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH proxmox 2/2] time: calendar event: fix skipped events after a field wraps around
2026-12-02 9:41 [PATCH proxmox 0/2] fix open ended repetitions in calendar events Dominik Csapak
2026-12-02 9:41 ` [PATCH proxmox 1/2] time: tm editor: add `days_in_month` helper Dominik Csapak
@ 2026-12-02 9:41 ` Dominik Csapak
1 sibling, 0 replies; 3+ messages in thread
From: Dominik Csapak @ 2026-12-02 9:41 UTC (permalink / raw)
To: pve-devel
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 <d.csapak@proxmox.com>
---
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<u32> {
+ // 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<u32> {
let mut next: Option<u32> = 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
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 9:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 9:41 [PATCH proxmox 0/2] fix open ended repetitions in calendar events Dominik Csapak
2026-12-02 9:41 ` [PATCH proxmox 1/2] time: tm editor: add `days_in_month` helper Dominik Csapak
2026-12-02 9:41 ` [PATCH proxmox 2/2] time: calendar event: fix skipped events after a field wraps around Dominik Csapak
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox