The Python Oracle

Is a specific timezone using DST right now?

This video explains
Is a specific timezone using DST right now?

--

Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay

--

Chapters
00:00 Question
00:37 Accepted answer (Score 19)
01:04 Answer 2 (Score 2)
01:26 Thank you

--

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

Answer 1 links:
[zoneinfo module]: https://docs.python.org/3/library/zonein...

--

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())