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.
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
The offset is 0 by calling the
utcoffset () method with a UTC date / time object.
tz.tzutc (). utcoffset (datetime.datetime.utcnow ()) p>
Pass the path to the file time zone in
gettz () to get tzinfo objects for other time zones.
tzfile ('/ usr / share / zoneinfo / US / Pacific')
'Europe / Paris'
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 () .
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
datetime.datetime (2010, 9, 25, 10, 36, tzinfo = tzfile ('/ usr / share / zoneinfo / US / Pacific'))
Everything is behind work —
Code: Pass the tzinfos keyword argument to the dateutil parser to detect unrecognized time zones
datetime. datetime (2010, 8, 4, 18, 30)
datetime.datetime (2010, 8, 4, 18, 30, tzinfo = tzfile ('/ usr / share / zoneinfo / US / Central'))
Mark Lutz is the global leader in Python training, author of the oldest and best-selling Python texts, and a pioneer in the Python community since 1992.
Mark Lutz is the author of the found...
Computer Age Statistical Inference: Algorithms, Evidence, and Data Science (Institute of Mathematical Statistics Monographs, Series Number 6). The twenty-first century has seen a breathtaking expan...
Cracking the Coding Interview PDF: 189 Programming Questions and Solutions, 6th Edition. I am not a recruiter. I am a software engineer. And as such, I know what it's like to be asked to create ing...
A Practical Approach to Computer Algorithms Using Python® and C# Rod Stephens started out as a mathematician, but while studying at MIT, he discovered how much fun algorithms are. He took every al...