Welcome to my blog. Help yourself.
timezone is a tricky concern within python, especially to convert string to time with timezone information.
As for me, I use 'pytz' and 'dateutil' together.
Timezone info string such as 'CST', 'CCT' within a time string is not work at all when using dateutil.parser.parse. It always was treated as utc time.
To make it work, we should use '+8:00' and something the same style specification.
from dateutil.parser import parser, parse from dateutil.tz import tzlocal sh="09-30 22:24 +8:00" dt=parse(sh) dt.astimezone(pytz.timezone('Asia/Shanghai')) dt.astimezone(pytz.timezone('UTC')) oops="09-30 22:24 CST" # not work, treated as utc
We will find the difference and know that it works.