Open In App

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




from dateutil import tz
tz.tzutc()

tzutc()

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






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. 




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




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




dat.replace(tzinfo = abc)

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

All behind working –

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




parser.parse('Wednesday, Aug 4, 2010 at 6:30 p.m. (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 p.m. (CDT)',
fuzzy = True, tzinfos = tzinfos)

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

Article Tags :