From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 5D9B782879 for ; Tue, 30 Nov 2021 13:13:13 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0479026A43 for ; Tue, 30 Nov 2021 13:12:20 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id EC2D12692D for ; Tue, 30 Nov 2021 13:12:12 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id C356B44689 for ; Tue, 30 Nov 2021 13:12:12 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Tue, 30 Nov 2021 13:12:06 +0100 Message-Id: <20211130121209.216846-13-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com> References: <20211130121209.216846-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.183 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [test.rs] Subject: [pbs-devel] [PATCH proxmox 12/14] proxmox-time: calendar-events: make compute_next_event a method X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 Nov 2021 12:13:13 -0000 and deprecated the standalone function Signed-off-by: Dominik Csapak --- proxmox-time/src/calendar_event.rs | 221 +++++++++++++++-------------- proxmox-time/src/test.rs | 4 +- 2 files changed, 117 insertions(+), 108 deletions(-) diff --git a/proxmox-time/src/calendar_event.rs b/proxmox-time/src/calendar_event.rs index 1c21a84..a839f9d 100644 --- a/proxmox-time/src/calendar_event.rs +++ b/proxmox-time/src/calendar_event.rs @@ -35,141 +35,150 @@ pub struct CalendarEvent { pub(crate) year: Vec, } -/// Verify the format of the [CalendarEvent] -pub fn verify_calendar_event(i: &str) -> Result<(), Error> { - parse_calendar_event(i)?; - Ok(()) -} - -/// Compute the next event -pub fn compute_next_event( - event: &CalendarEvent, - last: i64, - utc: bool, -) -> Result, Error> { +impl CalendarEvent { + /// Computes the next timestamp after `last`. If `utc` is false, the local + /// timezone will be used for the calculation. + pub fn compute_next_event(&self, last: i64, utc: bool) -> Result, Error> { + let last = last + 1; // at least one second later - let last = last + 1; // at least one second later + let all_days = self.days.is_empty() || self.days.is_all(); - let all_days = event.days.is_empty() || event.days.is_all(); + let mut t = TmEditor::with_epoch(last, utc)?; - let mut t = TmEditor::with_epoch(last, utc)?; + let mut count = 0; - let mut count = 0; - - loop { - // cancel after 1000 loops - if count > 1000 { - return Ok(None); - } else { - count += 1; - } + loop { + // cancel after 1000 loops + if count > 1000 { + return Ok(None); + } else { + count += 1; + } - if !event.year.is_empty() { - let year: u32 = t.year().try_into()?; - if !DateTimeValue::list_contains(&event.year, year) { - if let Some(n) = DateTimeValue::find_next(&event.year, year) { - t.add_years((n - year).try_into()?)?; - continue; - } else { - // if we have no valid year, we cannot find a correct timestamp - return Ok(None); + 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) { + t.add_years((n - year).try_into()?)?; + continue; + } else { + // if we have no valid year, we cannot find a correct timestamp + return Ok(None); + } } } - } - if !event.month.is_empty() { - let month: u32 = t.month().try_into()?; - if !DateTimeValue::list_contains(&event.month, month) { - if let Some(n) = DateTimeValue::find_next(&event.month, month) { - t.add_months((n - month).try_into()?)?; - } else { - // if we could not find valid month, retry next year - t.add_years(1)?; + 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) { + t.add_months((n - month).try_into()?)?; + } else { + // if we could not find valid month, retry next year + t.add_years(1)?; + } + continue; } - continue; } - } - if !event.day.is_empty() { - let day: u32 = t.day().try_into()?; - if !DateTimeValue::list_contains(&event.day, day) { - if let Some(n) = DateTimeValue::find_next(&event.day, day) { - t.add_days((n - day).try_into()?)?; - } else { - // if we could not find valid mday, retry next month - t.add_months(1)?; + 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) { + t.add_days((n - day).try_into()?)?; + } else { + // if we could not find valid mday, retry next month + t.add_months(1)?; + } + continue; } - continue; } - } - if !all_days { // match day first - let day_num: u32 = t.day_num().try_into()?; - let day = WeekDays::from_bits(1< Result<(), Error> { + parse_calendar_event(i)?; + Ok(()) +} + +/// Compute the next event. Use [CalendarEvent::compute_next_event] instead. +#[deprecated="use method 'compute_next_event' of CalendarEvent instead"] +pub fn compute_next_event( + event: &CalendarEvent, + last: i64, + utc: bool, +) -> Result, Error> { + event.compute_next_event(last, utc) +} + /// Parse a [CalendarEvent] pub fn parse_calendar_event(i: &str) -> Result { parse_complete_line("calendar event", i, parse_calendar_event_incomplete) diff --git a/proxmox-time/src/test.rs b/proxmox-time/src/test.rs index 3007156..8c5e1cf 100644 --- a/proxmox-time/src/test.rs +++ b/proxmox-time/src/test.rs @@ -23,7 +23,7 @@ fn test_compute_next_event() -> Result<(), Error> { Err(err) => bail!("parsing '{}' failed - {}", v, err), }; - match compute_next_event(&event, last, true) { + match event.compute_next_event(last, true) { Ok(Some(next)) => { if next == expect { println!("next {:?} => {}", event, next); @@ -49,7 +49,7 @@ fn test_compute_next_event() -> Result<(), Error> { Err(err) => bail!("parsing '{}' failed - {}", v, err), }; - match compute_next_event(&event, last, true)? { + match event.compute_next_event(last, true)? { None => Ok(()), Some(next) => bail!( "compute next for '{}' succeeded, but expected fail - result {}", -- 2.30.2