I recently ran into a beginner problem at work where I had to write a utility method that would receive a timestamp and the timezone of the timestamp and convert it into a human readable form for UTC timezone. Now, this looked straightforward except that I hadn't worked with the datetime library before. So after much searching, I came across the very useful datetime.astimezone()
method which helped solve the problem.
The astimezone(tz=None)
takes in a datetime object and returns a new datetime object with the time set to the equivalent time in the datetime.tzinfo attribute tz. If no tz object is not provided, it defaults to UTC timezone.
>>> from datetime import datetime