Python timezone

Python timezone

Welcome to my blog. Help yourself.
----- perrin

2018-09-30 23:14


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.


Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Commenter Name
Cras sit amet nibh libero, in gravida nulla. Nulla vel metus scelerisque ante sollicitudin. Cras purus odio, vestibulum in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
Search
Page view info
{'ip': '18.204.227.250', 'lat_lon': (39.0481, -77.4728), 'city': 'Ashburn', 'pv': 57915, 'cur_pv': 110, 'cur_daily_pv': 1, 'ur_pv': 2, 'ur_cur_all': 1, 'ur_today_all': 2, 'ur_today_cur': 1, 'daily_pv': 138, 'daily_ip': 79, 'whole_ip': 9790, 'ip_delta': 4}
AMap
Baidu Map