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 12/14] proxmox-time: calendar-events: make compute_next_event a method
Date: Tue, 30 Nov 2021 13:12:06 +0100	[thread overview]
Message-ID: <20211130121209.216846-13-d.csapak@proxmox.com> (raw)
In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com>

and deprecated the standalone function

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox-time/src/calendar_event.rs | 221 +++++++++++++++--------------
 proxmox-time/src/test.rs           |   4 +-
 2 files changed, 117 insertions(+), 108 deletions(-)

diff --git a/proxmox-time/src/calendar_event.rs b/proxmox-time/src/calendar_event.rs
index 1c21a84..a839f9d 100644
--- a/proxmox-time/src/calendar_event.rs
+++ b/proxmox-time/src/calendar_event.rs
@@ -35,141 +35,150 @@ pub struct CalendarEvent {
     pub(crate) year: Vec<DateTimeValue>,
 }
 
-/// Verify the format of the [CalendarEvent]
-pub fn verify_calendar_event(i: &str) -> Result<(), Error> {
-    parse_calendar_event(i)?;
-    Ok(())
-}
-
-/// Compute the next event
-pub fn compute_next_event(
-    event: &CalendarEvent,
-    last: i64,
-    utc: bool,
-) -> Result<Option<i64>, Error> {
+impl CalendarEvent {
+    /// Computes the next timestamp after `last`. If `utc` is false, the local
+    /// timezone will be used for the calculation.
+    pub fn compute_next_event(&self, last: i64, utc: bool) -> Result<Option<i64>, Error> {
+        let last = last + 1; // at least one second later
 
-    let last = last + 1; // at least one second later
+        let all_days = self.days.is_empty() || self.days.is_all();
 
-    let all_days = event.days.is_empty() || event.days.is_all();
+        let mut t = TmEditor::with_epoch(last, utc)?;
 
-    let mut t = TmEditor::with_epoch(last, utc)?;
+        let mut count = 0;
 
-    let mut count = 0;
-
-    loop {
-        // cancel after 1000 loops
-        if count > 1000 {
-            return Ok(None);
-        } else {
-            count += 1;
-        }
+        loop {
+            // cancel after 1000 loops
+            if count > 1000 {
+                return Ok(None);
+            } else {
+                count += 1;
+            }
 
-        if !event.year.is_empty() {
-            let year: u32 = t.year().try_into()?;
-            if !DateTimeValue::list_contains(&event.year, year) {
-                if let Some(n) = DateTimeValue::find_next(&event.year, year) {
-                    t.add_years((n - year).try_into()?)?;
-                    continue;
-                } else {
-                    // if we have no valid year, we cannot find a correct timestamp
-                    return Ok(None);
+            if !self.year.is_empty() {
+                let year: u32 = t.year().try_into()?;
+                if !DateTimeValue::list_contains(&self.year, year) {
+                    if let Some(n) = DateTimeValue::find_next(&self.year, year) {
+                        t.add_years((n - year).try_into()?)?;
+                        continue;
+                    } else {
+                        // if we have no valid year, we cannot find a correct timestamp
+                        return Ok(None);
+                    }
                 }
             }
-        }
 
-        if !event.month.is_empty() {
-            let month: u32 = t.month().try_into()?;
-            if !DateTimeValue::list_contains(&event.month, month) {
-                if let Some(n) = DateTimeValue::find_next(&event.month, month) {
-                    t.add_months((n - month).try_into()?)?;
-                } else {
-                    // if we could not find valid month, retry next year
-                    t.add_years(1)?;
+            if !self.month.is_empty() {
+                let month: u32 = t.month().try_into()?;
+                if !DateTimeValue::list_contains(&self.month, month) {
+                    if let Some(n) = DateTimeValue::find_next(&self.month, month) {
+                        t.add_months((n - month).try_into()?)?;
+                    } else {
+                        // if we could not find valid month, retry next year
+                        t.add_years(1)?;
+                    }
+                    continue;
                 }
-                continue;
             }
-        }
 
-        if !event.day.is_empty() {
-            let day: u32 = t.day().try_into()?;
-            if !DateTimeValue::list_contains(&event.day, day) {
-                if let Some(n) = DateTimeValue::find_next(&event.day, day) {
-                    t.add_days((n - day).try_into()?)?;
-                } else {
-                    // if we could not find valid mday, retry next month
-                    t.add_months(1)?;
+            if !self.day.is_empty() {
+                let day: u32 = t.day().try_into()?;
+                if !DateTimeValue::list_contains(&self.day, day) {
+                    if let Some(n) = DateTimeValue::find_next(&self.day, day) {
+                        t.add_days((n - day).try_into()?)?;
+                    } else {
+                        // if we could not find valid mday, retry next month
+                        t.add_months(1)?;
+                    }
+                    continue;
                 }
-                continue;
             }
-        }
 
-        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 !event.days.contains(day) {
-                if let Some(n) = ((day_num+1)..7)
-                    .find(|d| event.days.contains(WeekDays::from_bits(1<<d).unwrap()))
-                {
-                    // try next day
-                    t.add_days((n - day_num).try_into()?)?;
-                } else {
-                    // try next week
-                    t.add_days((7 - day_num).try_into()?)?;
+            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) {
+                    if let Some(n) = ((day_num + 1)..7)
+                        .find(|d| self.days.contains(WeekDays::from_bits(1 << d).unwrap()))
+                    {
+                        // try next day
+                        t.add_days((n - day_num).try_into()?)?;
+                    } else {
+                        // try next week
+                        t.add_days((7 - day_num).try_into()?)?;
+                    }
+                    continue;
                 }
-                continue;
             }
-        }
 
-        // this day
-        if !event.hour.is_empty() {
-            let hour = t.hour().try_into()?;
-            if !DateTimeValue::list_contains(&event.hour, hour) {
-                if let Some(n) = DateTimeValue::find_next(&event.hour, hour) {
-                    // test next hour
-                    t.set_time(n.try_into()?, 0, 0)?;
-                } else {
-                    // test next day
-                    t.add_days(1)?;
+            // this day
+            if !self.hour.is_empty() {
+                let hour = t.hour().try_into()?;
+                if !DateTimeValue::list_contains(&self.hour, hour) {
+                    if let Some(n) = DateTimeValue::find_next(&self.hour, hour) {
+                        // test next hour
+                        t.set_time(n.try_into()?, 0, 0)?;
+                    } else {
+                        // test next day
+                        t.add_days(1)?;
+                    }
+                    continue;
                 }
-                continue;
             }
-        }
 
-        // this hour
-        if !event.minute.is_empty() {
-            let minute = t.min().try_into()?;
-            if !DateTimeValue::list_contains(&event.minute, minute) {
-                if let Some(n) = DateTimeValue::find_next(&event.minute, minute) {
-                    // test next minute
-                    t.set_min_sec(n.try_into()?, 0)?;
-                } else {
-                    // test next hour
-                    t.set_time(t.hour() + 1, 0, 0)?;
+            // this hour
+            if !self.minute.is_empty() {
+                let minute = t.min().try_into()?;
+                if !DateTimeValue::list_contains(&self.minute, minute) {
+                    if let Some(n) = DateTimeValue::find_next(&self.minute, minute) {
+                        // test next minute
+                        t.set_min_sec(n.try_into()?, 0)?;
+                    } else {
+                        // test next hour
+                        t.set_time(t.hour() + 1, 0, 0)?;
+                    }
+                    continue;
                 }
-                continue;
             }
-        }
 
-        // this minute
-        if !event.second.is_empty() {
-            let second = t.sec().try_into()?;
-            if !DateTimeValue::list_contains(&event.second, second) {
-                if let Some(n) = DateTimeValue::find_next(&event.second, second) {
-                    // test next second
-                    t.set_sec(n.try_into()?)?;
-                } else {
-                    // test next min
-                    t.set_min_sec(t.min() + 1, 0)?;
+            // this minute
+            if !self.second.is_empty() {
+                let second = t.sec().try_into()?;
+                if !DateTimeValue::list_contains(&self.second, second) {
+                    if let Some(n) = DateTimeValue::find_next(&self.second, second) {
+                        // test next second
+                        t.set_sec(n.try_into()?)?;
+                    } else {
+                        // test next min
+                        t.set_min_sec(t.min() + 1, 0)?;
+                    }
+                    continue;
                 }
-                continue;
             }
-        }
 
-        let next = t.into_epoch()?;
-        return Ok(Some(next))
+            let next = t.into_epoch()?;
+            return Ok(Some(next));
+        }
     }
 }
 
+/// Verify the format of the [CalendarEvent]
+pub fn verify_calendar_event(i: &str) -> Result<(), Error> {
+    parse_calendar_event(i)?;
+    Ok(())
+}
+
+/// Compute the next event. Use [CalendarEvent::compute_next_event] instead.
+#[deprecated="use method 'compute_next_event' of CalendarEvent instead"]
+pub fn compute_next_event(
+    event: &CalendarEvent,
+    last: i64,
+    utc: bool,
+) -> Result<Option<i64>, Error> {
+    event.compute_next_event(last, utc)
+}
+
 /// Parse a [CalendarEvent]
 pub fn parse_calendar_event(i: &str) -> Result<CalendarEvent, Error> {
     parse_complete_line("calendar event", i, parse_calendar_event_incomplete)
diff --git a/proxmox-time/src/test.rs b/proxmox-time/src/test.rs
index 3007156..8c5e1cf 100644
--- a/proxmox-time/src/test.rs
+++ b/proxmox-time/src/test.rs
@@ -23,7 +23,7 @@ fn test_compute_next_event() -> Result<(), Error> {
             Err(err) => bail!("parsing '{}' failed - {}", v, err),
         };
 
-        match compute_next_event(&event, last, true) {
+        match event.compute_next_event(last, true) {
             Ok(Some(next)) => {
                 if next == expect {
                     println!("next {:?} => {}", event, next);
@@ -49,7 +49,7 @@ fn test_compute_next_event() -> Result<(), Error> {
             Err(err) => bail!("parsing '{}' failed - {}", v, err),
         };
 
-        match compute_next_event(&event, last, true)? {
+        match event.compute_next_event(last, true)? {
             None => Ok(()),
             Some(next) => bail!(
                 "compute next for '{}' succeeded, but expected fail - result {}",
-- 
2.30.2





  parent reply	other threads:[~2021-11-30 12:13 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 ` [pbs-devel] [PATCH proxmox 01/14] proxmox-time: calendar-events: implement repeated ranges Dominik Csapak
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 ` Dominik Csapak [this message]
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-13-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