本文介绍了显示不同时区的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种优雅的方式来显示另一个时区的当前时间?
我想要一些具有以下总体精神的东西:
cur = print("当地时间{}".format(cur))print("太平洋时间{}".format(<类似 cur.tz('PST')>))print("以色列时间{}".format(<类似 cur.tz('IST')>)) 解决方案您可以使用 pytz 库:
>>>从日期时间导入日期时间>>>进口pytz>>>utc = pytz.utc>>>UTC区域'世界标准时间'>>>东部 = pytz.timezone('美国/东部')>>>东区'美国/东部'>>>阿姆斯特丹 = pytz.timezone('欧洲/阿姆斯特丹')>>>fmt = '%Y-%m-%d %H:%M:%S %Z%z'>>>loc_dt = Eastern.localize(datetime(2002, 10, 27, 6, 0, 0))>>>打印 loc_dt.strftime(fmt)2002-10-27 06:00:00 EST-0500>>>ams_dt = loc_dt.astimezone(阿姆斯特丹)>>>ams_dt.strftime(fmt)'2002-10-27 12:00:00 CET+0100'Is there an elegant way to display the current time in another time zone?
I would like to have something with the general spirit of:
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')>))解决方案
You could use the pytz library:
>>> 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'
更多推荐
显示不同时区的时间
发布评论