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 80F867C68E for ; Fri, 5 Nov 2021 10:59:07 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6E882A39D for ; Fri, 5 Nov 2021 10:58:37 +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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id B1632A394 for ; Fri, 5 Nov 2021 10:58:36 +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 8991745BF1; Fri, 5 Nov 2021 10:58:36 +0100 (CET) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Fri, 5 Nov 2021 10:58:34 +0100 Message-Id: <20211105095834.2879703-1-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.522 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 Subject: [pbs-devel] [proxmox-backup] CalendarEvent: add calendar_event_match and fix compute_next_event 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: Fri, 05 Nov 2021 09:59:07 -0000 Signed-off-by: Dietmar Maurer --- proxmox-systemd/src/parse_time.rs | 4 +- proxmox-systemd/src/time.rs | 129 +++++++++++++++++++++++++++++- 2 files changed, 130 insertions(+), 3 deletions(-) diff --git a/proxmox-systemd/src/parse_time.rs b/proxmox-systemd/src/parse_time.rs index ba9449b1..9a2ecd77 100644 --- a/proxmox-systemd/src/parse_time.rs +++ b/proxmox-systemd/src/parse_time.rs @@ -211,7 +211,7 @@ fn parse_date_time_comp_list(start: u32, max: usize) -> impl Fn(&str) -> IResult return Ok((n, vec![DateTimeValue::Repeated(start, repeat)])); } } - return Ok((rest, Vec::new())); + return Ok((rest, vec![DateTimeValue::Range(start, max as u32)])); } separated_nonempty_list(tag(","), parse_date_time_comp(max))(i) @@ -229,7 +229,7 @@ fn parse_time_spec(i: &str) -> IResult<&str, TimeSpec> { if let Some(second) = opt_second { Ok((i, TimeSpec { hour, minute, second })) } else { - Ok((i, TimeSpec { hour, minute, second: vec![DateTimeValue::Single(0)] })) + Ok((i, TimeSpec { hour, minute, second: Vec::new() })) } } diff --git a/proxmox-systemd/src/time.rs b/proxmox-systemd/src/time.rs index b81e970e..acba4763 100644 --- a/proxmox-systemd/src/time.rs +++ b/proxmox-systemd/src/time.rs @@ -234,6 +234,69 @@ pub fn verify_calendar_event(i: &str) -> Result<(), Error> { Ok(()) } +pub fn calendar_event_match( + event: &CalendarEvent, + epoch: i64, + utc: bool, +) -> Result { + + let t = TmEditor::with_epoch(epoch, utc)?; + + if !event.year.is_empty() { + let year: u32 = t.year().try_into()?; + if !DateTimeValue::list_contains(&event.year, year) { + return Ok(false); + } + } + + if !event.month.is_empty() { + let month: u32 = t.month().try_into()?; + if !DateTimeValue::list_contains(&event.month, month) { + return Ok(false); + } + } + + if !event.day.is_empty() { + let day: u32 = t.day().try_into()?; + if !DateTimeValue::list_contains(&event.day, day) { + return Ok(false); + } + } + + let all_days = event.days.is_empty() || event.days.is_all(); + + if !all_days { + let day_num: u32 = t.day_num().try_into()?; + let day = WeekDays::from_bits(1< Result<(), Error> { + + let dtv = DateTimeValue::Repeated(5, 2); + + assert!(!dtv.contains(0)); + assert!(!dtv.contains(1)); + assert!(!dtv.contains(2)); + assert!(!dtv.contains(3)); + assert!(!dtv.contains(4)); + assert!(dtv.contains(5)); + assert!(!dtv.contains(6)); + assert!(dtv.contains(7)); + assert!(!dtv.contains(8)); + + Ok(()) + } + + #[test] + fn test_event_match() -> Result<(), Error> { + + const THURSDAY_00_00: i64 = make_test_time(0, 0, 0); + const SATURDAY_09_00: i64 = make_test_time(2, 9, 0); + const SUNDAY_09_00: i64 = make_test_time(3, 9, 0); + + let event = parse_calendar_event("mon..fri 8..16:*")?; + + const HOUR: i64 = 3600; + + assert!(!calendar_event_match(&event, THURSDAY_00_00, true)?); + assert!(!calendar_event_match(&event, THURSDAY_00_00 + 7*HOUR, true)?); + assert!(calendar_event_match(&event, THURSDAY_00_00 + 8*HOUR, true)?); + assert!(calendar_event_match(&event, THURSDAY_00_00 + 15*HOUR, true)?); + assert!(calendar_event_match(&event, THURSDAY_00_00 + 16*HOUR, true)?); + assert!(!calendar_event_match(&event, THURSDAY_00_00 + 17*HOUR, true)?); + assert!(!calendar_event_match(&event, SATURDAY_09_00, true)?); + assert!(!calendar_event_match(&event, SUNDAY_09_00, true)?); + + Ok(()) + } + #[test] fn test_compute_next_event() -> Result<(), Error> { @@ -405,7 +532,7 @@ mod test { ); } } - Ok(None) => bail!("next {:?} failed to find a timestamp", event), + Ok(None) => bail!("next {} {:?} failed to find a timestamp", v, event), Err(err) => bail!("compute next for '{}' failed - {}", v, err), } -- 2.30.2