CalendarEvent - compute_next_value: use change tracking to avoid repeated testing

This commit is contained in:
Dietmar Maurer 2020-05-16 10:32:27 +02:00
parent 50ce1f987d
commit 2d08c97ae2
2 changed files with 17 additions and 9 deletions

View File

@ -157,10 +157,12 @@ pub fn compute_next_event(
count += 1; count += 1;
} }
if !all_days { // match day first if !all_days && t.changes.contains(TMChanges::WDAY) { // match day first
let day_num = t.day_num(); let day_num = t.day_num();
let day = WeekDays::from_bits(1<<day_num).unwrap(); let day = WeekDays::from_bits(1<<day_num).unwrap();
if !event.days.contains(day) { if event.days.contains(day) {
t.changes.remove(TMChanges::WDAY);
} else {
if let Some(n) = (day_num+1..6) if let Some(n) = (day_num+1..6)
.map(|d| WeekDays::from_bits(1<<d).unwrap()) .map(|d| WeekDays::from_bits(1<<d).unwrap())
.find(|d| event.days.contains(*d)) .find(|d| event.days.contains(*d))
@ -177,9 +179,11 @@ pub fn compute_next_event(
} }
// this day // this day
if !event.hour.is_empty() { if !event.hour.is_empty() && t.changes.contains(TMChanges::HOUR) {
let hour = t.hour() as u32; let hour = t.hour() as u32;
if !DateTimeValue::list_contains(&event.hour, hour) { if DateTimeValue::list_contains(&event.hour, hour) {
t.changes.remove(TMChanges::HOUR);
} else {
if let Some(n) = DateTimeValue::find_next(&event.hour, hour) { if let Some(n) = DateTimeValue::find_next(&event.hour, hour) {
// test next hour // test next hour
t.set_time(n as libc::c_int, 0, 0); t.set_time(n as libc::c_int, 0, 0);
@ -193,9 +197,11 @@ pub fn compute_next_event(
} }
// this hour // this hour
if !event.minute.is_empty() { if !event.minute.is_empty() && t.changes.contains(TMChanges::MIN) {
let minute = t.min() as u32; let minute = t.min() as u32;
if !DateTimeValue::list_contains(&event.minute, minute) { if DateTimeValue::list_contains(&event.minute, minute) {
t.changes.remove(TMChanges::MIN);
} else {
if let Some(n) = DateTimeValue::find_next(&event.minute, minute) { if let Some(n) = DateTimeValue::find_next(&event.minute, minute) {
// test next minute // test next minute
t.set_min_sec(n as libc::c_int, 0); t.set_min_sec(n as libc::c_int, 0);
@ -209,9 +215,11 @@ pub fn compute_next_event(
} }
// this minute // this minute
if !event.second.is_empty() { if !event.second.is_empty() && t.changes.contains(TMChanges::SEC) {
let second = t.sec() as u32; let second = t.sec() as u32;
if !DateTimeValue::list_contains(&event.second, second) { if DateTimeValue::list_contains(&event.second, second) {
t.changes.remove(TMChanges::SEC);
} else {
if let Some(n) = DateTimeValue::find_next(&event.second, second) { if let Some(n) = DateTimeValue::find_next(&event.second, second) {
// test next second // test next second
t.set_sec(n as libc::c_int); t.set_sec(n as libc::c_int);

View File

@ -19,7 +19,7 @@ bitflags!{
pub struct TmEditor { pub struct TmEditor {
utc: bool, utc: bool,
t: libc::tm, t: libc::tm,
changes: TMChanges, pub changes: TMChanges,
} }
fn is_leap_year(year: libc::c_int) -> bool { fn is_leap_year(year: libc::c_int) -> bool {