From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH proxmox 1/2] time: tm editor: add `days_in_month` helper
Date: Wed, 2 Dec 2026 10:41:07 +0100 [thread overview]
Message-ID: <20261202094209.1345186-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20261202094209.1345186-1-d.csapak@proxmox.com>
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
next prev parent reply other threads:[~2026-12-02 9:42 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2026-12-02 9:41 ` [PATCH proxmox 2/2] time: calendar event: fix skipped events after a field wraps around Dominik Csapak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202094209.1345186-2-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox