mirror of
https://git.proxmox.com/git/proxmox
synced 2025-05-01 17:07:32 +00:00
proxmox-time: split DateTimeValue into own file
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
parent
07cc21bd5a
commit
f61ee1372f
82
proxmox-time/src/date_time_value.rs
Normal file
82
proxmox-time/src/date_time_value.rs
Normal file
@ -0,0 +1,82 @@
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) enum DateTimeValue {
|
||||
Single(u32),
|
||||
Range(u32, u32),
|
||||
Repeated(u32, u32, Option<u32>),
|
||||
}
|
||||
|
||||
impl DateTimeValue {
|
||||
// Test if the entry contains the value
|
||||
pub fn contains(&self, value: u32) -> bool {
|
||||
match self {
|
||||
DateTimeValue::Single(v) => *v == value,
|
||||
DateTimeValue::Range(start, end) => value >= *start && value <= *end,
|
||||
DateTimeValue::Repeated(start, repetition, opt_end) => {
|
||||
if value >= *start {
|
||||
if *repetition > 0 {
|
||||
let offset = value - start;
|
||||
let res = offset % repetition == 0;
|
||||
if let Some(end) = opt_end {
|
||||
res && value <= *end
|
||||
} else {
|
||||
res
|
||||
}
|
||||
} else {
|
||||
*start == value
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list_contains(list: &[DateTimeValue], value: u32) -> bool {
|
||||
list.iter().any(|spec| spec.contains(value))
|
||||
}
|
||||
|
||||
// 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| {
|
||||
if let Some(n) = next {
|
||||
if v < n { next = Some(v); }
|
||||
} else {
|
||||
next = Some(v);
|
||||
}
|
||||
};
|
||||
for spec in list {
|
||||
match spec {
|
||||
DateTimeValue::Single(v) => {
|
||||
if *v > value { set_next(*v); }
|
||||
}
|
||||
DateTimeValue::Range(start, end) => {
|
||||
if value < *start {
|
||||
set_next(*start);
|
||||
} else {
|
||||
let n = value + 1;
|
||||
if n >= *start && n <= *end {
|
||||
set_next(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
DateTimeValue::Repeated(start, repetition, opt_end) => {
|
||||
if value < *start {
|
||||
set_next(*start);
|
||||
} else if *repetition > 0 {
|
||||
let n = start + ((value - start + repetition) / repetition) * repetition;
|
||||
if let Some(end) = opt_end {
|
||||
if n <= *end {
|
||||
set_next(n);
|
||||
}
|
||||
} else {
|
||||
set_next(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
next
|
||||
}
|
||||
}
|
@ -15,6 +15,8 @@ pub use time::*;
|
||||
|
||||
pub(crate) mod parse_helpers;
|
||||
|
||||
pub(crate) mod date_time_value;
|
||||
|
||||
mod week_days;
|
||||
pub use week_days::*;
|
||||
|
||||
|
@ -17,6 +17,7 @@ use nom::{
|
||||
|
||||
use crate::parse_helpers::{parse_complete_line, parse_error, parse_hm_time, parse_time_comp, parse_u64, IResult};
|
||||
use crate::{parse_weekdays_range, WeekDays};
|
||||
use crate::date_time_value::DateTimeValue;
|
||||
|
||||
lazy_static! {
|
||||
static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = {
|
||||
|
@ -2,94 +2,12 @@ use std::convert::TryInto;
|
||||
|
||||
use anyhow::Error;
|
||||
|
||||
use crate::date_time_value::DateTimeValue;
|
||||
use crate::TmEditor;
|
||||
use crate::WeekDays;
|
||||
|
||||
use crate::{parse_calendar_event, parse_time_span};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub(crate) enum DateTimeValue {
|
||||
Single(u32),
|
||||
Range(u32, u32),
|
||||
Repeated(u32, u32, Option<u32>),
|
||||
}
|
||||
|
||||
impl DateTimeValue {
|
||||
// Test if the entry contains the value
|
||||
pub fn contains(&self, value: u32) -> bool {
|
||||
match self {
|
||||
DateTimeValue::Single(v) => *v == value,
|
||||
DateTimeValue::Range(start, end) => value >= *start && value <= *end,
|
||||
DateTimeValue::Repeated(start, repetition, opt_end) => {
|
||||
if value >= *start {
|
||||
if *repetition > 0 {
|
||||
let offset = value - start;
|
||||
let res = offset % repetition == 0;
|
||||
if let Some(end) = opt_end {
|
||||
res && value <= *end
|
||||
} else {
|
||||
res
|
||||
}
|
||||
} else {
|
||||
*start == value
|
||||
}
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list_contains(list: &[DateTimeValue], value: u32) -> bool {
|
||||
list.iter().any(|spec| spec.contains(value))
|
||||
}
|
||||
|
||||
// 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| {
|
||||
if let Some(n) = next {
|
||||
if v < n { next = Some(v); }
|
||||
} else {
|
||||
next = Some(v);
|
||||
}
|
||||
};
|
||||
for spec in list {
|
||||
match spec {
|
||||
DateTimeValue::Single(v) => {
|
||||
if *v > value { set_next(*v); }
|
||||
}
|
||||
DateTimeValue::Range(start, end) => {
|
||||
if value < *start {
|
||||
set_next(*start);
|
||||
} else {
|
||||
let n = value + 1;
|
||||
if n >= *start && n <= *end {
|
||||
set_next(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
DateTimeValue::Repeated(start, repetition, opt_end) => {
|
||||
if value < *start {
|
||||
set_next(*start);
|
||||
} else if *repetition > 0 {
|
||||
let n = start + ((value - start + repetition) / repetition) * repetition;
|
||||
if let Some(end) = opt_end {
|
||||
if n <= *end {
|
||||
set_next(n);
|
||||
}
|
||||
} else {
|
||||
set_next(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
next
|
||||
}
|
||||
}
|
||||
|
||||
/// Calendar events may be used to refer to one or more points in time in a
|
||||
/// single expression. They are designed after the systemd.time Calendar Events
|
||||
/// specification, but are not guaranteed to be 100% compatible.
|
||||
|
Loading…
Reference in New Issue
Block a user