this time, i have questions on timezones in Python
How do i , say from anywhere in the world, convert that local time into say, New york time? first of, I think datetime module is the one to use. Should I use utcfromtimestamp() , then use some other functions to convert to New york time? How do i actually do that. thanks
It sounds like you want to use the pytz module. The docs are very comprehensive and have some nice examples for you. http://pytz.sourceforge.net/
From the docs:
>>> from datetime import datetime, timedelta
>>> from pytz import timezone
>>> import pytz
>>> utc = pytz.utc
>>> utc.zone
'UTC'
>>> eastern = timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'
>>> amsterdam = timezone('Europe/Amsterdam')
>>> fmt = '%Y-%m-%d %H:%M:%S %Z%z'
>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
>>> print(loc_dt.strftime(fmt))
2002-10-27 06:00:00 EST-0500
>>> ams_dt = loc_dt.astimezone(amsterdam)
>>> ams_dt.strftime(fmt)
'2002-10-27 12:00:00 CET+0100'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1830 次 |
| 最近记录: |