The Python Oracle

Is a specific timezone using DST right now?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Is A Specific Timezone Using Dst Right Now?
00:20 Accepted Answer Score 19
00:43 Answer 2 Score 3
00:57 Thank you

--

Full question
https://stackoverflow.com/questions/1717...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #time #timezone #pytz

#avk47



ACCEPTED ANSWER

Score 19


from pytz import timezone
from datetime import datetime

zonename = "Pacific/Wallis"
now = datetime.now(tz=timezone(zonename))
dst_timedelta = now.dst()
### dst_timedelta is offset to the winter time, 
### thus timedelta(0) for winter time and timedelta(0, 3600) for DST; 
### it returns None if timezone is not set

print "DST" if dst_timedelta else "no DST"

alternative is to use:

now.timetuple().tm_isdst 

Which can have one of 3 values: 0 for no DST, 1 for DST and -1 for timezone not set.




ANSWER 2

Score 3


Python 3.9 has added the zoneinfo module which replaces pytz. Here is a new updated version for modern Python versions.

from zoneinfo import ZoneInfo
from datetime import datetime

bool(datetime.now(tz=ZoneInfo("America/Chicago")).dst())