Open In App

Python | Timezone Conversion

Improve
Improve
Like Article
Like
Save
Share
Report

Most datetime items came back from the dateutil parser are naive, which means they don’t have an explicit tzinfo. tzinfo determines the timezone and UTC offset. It is the standard ISO format for UTC datetime strings. UTC is the coordinated universal time, and is fundamentally the equivalent as GMT. ISO is the International Standards Organization, which in addition to other things, determines standard datetime designing. Python datetime items can either be naive or mindful. In the event that a datetime item has a tzinfo, at that point it knows. Something else, the datetime is naive. To make an naive datetime object timezone aware, define tzinfo abstract baseclass. In any case, the Python datetime library just characterizes a conceptual baseclass for tzinfo, and leaves it over to others to really actualize tzinfo creation. This is the place the tz module of dateutil comes in—it gives all that it is required to turn upward timezones from your OS timezone information. Installation: 

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.

Getting a UTC tzinfo object – by calling tz.tzutc()

Python3




from dateutil import tz
tz.tzutc()


tzutc()

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

Python3




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


datetime.timedelta(0)

Pass in a timezone file path to the gettz() function to get tzinfo objects for other timezones. 

Python3




tz.gettz('US/Pacific')


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

Python3




tz.gettz('Europe / Paris')


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

Python3




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


datetime.timedelta(-1, 61200)

To change over a non-UTC datetime item to UTC, it must be made timezone mindful. On the off chance that you attempt to change over a credulous datetime to UTC, you’ll get a ValueError exemption. To make a naive datetime timezone mindful, you basically call the replace() strategy with the right tzinfo. Once a datetime item has a tzinfo, at that point UTC change can be performed by calling the astimezone() technique with tz.tzutc(). 

Python3




abc = tz.gettz('US/Pacific')
dat = datetime.datetime(2010, 9, 25, 10, 36)
dat.tzinfo
dat.astimezone(tz.tzutc())


Traceback (most 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

Python3




dat.replace(tzinfo = abc)


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

All behind working –

  • The tzutc and tzfile items are the two subclasses of tzinfo.
  • All things considered, they know the right UTC offset for timezone change (which is 0 for tzutc).
  • A tzfile item realizes how to peruse the working framework’s zoneinfo documents to get the fundamental counterbalance information.
  • The replace() strategy for a datetime item does what the name suggests—it replaces qualities.
  • Once a datetime has a tzinfo, the astimezone() strategy will most likely believer the time utilizing the UTC counterbalances, and afterward supplant the current tzinfo with the new tzinfo

Code : Passing a tzinfos keyword argument into the dateutil parser to detect the unrecognized timezones 

Python3




parser.parse('Wednesday, Aug 4, 2010 at 6:30 p.m. (CDT)',
             fuzzy = True)


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

Python3




tzinfos = {'CDT': tz.gettz('US/Central')}
parser.parse('Wednesday, Aug 4, 2010 at 6:30 p.m. (CDT)',
fuzzy = True, tzinfos = tzinfos)


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


Last Updated : 19 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads