public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 01/14] proxmox-time: calendar-events: implement repeated ranges
Date: Tue, 30 Nov 2021 13:11:55 +0100	[thread overview]
Message-ID: <20211130121209.216846-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com>

so that we can have e.g. '7..17/2:00' as timespec

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox-time/src/parse_time.rs | 10 +++++++---
 proxmox-time/src/time.rs       | 27 ++++++++++++++++++++++-----
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/proxmox-time/src/parse_time.rs b/proxmox-time/src/parse_time.rs
index 89d147e..8159a1a 100644
--- a/proxmox-time/src/parse_time.rs
+++ b/proxmox-time/src/parse_time.rs
@@ -191,12 +191,16 @@ fn parse_date_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, DateTimeVa
             if value > end {
                 return Err(parse_error(i, "range start is bigger than end"));
             }
-            return Ok((i, DateTimeValue::Range(value, end)))
+            if let Some(time) = i.strip_prefix('/') {
+                let (time, repeat) = parse_time_comp(max)(time)?;
+                return Ok((time, DateTimeValue::Repeated(value, repeat, Some(end))));
+            }
+            return Ok((i, DateTimeValue::Range(value, end)));
         }
 
         if let Some(time) = i.strip_prefix('/') {
             let (time, repeat) = parse_time_comp(max)(time)?;
-            Ok((time, DateTimeValue::Repeated(value, repeat)))
+            Ok((time, DateTimeValue::Repeated(value, repeat, None)))
         } else {
             Ok((i, DateTimeValue::Single(value)))
         }
@@ -209,7 +213,7 @@ fn parse_date_time_comp_list(start: u32, max: usize) -> impl Fn(&str) -> IResult
             if let Some(time) = rest.strip_prefix('/') {
                 let (n, repeat) = parse_time_comp(max)(time)?;
                 if repeat > 0 {
-                    return Ok((n, vec![DateTimeValue::Repeated(start, repeat)]));
+                    return Ok((n, vec![DateTimeValue::Repeated(start, repeat, None)]));
                 }
             }
             return Ok((rest, Vec::new()));
diff --git a/proxmox-time/src/time.rs b/proxmox-time/src/time.rs
index a220f2c..2dfe425 100644
--- a/proxmox-time/src/time.rs
+++ b/proxmox-time/src/time.rs
@@ -25,7 +25,7 @@ bitflags!{
 pub(crate) enum DateTimeValue {
     Single(u32),
     Range(u32, u32),
-    Repeated(u32, u32),
+    Repeated(u32, u32, Option<u32>),
 }
 
 impl DateTimeValue {
@@ -34,11 +34,16 @@ impl DateTimeValue {
         match self {
             DateTimeValue::Single(v) => *v == value,
             DateTimeValue::Range(start, end) => value >= *start && value <= *end,
-            DateTimeValue::Repeated(start, repetition) => {
+            DateTimeValue::Repeated(start, repetition, opt_end) => {
                 if value >= *start {
                     if *repetition > 0 {
                         let offset = value - start;
-                        offset % repetition == 0
+                        let res = offset % repetition == 0;
+                        if let Some(end) = opt_end {
+                            res && value <= *end
+                        } else {
+                            res
+                        }
                     } else {
                         *start == value
                     }
@@ -78,11 +83,18 @@ impl DateTimeValue {
                         }
                     }
                 }
-                DateTimeValue::Repeated(start, repetition) => {
+                DateTimeValue::Repeated(start, repetition, opt_end) => {
                     if value < *start {
                         set_next(*start);
                     } else if *repetition > 0 {
-                        set_next(start + ((value - start + repetition) / repetition) * repetition);
+                        let n = start + ((value - start + repetition) / repetition) * repetition;
+                        if let Some(end) = opt_end {
+                            if n <= *end {
+                                set_next(n);
+                            }
+                        } else {
+                            set_next(n);
+                        }
                     }
                 }
             }
@@ -455,6 +467,11 @@ mod test {
         test_value("sat", THURSDAY_00_00, THURSDAY_00_00 + 2*DAY)?;
         test_value("sun", THURSDAY_00_00, THURSDAY_00_00 + 3*DAY)?;
 
+        // test repeated ranges
+        test_value("4..10/2:0", THURSDAY_00_00, THURSDAY_00_00 + 4*HOUR)?;
+        test_value("4..10/2:0", THURSDAY_00_00 + 5*HOUR, THURSDAY_00_00 + 6*HOUR)?;
+        test_value("4..10/2:0", THURSDAY_00_00 + 11*HOUR, THURSDAY_00_00 + 1*DAY + 4*HOUR)?;
+
         // test multiple values for a single field
         // and test that the order does not matter
         test_value("5,10:4,8", THURSDAY_00_00, THURSDAY_00_00 + 5*HOUR + 4*MIN)?;
-- 
2.30.2





  reply	other threads:[~2021-11-30 12:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-30 12:11 [pbs-devel] [PATCH proxmox/proxmox-backup 00/14] improve & cleanup proxmox-time Dominik Csapak
2021-11-30 12:11 ` Dominik Csapak [this message]
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 02/14] proxmox-time: calendar-events: make hour optional Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 03/14] proxmox-time: move common parse functions to parse_helpers Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 04/14] proxmox-time: move WeekDays into own file Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 05/14] proxmox-time: split DateTimeValue " Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 06/14] proxmox-time: move parse_daily_duration to daily_duration.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 07/14] proxmox-time: daily_duration.rs: rustfmt Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 08/14] proxmox-time: move CalendarEvent into calendar_events.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 09/14] proxmox-time: move TimeSpan into time_span.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 10/14] proxmox-time: move tests from time.rs to test.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 11/14] proxmox-time: lib.rs: rustfmt Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 12/14] proxmox-time: calendar-events: make compute_next_event a method Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 13/14] proxmox-time: calendar_events: implement FromStr Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 14/14] proxmox-time: time-span: " Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox-backup 1/1] remove use of deprecated functions from proxmox-time Dominik Csapak
2021-12-01  6:26   ` [pbs-devel] applied: " Dietmar Maurer
2021-12-01  6:20 ` [pbs-devel] applied: [PATCH proxmox/proxmox-backup 00/14] improve & cleanup proxmox-time 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=20211130121209.216846-2-d.csapak@proxmox.com \
    --to=d.csapak@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