My Python-based web server needs to perform some date manipulation using the client's timezone, represented by its UTC offset. How do I construct a datetime object with the specified UTC offset as timezone?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
The
datetime
module documentation contains an exampletzinfo
class that represents a fixed offset.Since Python 3.2 it is no longer necessary to provide this code, as
datetime.timezone
anddatetime.timezone.utc
are included in thedatetime
module and should be used instead.Using
dateutil
:As an aside, Python 3 (since v3.2) now has a timezone class that does this:
However, note that "objects of this class cannot be used to represent timezone information in the locations where different offsets are used in different days of the year or where historical changes have been made to civil time." This is generally true of any time zone conversion relying strictly on UTC offset.