Time zones in Python

July 29, 2020 Python
# timezone.py
# exemplar
from datetime import datetime
from dateutil.parser import parse
from dateutil import tz

# The input isn't something that Python recognizes as a date
s = "Mon Aug 15 21:17:14 GMT 2011"     #start off with a string
d = parse(s)                           #convert it to a datetime object

# parse doesn't capture timezone information, so we need to specify
GMT = tz.gettz('UTC')                  #label for GMT
Beijing = tz.gettz('Asia/Shanghai')    #label for localtime

there = d.astimezone(GMT)              #express the datetime object in the GMT(UTC) zone
here = d.astimezone(Beijing)           #express it local zone