[tz] How to compute the moment of the next DST change

Ian Abbott abbotti at mev.co.uk
Tue Apr 2 10:14:34 UTC 2013


On 2013-04-02 10:23, John Haxby wrote:
> On 02/04/13 09:58, Simon Perreault wrote:
>>
>> I want to write a cron job or similar that activates one week prior to
>> daylight saving time changes. (Or, more generically, one week prior to
>> any time jumps, for any reason, DST or else.)
>>
>> Any pointers?
>>
>> I have looked at the usual places: POSIX time functions, various
>> date/time modules on CPAN, etc. Nothing seems to do that computation. Do
>> I need to parse the tz database myself?
>
> If you're on Linux:
>
> TZ1=$(date +%Z)
> TZ2=$(date =d 'next week' +%Z)
> if [ $TZ1 != $TZ2 ]; then
>      echo clocks change next week
> fi

That wouldn't work if the abbreviations for normal time and daylight 
savings time are the same, but that might not be a problem for the 
original poster.  It also only works for the date command from GNU 
coreutils as the -d option either doesn't exist or does something else 
on other systems.

If using the date command from GNU coreutils, you could use +%z or %:z 
instead of %Z to get a numeric timezone offset instead of a timezone 
abbreviation.

> You can also use localtime(3) with the current time and the current time
> plus 7*24*2600 and compare the tm_hour field.

That sounds okay, though better to take the minutes into consideration 
as well in case you call it at xx:59:59 just as the hour is about to 
roll over.

Of course, the cron job would have to run once a day (or whatever, 
depending on the precision required) to check for a change and then 
decide what to do.  There is no cron time syntax related to jumps in 
local time.

-- 
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti at mev.co.uk>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-


More information about the tz mailing list