How does `at` know there will be a time change?

I happened to be twiddling around with at just yesterday at 2:40 AM. (Don’t ask…) I tried scheduling events one minute in the future (as a test), and – it just didn’t happen. The events were in the queue allright, but they just stayed there when it was 2:41, without anything happening. I gave up and went to bed.

What was going on? Well – it turns out today daylight saving time ended in Norway (summer time), so indeed 2:41 happened twice tonight! And sure enough, at the second one (i.e. 3:41 as seen from summer time), at then fired off those scheduled events.

How does this work? Why and how is the scheduling aware of time change, and picks the second time around, rather than just doing it when the time passes the specified one?

Asked By: leftaroundabout

||

at attempts to parse the date and time given and runs it through mktime() with “Daylight Saving Time” set to -1 (not available, auto-detect).

at source code:

  tm1.tm_isdst = -1;
  t = mktime (&tm1);

man 3 mktime:

The value specified in the tm_isdst field informs mktime() whether or not daylight saving time (DST) is in effect for the time supplied in the tm structure: a positive value means DST is in effect; zero means that DST is not in effect; and a negative value means that mktime() should (use timezone information and system databases to) attempt to determine whether DST is in effect at the specified time.

The mktime() function modifies the fields of the tm structure as follows: […] tm_isdst is set (regardless of its initial value) to a positive value or to 0, respectively, to indicate whether DST is or is not in effect at the specified time.

So mktime() decides whether DST is or is not in effect; and it seems to prefer the later date out of two possible choices (although it’s not clear from documentation how that decision is made).

at then converts it to the UNIX timestamp (seconds since 1 January 1970). And once you have that, there’s no such thing as a time change anymore. Changing the clock back and forth by one hour only changes the human representation of time, or the timezone you’re in; it does not change the number of seconds since epoch nor the number of seconds until the task will be run.


I tried scheduling events one minute in the future (as a test)

On a sidenote, you can use an expression like next minute, that’ll run the next minute, time change or no.

Answered By: frostschutz
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.