public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 5/9] DailyDuration: implement time_match()
Date: Tue,  9 Nov 2021 07:52:46 +0100	[thread overview]
Message-ID: <20211109065253.980304-10-dietmar@proxmox.com> (raw)
In-Reply-To: <20211109065253.980304-1-dietmar@proxmox.com>

---
 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<bool, Error> {
+
+        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<<day_num).unwrap();
+            if !self.days.contains(day) { return Ok(false); }
+        }
+
+        let hour = t.hour().try_into()?;
+        let minute = t.min().try_into()?;
+
+        let ctime = HmTime { hour, minute };
+
+        Ok(ctime >= 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





  parent reply	other threads:[~2021-11-09  6:53 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-09  6:52 [pbs-devel] [PATCH proxmox/proxmox-backup] Rate Limiter Implementation Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox 1/7] Implement a rate limiting stream (AsyncRead, AsyncWrite) Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox-backup 1/9] pbs-client: add option to use the new RateLimiter Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox 2/7] RateLimitedStream: implement poll_write_vectored Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox-backup 2/9] proxmox-backup-client: add rate/burst parameter to backup CLI Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox 3/7] HttpsConnector: use RateLimitedStream Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox-backup 3/9] implement Servive for RateLimitedStream Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox-backup 4/9] New DailyDuration type with nom parser Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox 4/7] RateLimitedStream: allow periodic limiter updates Dietmar Maurer
2021-11-09  6:52 ` Dietmar Maurer [this message]
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox 5/7] RateLimiter: avoid panic in time computations Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox-backup 6/9] Add traffic control configuration config with API Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox 6/7] RateLimitedStream: implement peer_addr Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox 7/7] RateLimiter: add update_rate method Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox-backup 7/9] traffic_control: use Memcom to track. config versions Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox-backup 8/9] implement a traffic control cache for fast rate control limiter lockups Dietmar Maurer
2021-11-09  6:52 ` [pbs-devel] [PATCH proxmox-backup 9/9] proxmox-backup-proxy: implement traffic control Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211109065253.980304-10-dietmar@proxmox.com \
    --to=dietmar@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal