[pbs-devel] [PATCH proxmox 1/1] proxmox-time: calendar-events: parse 'UTC' timezone into calendarevent
Dominik Csapak
d.csapak at proxmox.com
Wed Dec 1 09:45:06 CET 2021
like we do in pve. this breaks the (newly added) compute_next_event
method of CalendarEvent, but we can keep compatibilty with the
deprecated function
adapt the tests so that they add a ' UTC' by default
(so they can run reliably on any machine)
Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
proxmox-time/src/calendar_event.rs | 25 ++++++++++++++++++++++---
proxmox-time/src/test.rs | 8 ++++----
2 files changed, 26 insertions(+), 7 deletions(-)
diff --git a/proxmox-time/src/calendar_event.rs b/proxmox-time/src/calendar_event.rs
index d1d74a2..efde81a 100644
--- a/proxmox-time/src/calendar_event.rs
+++ b/proxmox-time/src/calendar_event.rs
@@ -19,6 +19,8 @@ use crate::{parse_weekdays_range, TmEditor, WeekDays};
/// specification, but are not guaranteed to be 100% compatible.
#[derive(Default, Clone, Debug)]
pub struct CalendarEvent {
+ /// if true, the event is calculated in utc and the local timezone otherwise
+ utc: bool,
/// the days in a week this event should trigger
pub(crate) days: WeekDays,
/// the second(s) this event should trigger
@@ -38,12 +40,12 @@ pub struct CalendarEvent {
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> {
+ pub fn compute_next_event(&self, last: i64) -> Result<Option<i64>, Error> {
let last = last + 1; // at least one second later
let all_days = self.days.is_empty() || self.days.is_all();
- let mut t = TmEditor::with_epoch(last, utc)?;
+ let mut t = TmEditor::with_epoch(last, self.utc)?;
let mut count = 0;
@@ -184,7 +186,12 @@ pub fn compute_next_event(
last: i64,
utc: bool,
) -> Result<Option<i64>, Error> {
- event.compute_next_event(last, utc)
+ if event.utc != utc {
+ let mut event = event.clone();
+ event.utc = utc;
+ return event.compute_next_event(last);
+ }
+ event.compute_next_event(last)
}
/// Parse a [CalendarEvent]
@@ -199,6 +206,10 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
let mut has_datespec = false;
let mut event = CalendarEvent::default();
+ if let Some(n) = i.strip_suffix("UTC") {
+ event.utc = true;
+ i = n.trim_end_matches(' ');
+ }
if i.starts_with(|c: char| char::is_ascii_alphabetic(&c)) {
match i {
@@ -206,6 +217,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
return Ok((
"",
CalendarEvent {
+ utc: event.utc,
second: vec![DateTimeValue::Single(0)],
..Default::default()
},
@@ -215,6 +227,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
return Ok((
"",
CalendarEvent {
+ utc: event.utc,
minute: vec![DateTimeValue::Single(0)],
second: vec![DateTimeValue::Single(0)],
..Default::default()
@@ -225,6 +238,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
return Ok((
"",
CalendarEvent {
+ utc: event.utc,
hour: vec![DateTimeValue::Single(0)],
minute: vec![DateTimeValue::Single(0)],
second: vec![DateTimeValue::Single(0)],
@@ -236,6 +250,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
return Ok((
"",
CalendarEvent {
+ utc: event.utc,
hour: vec![DateTimeValue::Single(0)],
minute: vec![DateTimeValue::Single(0)],
second: vec![DateTimeValue::Single(0)],
@@ -248,6 +263,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
return Ok((
"",
CalendarEvent {
+ utc: event.utc,
hour: vec![DateTimeValue::Single(0)],
minute: vec![DateTimeValue::Single(0)],
second: vec![DateTimeValue::Single(0)],
@@ -260,6 +276,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
return Ok((
"",
CalendarEvent {
+ utc: event.utc,
hour: vec![DateTimeValue::Single(0)],
minute: vec![DateTimeValue::Single(0)],
second: vec![DateTimeValue::Single(0)],
@@ -273,6 +290,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
return Ok((
"",
CalendarEvent {
+ utc: event.utc,
hour: vec![DateTimeValue::Single(0)],
minute: vec![DateTimeValue::Single(0)],
second: vec![DateTimeValue::Single(0)],
@@ -291,6 +309,7 @@ fn parse_calendar_event_incomplete(mut i: &str) -> IResult<&str, CalendarEvent>
return Ok((
"",
CalendarEvent {
+ utc: event.utc,
hour: vec![DateTimeValue::Single(0)],
minute: vec![DateTimeValue::Single(0)],
second: vec![DateTimeValue::Single(0)],
diff --git a/proxmox-time/src/test.rs b/proxmox-time/src/test.rs
index ec63a37..c5c23b4 100644
--- a/proxmox-time/src/test.rs
+++ b/proxmox-time/src/test.rs
@@ -18,12 +18,12 @@ const fn make_test_time(mday: i32, hour: i32, min: i32) -> i64 {
#[test]
fn test_compute_next_event() -> Result<(), Error> {
let test_value = |v: &'static str, last: i64, expect: i64| -> Result<i64, Error> {
- let event: CalendarEvent = match v.parse() {
+ let event: CalendarEvent = match format!("{} UTC", v).parse() {
Ok(event) => event,
Err(err) => bail!("parsing '{}' failed - {}", v, err),
};
- match event.compute_next_event(last, true) {
+ match event.compute_next_event(last) {
Ok(Some(next)) => {
if next == expect {
println!("next {:?} => {}", event, next);
@@ -44,12 +44,12 @@ fn test_compute_next_event() -> Result<(), Error> {
};
let test_never = |v: &'static str, last: i64| -> Result<(), Error> {
- let event: CalendarEvent = match v.parse() {
+ let event: CalendarEvent = match format!("{} UTC", v).parse() {
Ok(event) => event,
Err(err) => bail!("parsing '{}' failed - {}", v, err),
};
- match event.compute_next_event(last, true)? {
+ match event.compute_next_event(last)? {
None => Ok(()),
Some(next) => bail!(
"compute next for '{}' succeeded, but expected fail - result {}",
--
2.30.2
More information about the pbs-devel
mailing list