forked from Proxmox/proxmox
proxmox-time: calendar-events: make compute_next_event a method
and deprecated the standalone function Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
parent
032787a6a3
commit
a96e9fb724
@ -35,22 +35,13 @@ 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 all_days = event.days.is_empty() || event.days.is_all();
|
||||
let all_days = self.days.is_empty() || self.days.is_all();
|
||||
|
||||
let mut t = TmEditor::with_epoch(last, utc)?;
|
||||
|
||||
@ -64,10 +55,10 @@ pub fn compute_next_event(
|
||||
count += 1;
|
||||
}
|
||||
|
||||
if !event.year.is_empty() {
|
||||
if !self.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) {
|
||||
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 {
|
||||
@ -77,10 +68,10 @@ pub fn compute_next_event(
|
||||
}
|
||||
}
|
||||
|
||||
if !event.month.is_empty() {
|
||||
if !self.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) {
|
||||
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
|
||||
@ -90,10 +81,10 @@ pub fn compute_next_event(
|
||||
}
|
||||
}
|
||||
|
||||
if !event.day.is_empty() {
|
||||
if !self.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) {
|
||||
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
|
||||
@ -103,12 +94,13 @@ pub fn compute_next_event(
|
||||
}
|
||||
}
|
||||
|
||||
if !all_days { // match day first
|
||||
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()))
|
||||
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()?)?;
|
||||
@ -121,10 +113,10 @@ pub fn compute_next_event(
|
||||
}
|
||||
|
||||
// this day
|
||||
if !event.hour.is_empty() {
|
||||
if !self.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) {
|
||||
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 {
|
||||
@ -136,10 +128,10 @@ pub fn compute_next_event(
|
||||
}
|
||||
|
||||
// this hour
|
||||
if !event.minute.is_empty() {
|
||||
if !self.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) {
|
||||
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 {
|
||||
@ -151,10 +143,10 @@ pub fn compute_next_event(
|
||||
}
|
||||
|
||||
// this minute
|
||||
if !event.second.is_empty() {
|
||||
if !self.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) {
|
||||
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 {
|
||||
@ -166,8 +158,25 @@ pub fn compute_next_event(
|
||||
}
|
||||
|
||||
let next = t.into_epoch()?;
|
||||
return Ok(Some(next))
|
||||
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]
|
||||
|
@ -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 {}",
|
||||
|
Loading…
Reference in New Issue
Block a user