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 4F31082C3A for ; Wed, 1 Dec 2021 09:45:15 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4D57B8A03 for ; Wed, 1 Dec 2021 09:45:15 +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 E96DD89D2 for ; Wed, 1 Dec 2021 09:45:13 +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 0EAC344324 for ; Wed, 1 Dec 2021 09:45:09 +0100 (CET) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Wed, 1 Dec 2021 09:45:06 +0100 Message-Id: <20211201084507.1510270-2-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211201084507.1510270-1-d.csapak@proxmox.com> References: <20211201084507.1510270-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.182 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 1/1] proxmox-time: calendar-events: parse 'UTC' timezone into calendarevent 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: Wed, 01 Dec 2021 08:45:15 -0000 like we do in pve. this breaks the (newly added) compute_next_event method of CalendarEvent, but we can keep compatibilty with the deprecated function adapt the tests so that they add a ' UTC' by default (so they can run reliably on any machine) Signed-off-by: Dominik Csapak --- proxmox-time/src/calendar_event.rs | 25 ++++++++++++++++++++++--- proxmox-time/src/test.rs | 8 ++++---- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/proxmox-time/src/calendar_event.rs b/proxmox-time/src/calendar_event.rs index d1d74a2..efde81a 100644 --- a/proxmox-time/src/calendar_event.rs +++ b/proxmox-time/src/calendar_event.rs @@ -19,6 +19,8 @@ use crate::{parse_weekdays_range, TmEditor, WeekDays}; /// specification, but are not guaranteed to be 100% compatible. #[derive(Default, Clone, Debug)] pub struct CalendarEvent { + /// if true, the event is calculated in utc and the local timezone otherwise + utc: bool, /// the days in a week this event should trigger pub(crate) days: WeekDays, /// the second(s) this event should trigger @@ -38,12 +40,12 @@ pub struct CalendarEvent { 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> { + pub fn compute_next_event(&self, last: i64) -> Result, Error> { let last = last + 1; // at least one second later let all_days = self.days.is_empty() || self.days.is_all(); - let mut t = TmEditor::with_epoch(last, utc)?; + let mut t = TmEditor::with_epoch(last, self.utc)?; let mut count = 0; @@ -184,7 +186,12 @@ pub fn compute_next_event( last: i64, utc: bool, ) -> Result, Error> { - event.compute_next_event(last, utc) + if event.utc != utc { + let mut event = event.clone(); + event.utc = utc; + return event.compute_next_event(last); + } + event.compute_next_event(last) } /// Parse a [CalendarEvent] @@ -199,6 +206,10 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> let mut has_datespec = false; let mut event = CalendarEvent::default(); + if let Some(n) = i.strip_suffix("UTC") { + event.utc = true; + i = n.trim_end_matches(' '); + } if i.starts_with(|c: char| char::is_ascii_alphabetic(&c)) { match i { @@ -206,6 +217,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> return Ok(( "", CalendarEvent { + utc: event.utc, second: vec![DateTimeValue::Single(0)], ..Default::default() }, @@ -215,6 +227,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> return Ok(( "", CalendarEvent { + utc: event.utc, minute: vec![DateTimeValue::Single(0)], second: vec![DateTimeValue::Single(0)], ..Default::default() @@ -225,6 +238,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> return Ok(( "", CalendarEvent { + utc: event.utc, hour: vec![DateTimeValue::Single(0)], minute: vec![DateTimeValue::Single(0)], second: vec![DateTimeValue::Single(0)], @@ -236,6 +250,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> return Ok(( "", CalendarEvent { + utc: event.utc, hour: vec![DateTimeValue::Single(0)], minute: vec![DateTimeValue::Single(0)], second: vec![DateTimeValue::Single(0)], @@ -248,6 +263,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> return Ok(( "", CalendarEvent { + utc: event.utc, hour: vec![DateTimeValue::Single(0)], minute: vec![DateTimeValue::Single(0)], second: vec![DateTimeValue::Single(0)], @@ -260,6 +276,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> return Ok(( "", CalendarEvent { + utc: event.utc, hour: vec![DateTimeValue::Single(0)], minute: vec![DateTimeValue::Single(0)], second: vec![DateTimeValue::Single(0)], @@ -273,6 +290,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> return Ok(( "", CalendarEvent { + utc: event.utc, hour: vec![DateTimeValue::Single(0)], minute: vec![DateTimeValue::Single(0)], second: vec![DateTimeValue::Single(0)], @@ -291,6 +309,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent> return Ok(( "", CalendarEvent { + utc: event.utc, hour: vec![DateTimeValue::Single(0)], minute: vec![DateTimeValue::Single(0)], second: vec![DateTimeValue::Single(0)], diff --git a/proxmox-time/src/test.rs b/proxmox-time/src/test.rs index ec63a37..c5c23b4 100644 --- a/proxmox-time/src/test.rs +++ b/proxmox-time/src/test.rs @@ -18,12 +18,12 @@ const fn make_test_time(mday: i32, hour: i32, min: i32) -> i64 { #[test] fn test_compute_next_event() -> Result<(), Error> { let test_value = |v: &'static str, last: i64, expect: i64| -> Result { - let event: CalendarEvent = match v.parse() { + let event: CalendarEvent = match format!("{} UTC", v).parse() { Ok(event) => event, Err(err) => bail!("parsing '{}' failed - {}", v, err), }; - match event.compute_next_event(last, true) { + match event.compute_next_event(last) { Ok(Some(next)) => { if next == expect { println!("next {:?} => {}", event, next); @@ -44,12 +44,12 @@ fn test_compute_next_event() -> Result<(), Error> { }; let test_never = |v: &'static str, last: i64| -> Result<(), Error> { - let event: CalendarEvent = match v.parse() { + let event: CalendarEvent = match format!("{} UTC", v).parse() { Ok(event) => event, Err(err) => bail!("parsing '{}' failed - {}", v, err), }; - match event.compute_next_event(last, true)? { + match event.compute_next_event(last)? { None => Ok(()), Some(next) => bail!( "compute next for '{}' succeeded, but expected fail - result {}", -- 2.30.2