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-backup 07/10] tools/systemd/time: use i32 for DateTimeValues instead of u32
Date: Thu,  3 Sep 2020 13:39:57 +0200	[thread overview]
Message-ID: <20200903114000.6932-8-d.csapak@proxmox.com> (raw)
In-Reply-To: <20200903114000.6932-1-d.csapak@proxmox.com>

for most of the fields it does not matter
(mon,day,hour,min,sec)

but for years it matters, since negative years in a tm struct are valid
so to be able to handle that, convert those to i32

on parsing, we never allow negative numbers anyway

also, this way we can drop the manual casts (which would have been very
wrong if the underlying tm struct would have contained negative values)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/tools/systemd/parse_time.rs |  2 +-
 src/tools/systemd/time.rs       | 22 +++++++++++-----------
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/tools/systemd/parse_time.rs b/src/tools/systemd/parse_time.rs
index 051c4968..810a9883 100644
--- a/src/tools/systemd/parse_time.rs
+++ b/src/tools/systemd/parse_time.rs
@@ -86,7 +86,7 @@ lazy_static! {
         map
     };
 }
-fn parse_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, u32> {
+fn parse_time_comp(max: usize) -> impl Fn(&str) -> IResult<&str, i32> {
     move |i: &str| {
         let (i, v) = map_res(recognize(digit1), str::parse)(i)?;
         if (v as usize) >= max {
diff --git a/src/tools/systemd/time.rs b/src/tools/systemd/time.rs
index 83ae19e8..6ce881e7 100644
--- a/src/tools/systemd/time.rs
+++ b/src/tools/systemd/time.rs
@@ -19,14 +19,14 @@ bitflags!{
 
 #[derive(Debug)]
 pub enum DateTimeValue {
-    Single(u32),
-    Range(u32, u32),
-    Repeated(u32, u32),
+    Single(i32),
+    Range(i32, i32),
+    Repeated(i32, i32),
 }
 
 impl DateTimeValue {
     // Test if the entry contains the value
-    pub fn contains(&self, value: u32) -> bool {
+    pub fn contains(&self, value: i32) -> bool {
         match self {
             DateTimeValue::Single(v) => *v == value,
             DateTimeValue::Range(start, end) => value >= *start && value <= *end,
@@ -45,14 +45,14 @@ impl DateTimeValue {
         }
     }
 
-    pub fn list_contains(list: &[DateTimeValue], value: u32) -> bool {
+    pub fn list_contains(list: &[DateTimeValue], value: i32) -> bool {
         list.iter().find(|spec| spec.contains(value)).is_some()
     }
 
     // Find an return an entry greater than value
-    pub fn find_next(list: &[DateTimeValue], value: u32) -> Option<u32> {
-        let mut next: Option<u32> = None;
-        let mut set_next = |v: u32| {
+    pub fn find_next(list: &[DateTimeValue], value: i32) -> Option<i32> {
+        let mut next: Option<i32> = None;
+        let mut set_next = |v: i32| {
             if let Some(n) = next {
                 if v < n { next = Some(v); }
             } else {
@@ -194,7 +194,7 @@ pub fn compute_next_event(
 
         // this day
         if !event.hour.is_empty() && t.changes.contains(TMChanges::HOUR) {
-            let hour = t.hour() as u32;
+            let hour = t.hour();
             if DateTimeValue::list_contains(&event.hour, hour) {
                 t.changes.remove(TMChanges::HOUR);
             } else {
@@ -211,7 +211,7 @@ pub fn compute_next_event(
 
         // this hour
         if !event.minute.is_empty()  && t.changes.contains(TMChanges::MIN) {
-            let minute = t.min() as u32;
+            let minute = t.min();
             if DateTimeValue::list_contains(&event.minute, minute) {
                 t.changes.remove(TMChanges::MIN);
             } else {
@@ -228,7 +228,7 @@ pub fn compute_next_event(
 
         // this minute
         if !event.second.is_empty() && t.changes.contains(TMChanges::SEC) {
-            let second = t.sec() as u32;
+            let second = t.sec();
             if DateTimeValue::list_contains(&event.second, second) {
                 t.changes.remove(TMChanges::SEC);
             } else {
-- 
2.20.1





  parent reply	other threads:[~2020-09-03 11:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-03 11:39 [pbs-devel] [PATCH proxmox-backup 00/10] implement dates for calendarevents Dominik Csapak
2020-09-03 11:39 ` [pbs-devel] [PATCH proxmox-backup 01/10] tools/systemd/time: let libc normalize time for us Dominik Csapak
2020-09-04  4:52   ` Dietmar Maurer
2020-09-04  4:57   ` Dietmar Maurer
2020-09-03 11:39 ` [pbs-devel] [PATCH proxmox-backup 02/10] tools/systemd/time: move continue out of the if/else Dominik Csapak
2020-09-03 11:39 ` [pbs-devel] [PATCH proxmox-backup 03/10] tools/systemd/time: convert the resulting timestamp into an option Dominik Csapak
2020-09-03 11:39 ` [pbs-devel] [PATCH proxmox-backup 04/10] tools/systemd/tm_editor: remove reset_time from add_days and document it Dominik Csapak
2020-09-04  5:12   ` Dietmar Maurer
2020-09-03 11:39 ` [pbs-devel] [PATCH proxmox-backup 05/10] tools/systemd/parse_time: error out on invalid ranges Dominik Csapak
2020-09-03 11:39 ` [pbs-devel] [PATCH proxmox-backup 06/10] tools/systemd/time: fix selection for multiple options Dominik Csapak
2020-09-03 11:39 ` Dominik Csapak [this message]
2020-09-04  5:31   ` [pbs-devel] [PATCH proxmox-backup 07/10] tools/systemd/time: use i32 for DateTimeValues instead of u32 Dietmar Maurer
2020-09-04  5:39   ` Dietmar Maurer
2020-09-03 11:39 ` [pbs-devel] [PATCH proxmox-backup 08/10] tools/systemd/tm_editor: remove conversion of the year Dominik Csapak
2020-09-03 11:39 ` [pbs-devel] [PATCH proxmox-backup 09/10] tools/systemd/tm_editor: add setter/getter for months/years/days Dominik Csapak
2020-09-03 11:40 ` [pbs-devel] [PATCH proxmox-backup 10/10] tools/systemd/time: enable dates for calendarevents Dominik Csapak

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=20200903114000.6932-8-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