From 0436cda4efc15fd61af3b881b7fbac8d77747a77 Mon Sep 17 00:00:00 2001 From: Per Malmberg Date: Tue, 22 May 2018 21:45:00 +0200 Subject: [PATCH] Simplified handling of clock changes --- libcron/Cron.h | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/libcron/Cron.h b/libcron/Cron.h index b4101a0..6746ada 100644 --- a/libcron/Cron.h +++ b/libcron/Cron.h @@ -128,24 +128,37 @@ namespace libcron } + + if (first_tick) { first_tick = false; } - else if (now > last_tick && now - last_tick <= std::chrono::hours{3}) + else { - // Reschedule all tasks. - for (auto& t : tasks.get_tasks()) + // https://linux.die.net/man/8/cron + + constexpr auto three_hours = std::chrono::hours{3}; + auto diff = now - last_tick; + auto absolute_diff = diff > diff.zero() ? diff : -diff; + + if(absolute_diff >= three_hours) { - t.calculate_next(now); + // Time changes of more than 3 hours are considered to be corrections to the + // clock or timezone, and the new time is used immediately. + for (auto& t : tasks.get_tasks()) + { + t.calculate_next(now); + } } - } - else if (now < last_tick && now - last_tick <= -std::chrono::hours{3}) - { - // Reschedule all tasks. - for (auto& t : tasks.get_tasks()) + else { - t.calculate_next(now); + // Change of less than three hours + + // If time has moved backwards: Since tasks are not rescheduled, they won't run before + // we're back at least the original point in time which prevents running tasks twice. + + // If time has moved forward, tasks that would have run since last tick will be run. } }