Python | Timezone Conversion

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 offest. 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()

filter_none

edit
close

play_arrow

link
brightness_4
code

from dateutil import tz
tz.tzutc()

chevron_right


tzutc()

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


datetime.timedelta(0)

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

filter_none

edit
close

play_arrow

link
brightness_4
code

tz.gettz('US/Pacific')

chevron_right


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

edit
close

play_arrow

link
brightness_4
code

tz.gettz('Europe / Paris')

chevron_right


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

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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().

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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
filter_none

edit
close

play_arrow

link
brightness_4
code

dat.replace(tzinfo = abc)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.