Daylight saving time in bash

I want to find out if I am in winter or summer. My current approach:

if date +%Z | grep -e CET -e EST; then # I'm in winter time else # I'm in summer time fi 

which have an obvious drawback, since you must know all the names of time zones.

+7
date bash dst
source share
3 answers

Pearl for salvation:

 if perl -e 'exit ((localtime)[8])' ; then echo winter else echo summer fi 
+8
source share

I’m not right now if that answers your question exactly, but it gives you some tools to help you better understand and verify what is happening.

You can use date and environment-var TZ to help you.

So, for example, I live in Sweden, so my time zone is Europe / Stockholm. So in winter, date +%Z reports CET and in summer CEST. The best part is that you can specify the time zone for the environment of a particular command, then you can specify what date the date command should be present. Therefore, to summarize, you can do any of the following:

 TZ=Europe/Stockholm date +%Z # CET or CEST depending of when its run TZ=Europe/Stockholm date --date=20170101 +%Z # CET TZ=Europe/Stockholm date --date=20170601 +%Z # CEST TZ=CET date --date=20170101 +%Z # CET TZ=CET date --date=20170601 +%Z # CEST, note that its auto adjusted to CEST 

If you need a time difference with UTC, you can use the lowercase z:

 TZ=Europe/Stockholm date +%z # +0100 or +0200 depending of when its run TZ=Europe/Stockholm date --date=20170101 +%z # +0100 TZ=Europe/Stockholm date --date=20170601 +%z # +0200 TZ=CET date --date=20170101 +%z # +0100 TZ=CET date --date=20170601 +%z # +0200 

NOTE You cannot use TZ=CEST or TZ=ULFR , as this is not a valid TZ:

 TZ=CEST date --date=20170101 +%z # +0000 TZ=ULFR date --date=20170101 +%z # +0000 

Crontab example:

We run our servers in UTC, but some tasks performed by crontab must be performed on a given stand (CET / CEST). Therefore, since we want jobs to be completed one hour in winter (the clock is set one hour ahead, the witch makes it reach a certain UTC time an hour earlier in the summer than in winter), we do sleep before the actual work is done in the winter .

We want the work of /whatever/bin/foobar done at 04:15 times a day. But since cron runs on UTC, work needs to be set up an hour earlier for CET and two hours earlier for CEST. This would be the same as always executing a command two hours before, but sleeping for an hour in the winter. Example crontab line:

 15 2 * * * [ `TZ=CET date +\%Z` = CET ] && sleep 3600; /whatever/bin/foobar 

If you have a better solution to this problem, please feel free to advise me!

+3
source share

In the northern hemisphere, in regions with summer savings, then it is active when the displacement is greater than the displacement in January. In the southern hemisphere time zones, daylight saving time is effective when the offset is greater than in July.

You can detect biases in January and July, as well as now:

 OFF_1=$(date -d '1 Jan' +%z) OFF_7=$(date -d '1 Jul' +%z) OFF_NOW=$(date +%z) 

If your zone does not have daylight saving time, all three will be the same (this year). In any case, we can make two comparisons: one for the northern hemisphere and one for the south; if the current offset is greater than one of them, then the current zone is in the summer:

 if test $OFF_NOW -gt $OFF_1 || test $OFF_NOW -gt $OFF_7 then # I'm in summer time else # I'm in winter time fi 

The assumption here is that all regions that are observing daylight saving time have a transition somewhere between July and January. As far as I know, this is true in all areas. The only time this can fail is when the winter shift is changing (for example, 1940 in the UK, when the next winter was GMT + 1).

+3
source share

All Articles