Python | Time zone conversion

Python date and time elements can be either naive or attentive. In case the datetime element has tzinfo, at this point it knows. Something else, the date and time are naive. To clarify the time zone of a datetime object, define an abstract base class, tzinfo. In any case, the Python datetime library simply characterizes the conceptual base class for tzinfo and leaves it to others to actually implement the creation of tzinfo. This is where the tz dateutil module comes in — it gives everything it takes to switch upstream timezones from your OS timezone information.

Setting :

Use pip or easy_install dateutil to install. Make sure that the operating system has timezone data.

On Linux, this is usually found in / usr / share / zoneinfo , and the Ubuntu package is called tzdata. In case of the number of files and directories in / usr / share / zoneinfo , such as America / and Europe /, then it`s ready to proceed.

Get UTC object tzinfo — by calling tz.tzutc()

from dateutil import tz

tz.tzutc ()

tzutc()

The offset is 0 by calling the utcoffset () method with a UTC date / time object.

import datetime

tz.tzutc (). utcoffset (datetime.datetime.utcnow ())

 datetime.timedelta (0) 

Pass the path to the file time zone in gettz () to get tzinfo objects for other time zones.

tz.gettz ( `US / Pacific` )

 tzfile (`/ usr / share / zoneinfo / US / Pacific`) 

tz.gettz ( `Europe / Paris` )

tzfile(`/usr/share/zoneinfo/Europe/Paris`)

tz.gettz ( `US / Pacific` ). utcoffset (datetime.datetime.utcnow ())

 datetime.timedelta (-1, 61200) 

To change a time and date element other than UTC to UTC, you need to Remember the time zone. If you try to change the trusted time to UTC, you will get a ValueError exception. To remember the naive datetime timezone, you basically call the replace () strategy with the tzinfo you want. Once the datetime element has tzinfo, UTC can be changed at that point by calling the astimezone () method with tz.tzutc () .

abc = tz.gettz ( `US / Pacific` )

dat = datetime.datetime ( 2010 , 9 , 25 , 10 , 36 )

dat.tzinfo
dat.astimezone (tz.tzutc ())

 Traceback (mos t recent call last): File "/usr/lib/python2.6/doctest.py", line 1228, in __run compileflags, 1) in test.globs File "", line 1, in dat.astimezone (tz.tzutc ()) ValueError: astimezone () cannot be applied to a naive datetime 

dat.replace (tzinfo = abc)

 datetime.datetime (2010, 9, 25, 10, 36, tzinfo = tzfile (`/ usr / share / zoneinfo / US / Pacific`)) 

Everything is behind work —

  • The tzutc and tzfile elements are two subclasses of tzinfo.
  • All things considered, they know the correct UTC offset to change the timezone (0 for tzutc) .
  • The tzfile element understands how to scan the work structure zoneinfo documents for fundamental counterweight information.
  • The replace () strategy for the element nta datetime does what the name — it replaces qualities.
  • When a datetime has a tzinfo, the astimezone () strategy will most likely take time into account using UTC counterweights and then displace the current tzinfo with a new tzinfo.

Code: Pass the tzinfos keyword argument to the dateutil parser to detect unrecognized time zones

parser.parse ( `Wednesday, Aug 4, 2010 at 6:30 pm (CDT)` ,

fuzzy = True )

 datetime. datetime (2010, 8, 4, 18, 30) 

tzinfos  = { `CDT` : tz.gettz ( ` US / Central` )}

parser.parse ( `Wednesday, Aug 4, 2010 at 6:30 pm (CDT)` ,

fuzzy = True , tzinfos = tzinfos)

 datetime.datetime (2010, 8, 4, 18, 30, tzinfo = tzfile (`/ usr / share / zoneinfo / US / Central`))