From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
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 50DBA8284E
 for <pbs-devel@lists.proxmox.com>; Tue, 30 Nov 2021 13:12:43 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 4BAEE26944
 for <pbs-devel@lists.proxmox.com>; Tue, 30 Nov 2021 13:12:13 +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 6B2B2268E5
 for <pbs-devel@lists.proxmox.com>; Tue, 30 Nov 2021 13:12:10 +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 4339245438
 for <pbs-devel@lists.proxmox.com>; Tue, 30 Nov 2021 13:12:10 +0100 (CET)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Tue, 30 Nov 2021 13:11:55 +0100
Message-Id: <20211130121209.216846-2-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com>
References: <20211130121209.216846-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.185 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. [time.rs]
Subject: [pbs-devel] [PATCH proxmox 01/14] proxmox-time: calendar-events:
 implement repeated ranges
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 30 Nov 2021 12:12:43 -0000

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