How to use datetime.tzinfo in Python?

Charlie Lee picture Charlie Lee · Sep 5, 2017 · Viewed 8k times · Source

I have no idea how to use datetime.tzinfo module. I have to convert a datetime data from UTC to local timezone and if I retrieve '+2'(which means gmt+2) how do I convert this to tzinfo object?

Also, is it possible to convert from UTC to local timezone with daylight saving time applied automatically? How do I set to subtract one hour automatically only during dst?

Answer

dacada picture dacada · Sep 5, 2017

Python doesn't really implement well timezones. According to the documentation, tzinfo is just an abstract class meant to be implemented by the user.

I'd recommend the third party pytz module for dealing with timezones.