1832279 Members
2072 Online
110041 Solutions
New Discussion

Is DST in effect?

 
SOLVED
Go to solution
Craig Sterley
Advisor

Is DST in effect?

How can i determine via a script is Daylight Savings Time is in effect or not?
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Is DST in effect?

Hi Craig:

# perl -le '$dst=(localtime)[8];print $dst==1 ? "in DST":"not in DST"'

...is an easy way to answer your question.

Regards!

...JRF...
Craig Sterley
Advisor

Re: Is DST in effect?

Thank you for the quick response. That did the trick.
Craig Sterley
Advisor

Re: Is DST in effect?

case closed. Thanks again
Matti_Kurkela
Honored Contributor

Re: Is DST in effect?

A shell script solution: examine the timezone identifier (available with "date +%Z").

For example, at the US West Coast, it should return PST when using Standard Time and PDT when the Daylight Saving Time is in effect.

This can be made timezone-agnostic by using the fact that the only thing we need to know is whether the current timezone identifier is at the beginning of the TZ value, or at the end.

Disclaimer: quick&dirty, with very little testing. If it breaks, you can keep the pieces.

#!/bin/sh
NOW=$(date +%Z)
if [ ${TZ#$NOW} != ${TZ} ]
then
echo "Standard time"
elif [ ${TZ%$NOW} != ${TZ} ]
then
echo "Daylight time"
else
echo "ERROR: date command outputted a timezone identifier not listed in TZ"
fi

MK
MK