12

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?

leftaroundabout
  • 644
  • 6
  • 15

1 Answers1

20

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.

frostschutz
  • 47,228
  • 5
  • 112
  • 159
  • Of course we now are curious what would happen if I do `at 2:41` in the night of switching *to* summer time? – Hagen von Eitzen Oct 28 '19 at 15:09
  • As a bit of bonus info: on Android 8, I set the alarm for 02:05AM the previous day. The alarm did *not* go off the first time, but did after the switch from daylight savings time was made. Probably the same reason as in the answer above. – wurtel Oct 28 '19 at 15:24
  • 1
    @Hagen `at -v 2:41 March 29` shows that it takes that into account too: “Sun Mar 29 03:41:00 2020”. – Stephen Kitt Oct 29 '19 at 17:53