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.
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
The offset is 0 by calling the
utcoffset() method with a UTC datetime object.
Pass in a timezone file path to the
gettz() function to get tzinfo objects for other timezones.
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
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
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
datetime.datetime(2010, 8, 4, 18, 30)
datetime.datetime(2010, 8, 4, 18, 30, tzinfo=tzfile(' /usr/share/zoneinfo/US/Central'))
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course