Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Python to find out if a timezone currently in daylight savings time [duplicate]

We have a server that runs on GMT time. I need to write a Python script that determines if it's currently (at this very second) Daylight Savings Time (DST) in Los Angeles, CA. How can I accomplish this? I took a look at pytz and time, but I can't figure it out. I realize that I could create some logic such as comparing the current time in LA to GMT time, but it would be a lot cleaner if I could use a standard library instead.

Thanks

Edit: Here's some sample code of me setting up the timezone:

from pytz import timezone import pytz from datetime import datetime, timedelta  tz = timezone('America/Los_Angeles') // Instantiate a datetime object using tz?  

Edit: Here's a snippet of code that will work. It's not elegant, which is why I'm asking if there's a library or something that is made for this. Maybe like a is_dst() function.

utc = timezone("UTC") now = utc.localize(datetime.utcnow()) los_angeles_tz = timezone('America/Los_Angeles') los_angeles_time = now.astimezone(los_angeles_tz)  delta = los_angeles_time.utcoffset() dstDelta = timedelta(hours=-8)  is_dst = (delta == dstDelta) 
like image 732
Joey Mason Avatar asked Nov 04 '13 18:11

Joey Mason


People also ask

How do you check is daylight saving is on in Python?

daylight() Function. Time. daylight() function which returns a non zero integer value when Daylight Saving Time (DST) is defined, else it returns 0.

How does Python handle DST?

You should use datetime. datetime. utcnow(). astimezone(tz) -- This gets the time in UTC and then offsets it from UTC according to whatever rules apply in the timezone tz.

Does pytz handle DST?

pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference ( datetime. tzinfo ).

How do I check my DST time?

Test transition of DST, i.e. when you are currently in summer time, select a time value from winter. Test boundary cases, such as a timezone that is UTC+12, with DST, making the local time UTC+13 in summer and even places that are UTC+13 in winter.


2 Answers

import pytz from datetime import datetime, timedelta  def is_dst(zonename):     tz = pytz.timezone(zonename)     now = pytz.utc.localize(datetime.utcnow())     return now.astimezone(tz).dst() != timedelta(0) 

Usage:

>>> is_dst("America/Los_Angeles") False  >>> is_dst("America/Sao_Paulo") True 
like image 153
Matt Johnson-Pint Avatar answered Sep 29 '22 13:09

Matt Johnson-Pint


from datetime import datetime import pytz  isdst_now_in = lambda zonename: bool(datetime.now(pytz.timezone(zonename)).dst()) 

Example:

>>> isdst_now_in("America/Los_Angeles") # 2014-10-27 12:32:07 PDT-0700 True >>> isdst_now_in("Australia/Melbourne") # 2014-10-28 06:32:07 AEDT+1100 True 

Here's stdlib-only version on Unix using time.tzset() function:

import os import time from contextlib import contextmanager  @contextmanager def local_timezone(zonename):     #NOTE: it manipulates global state     oldname = os.environ.get('TZ')     try:         os.environ['TZ'] = zonename         time.tzset()         yield     finally:         if oldname is None:            del os.environ['TZ']         else:            os.environ['TZ'] = oldname         time.tzset()  def time_isdst_now_in(zonename):     with local_timezone(zonename):          return time.localtime().tm_isdst > 0 

The usage is the same:

>>> time_isdst_now_in('Europe/London') # 2014-04-17 18:42:11 BST+0100 True 

Note: time.daylight is not used due to issues in some edge cases.

like image 26
jfs Avatar answered Sep 29 '22 12:09

jfs