croncpp icon indicating copy to clipboard operation
croncpp copied to clipboard

Daylight savings issue

Open gbohrn opened this issue 3 years ago • 6 comments

There is definitely a problem with this code having to do with daylight savings transition. Simple example

time_t now = 1647084600;    // Mar 12, 2022 4:30am
string e = "0 30 4 * * *";
cron::cronexpr exp = cron::make_cron(e);
time_t next= cron::cron_next(exp, now);

"now" and "next" come out identical. Expectation is next would be a day ahead. Works the rest of the year

gbohrn avatar Apr 07 '22 04:04 gbohrn

For a quick reference. mktime can and does modify the input to your tm2time function. This is just broken. Before using mktime or localtime_s you need to make a copy as these functions can and do screw up your calculations. localtime is allocated statically and mktime as per: https://www.cplusplus.com/reference/ctime/mktime/ specifically says it may actually change your input.

gbohrn avatar Apr 10 '22 23:04 gbohrn

I can't reproduce this. When I run your code, next has the value 1647138600, which is Sun Mar 13 04:30:00 2022.

mariusbancila avatar May 02 '22 20:05 mariusbancila

That's because in the US the DST occurred on March 13, but only on March 27 in Romania.

mariusbancila avatar May 03 '22 07:05 mariusbancila

setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1); tzset(); auto now = cron::cron_next(cron::make_cron("0 30 2 * * ?"), 1711825200);// 1711762200 1711762200 < 1711825200

ShunzDai avatar Apr 02 '24 10:04 ShunzDai

setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1); tzset(); auto now = cron::cron_next(cron::make_cron("0 30 2 * * ?"), 1711825200);// 1711762200 1711762200 < 1711825200

This can be solved by moving each date.tm_isdst = -1; outside the switch (field)

ShunzDai avatar Apr 02 '24 12:04 ShunzDai

Hello i encountered the same issue with the last daylight saving time in France which occurred the 31 march 2024.

check_next("0 0 8 * * ?", "2024-03-30 08:00:00", "2024-03-31 08:00:00");
or
check_next("0 0 8 * * ?", "2024-03-30 09:00:00", "2024-03-31 08:00:00");
FAILED:
  REQUIRE( value == expected )
with expansion:
  "2024-03-30 08:00:00"
  ==
  2024-03-31 08:00:00

the date.tm_isdst = -1; trick solve the issue

phmaurel avatar Apr 03 '24 14:04 phmaurel