Ada*_*tan 56 python time timezone
有没有一种优雅的方式来显示另一个时区的当前时间?
我想拥有一般的精神:
cur = <Get the current time, perhaps datetime.datetime.now()>
print("Local time {}".format(cur))
print("Pacific time {}".format(<something like cur.tz('PST')>))
print("Israeli time {}".format(<something like cur.tz('IST')>))
Run Code Online (Sandbox Code Playgroud)
Mar*_*sen 107
一种更简单的方法:
from datetime import datetime
from pytz import timezone
south_africa = timezone('Africa/Johannesburg')
sa_time = datetime.now(south_africa)
print sa_time.strftime('%Y-%m-%d_%H-%M-%S')
Run Code Online (Sandbox Code Playgroud)
And*_*ler 53
你可以使用pytz库:
>>> from datetime import datetime
>>> import pytz
>>> utc = pytz.utc
>>> utc.zone
'UTC'
>>> eastern = pytz.timezone('US/Eastern')
>>> eastern.zone
'US/Eastern'
>>> amsterdam = pytz.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)
MrF*_*pes 18
Python 3.9:使用标准库中的zoneinfo:
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
# Israel and US/Pacific time:
now_Israel = datetime.now(ZoneInfo('Israel'))
now_Pacific = datetime.now(ZoneInfo('US/Pacific'))
print(f"Israeli time {now_Israel.isoformat(timespec='seconds')}")
print(f"Pacific time {now_Pacific.isoformat(timespec='seconds')}")
# Israeli time 2021-03-26T18:09:18+03:00
# Pacific time 2021-03-26T08:09:18-07:00
# for reference, local time and UTC:
now_UTC = datetime.now(tz=timezone.utc)
now_local = datetime.now().astimezone()
print(f"Local time {now_local.isoformat(timespec='seconds')}")
print(f"UTC {now_UTC.isoformat(timespec='seconds')}")
# Local time 2021-03-26T16:09:18+01:00 # I'm on Europe/Berlin
# UTC 2021-03-26T15:09:18+00:00
Run Code Online (Sandbox Code Playgroud)
注意:有一个折旧垫片的pytz
。
旧版本的 Python 3:您可以zoneinfo
通过backports模块使用或改用dateutil。dateutiltz.gettz
遵循与以下相同的语义zoneinfo.ZoneInfo
:
from dateutil.tz import gettz
now_Israel = datetime.now(gettz('Israel'))
now_Pacific = datetime.now(gettz('US/Pacific'))
print(f"Israeli time {now_Israel.isoformat(timespec='seconds')}")
print(f"Pacific time {now_Pacific.isoformat(timespec='seconds')}")
# Israeli time 2021-03-26T18:09:18+03:00
# Pacific time 2021-03-26T08:09:18-07:00
Run Code Online (Sandbox Code Playgroud)
Mar*_*wis 12
一种方法,通过C库的时区设置,是
>>> cur=time.time()
>>> os.environ["TZ"]="US/Pacific"
>>> time.tzset()
>>> time.strftime("%T %Z", time.localtime(cur))
'03:09:51 PDT'
>>> os.environ["TZ"]="GMT"
>>> time.strftime("%T %Z", time.localtime(cur))
'10:09:51 GMT'
Run Code Online (Sandbox Code Playgroud)
小智 10
问题的最短答案可以是:
from datetime import datetime
import pytz
print(datetime.now(pytz.timezone('Asia/Kolkata')))
Run Code Online (Sandbox Code Playgroud)
这将打印:
2019-06-20 12:48:56.862291+05:30
这个使用pytz
和datetime
模块的脚本按要求构建:
#!/usr/bin/env python3
import pytz
from datetime import datetime, timezone
utc_dt = datetime.now(timezone.utc)
PST = pytz.timezone('US/Pacific')
IST = pytz.timezone('Asia/Jerusalem')
print("UTC time {}".format(utc_dt.isoformat()))
print("Local time {}".format(utc_dt.astimezone().isoformat()))
print("Pacific time {}".format(utc_dt.astimezone(PST).isoformat()))
print("Israeli time {}".format(utc_dt.astimezone(IST).isoformat()))
Run Code Online (Sandbox Code Playgroud)
它输出以下内容:
$ ./timezones.py
UTC time 2019-02-23T01:09:51.452247+00:00
Local time 2019-02-23T14:09:51.452247+13:00
Pacific time 2019-02-22T17:09:51.452247-08:00
Israeli time 2019-02-23T03:09:51.452247+02:00
Run Code Online (Sandbox Code Playgroud)