Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python daylight savings time

Tags:

python

time

dst

How do I check if daylight saving time is in effect?

like image 354
Pawel Furmaniak Avatar asked May 21 '10 09:05

Pawel Furmaniak


People also ask

How does Python determine Daylight Savings Time?

You can use time. localtime and look at the tm_isdst flag in the return value. Using time. localtime() , you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your current time zone.

Does UTC have daylight saving time?

The switch to daylight saving time does not affect UTC. It refers to time on the zero or Greenwich meridian, which is not adjusted to reflect changes either to or from Daylight Saving Time.

Does pytz account for daylight savings?

Timedelta and DSTpytz will help you to tell if an date is under DST influence by checking dst() method.

What is the function to offset the date for daylight saving in time series?

IsDaylightSavingTime(DateTimeOffset) Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.


2 Answers

You can use time.localtime and look at the tm_isdst flag in the return value.

>>> import time >>> time.localtime() (2010, 5, 21, 21, 48, 51, 4, 141, 0) >>> _.tm_isdst 0 

Using time.localtime(), you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your current time zone.

like image 87
Greg Hewgill Avatar answered Sep 18 '22 21:09

Greg Hewgill


The accepted answer is fine if you are running code on your laptop, but most python applications are running on a server using UTC as local time, so they will NEVER be in daylight savings time according to the accepted answer.

The second problem is that different regions implement daylight savings on different days and times. So even if you have an unambiguous time, such as datetime.utcnow(), it could be daylight savings time in one timezone but not in another.

The best we can do then, is tell whether a given time occurs during DST for a specific timezone, and the best method I can find for doing it has already been implemtend by pytz localize function and we can use it to get a pretty good answer that works both on our laptop and on a server.

import pytz  from datetime import datetime  def is_dst(dt=None, timezone="UTC"):     if dt is None:         dt = datetime.utcnow()     timezone = pytz.timezone(timezone)     timezone_aware_date = timezone.localize(dt, is_dst=None)     return timezone_aware_date.tzinfo._dst.seconds != 0 

Some examples

>>> is_dst() # it is never DST in UTC False >>> is_dst(datetime(2019, 1, 1), timezone="US/Pacific") False >>> is_dst(datetime(2019, 4, 1), timezone="US/Pacific") True >>> is_dst(datetime(2019, 3, 10, 2), timezone="US/Pacific") NonExistentTimeError >>> is_dst(datetime(2019, 11, 3, 1), timezone="US/Pacific") AmbiguousTimeError 

In our is_dst function, we specified is_dst=None as a parameter to timezone.localize, which will cause nonsense times to throw errors. You could use is_dst=False to ignore these errors and return False for those times.

like image 34
mehtunguh Avatar answered Sep 19 '22 21:09

mehtunguh