Search code examples
bashdatedst

Detect daylight saving time in bash


I want to detect if I'm in winter or summer time. My current approach is:

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

which have obvious drawback as you have to know all the timezone names.


Solution

  • Perl to the rescue:

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

    That's because localtime returns the following list:

    #     0    1    2     3     4    5     6     7     8
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                localtime(time);
    

    where "isdst" stands for is daylight saving time.