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 67DCD7D4E2 for ; Tue, 9 Nov 2021 07:53:04 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0FB8A2DFC6 for ; Tue, 9 Nov 2021 07:53:04 +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 7F1EE2DF62 for ; Tue, 9 Nov 2021 07:53:01 +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 6546C46063; Tue, 9 Nov 2021 07:52:56 +0100 (CET) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Tue, 9 Nov 2021 07:52:46 +0100 Message-Id: <20211109065253.980304-10-dietmar@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211109065253.980304-1-dietmar@proxmox.com> References: <20211109065253.980304-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.515 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] [PATCH proxmox-backup 5/9] DailyDuration: implement time_match() 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, 09 Nov 2021 06:53:04 -0000 --- proxmox-systemd/src/daily_duration.rs | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/proxmox-systemd/src/daily_duration.rs b/proxmox-systemd/src/daily_duration.rs index bed4eb47..25157b25 100644 --- a/proxmox-systemd/src/daily_duration.rs +++ b/proxmox-systemd/src/daily_duration.rs @@ -1,4 +1,9 @@ use std::cmp::{Ordering, PartialOrd}; +use std::convert::TryInto; + +use anyhow::Error; + +use proxmox_time::TmEditor; use super::time::{WeekDays}; @@ -29,6 +34,30 @@ pub struct DailyDuration { pub end: HmTime, } +impl DailyDuration { + + // Test it time is within this frame + pub fn time_match(&self, epoch: i64, utc: bool) -> Result { + + let t = TmEditor::with_epoch(epoch, utc)?; + + let all_days = self.days.is_empty() || self.days.is_all(); + + if !all_days { // match day first + let day_num: u32 = t.day_num().try_into()?; + let day = WeekDays::from_bits(1<= self.start && ctime < self.end) + } +} + #[cfg(test)] mod test { @@ -68,6 +97,10 @@ mod test { Ok(()) } + const fn make_test_time(mday: i32, hour: i32, min: i32) -> i64 { + (mday*3600*24 + hour*3600 + min*60) as i64 + } + #[test] fn test_daily_duration_parser() -> Result<(), Error> { @@ -89,4 +122,31 @@ mod test { Ok(()) } + + #[test] + fn test_time_match() -> Result<(), Error> { + const THURSDAY_80_00: i64 = make_test_time(0, 8, 0); + const THURSDAY_12_00: i64 = make_test_time(0, 12, 0); + const DAY: i64 = 3600*24; + + let duration = parse_daily_duration("thu..fri 8:05-12")?; + + assert!(!duration.time_match(THURSDAY_80_00, true)?); + assert!(!duration.time_match(THURSDAY_80_00 + DAY, true)?); + assert!(!duration.time_match(THURSDAY_80_00 + 2*DAY, true)?); + + assert!(duration.time_match(THURSDAY_80_00 + 5*60, true)?); + assert!(duration.time_match(THURSDAY_80_00 + 5*60 + DAY, true)?); + assert!(!duration.time_match(THURSDAY_80_00 + 5*60 + 2*DAY, true)?); + + assert!(duration.time_match(THURSDAY_12_00 - 1, true)?); + assert!(duration.time_match(THURSDAY_12_00 - 1 + DAY, true)?); + assert!(!duration.time_match(THURSDAY_12_00 - 1 + 2*DAY, true)?); + + assert!(!duration.time_match(THURSDAY_12_00, true)?); + assert!(!duration.time_match(THURSDAY_12_00 + DAY, true)?); + assert!(!duration.time_match(THURSDAY_12_00 + 2*DAY, true)?); + + Ok(()) + } } -- 2.30.2