Skip to content

Commit 0436cda

Browse files
committed
Simplified handling of clock changes
1 parent efeb1e4 commit 0436cda

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

libcron/Cron.h

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,37 @@ namespace libcron
128128
}
129129

130130

131+
132+
131133
if (first_tick)
132134
{
133135
first_tick = false;
134136
}
135-
else if (now > last_tick && now - last_tick <= std::chrono::hours{3})
137+
else
136138
{
137-
// Reschedule all tasks.
138-
for (auto& t : tasks.get_tasks())
139+
// https://linux.die.net/man/8/cron
140+
141+
constexpr auto three_hours = std::chrono::hours{3};
142+
auto diff = now - last_tick;
143+
auto absolute_diff = diff > diff.zero() ? diff : -diff;
144+
145+
if(absolute_diff >= three_hours)
139146
{
140-
t.calculate_next(now);
147+
// Time changes of more than 3 hours are considered to be corrections to the
148+
// clock or timezone, and the new time is used immediately.
149+
for (auto& t : tasks.get_tasks())
150+
{
151+
t.calculate_next(now);
152+
}
141153
}
142-
}
143-
else if (now < last_tick && now - last_tick <= -std::chrono::hours{3})
144-
{
145-
// Reschedule all tasks.
146-
for (auto& t : tasks.get_tasks())
154+
else
147155
{
148-
t.calculate_next(now);
156+
// Change of less than three hours
157+
158+
// If time has moved backwards: Since tasks are not rescheduled, they won't run before
159+
// we're back at least the original point in time which prevents running tasks twice.
160+
161+
// If time has moved forward, tasks that would have run since last tick will be run.
149162
}
150163
}
151164

0 commit comments

Comments
 (0)