Is there a way I can check if a specific timezone is in daylight saving in a date I specified?
test_dt = datetime(year=2015, month=2, day=1)
pst = pytz.timezone('America/Los_Angeles')
test_dt = pst.localize(test_dt)
# should return False
is_day_light_saving(test_dt)
Just call the datetime.dst()
method:
def is_summer_time(aware_dt):
assert aware_dt.tzinfo is not None
assert aware_dt.tzinfo.utcoffset(aware_dt) is not None
return bool(aware_dt.dst())
Example:
#!/usr/bin/env python
from datetime import datetime
import pytz # $ pip install pytz
naive = datetime(2015, 2, 1)
pacific = pytz.timezone('America/Los_Angeles')
aware = pacific.localize(naive, is_dst=None)
print(is_summer_time(aware))
It is equivalent to:
bool(pytz.timezone('America/Los_Angeles').dst(datetime(2015, 2, 1), is_dst=None))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With