From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id BDFEB1FF0BC for ; Wed, 02 Dec 2026 10:42:25 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 689A4216BD; Wed, 02 Dec 2026 10:42:14 +0100 (CET) From: Dominik Csapak 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 Message-ID: <20261202094209.1345186-2-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: DQPG4NB2ZXJBHRT5JUWUOWC46JO4XYEV X-Message-ID-Hash: DQPG4NB2ZXJBHRT5JUWUOWC46JO4XYEV 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: 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 --- 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