本文介绍了如何使用Python将UTC转换为EST并自动进行夏时制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一堆日期&时间为 UTC格式
,如何将它们转换为 EST
。
If I have a bunch of data with date & time in UTC format
, how can I convert them to EST
.
它可以确定何时将它们变为 -4(夏季)
和- 5 (冬季)
每年自动吗?
谢谢
It can determine when they will be -4(in summer)
and -5(in winter)
automatically every year? Thanks
推荐答案
您需要使用 pytz
模块(可从PyPI获得):
You'll need to use the pytz
module (available from PyPI):
import pytz
from datetime import datetime
est = pytz.timezone('US/Eastern')
utc = pytz.utc
fmt = '%Y-%m-%d %H:%M:%S %Z%z'
winter = datetime(2016, 1, 24, 18, 0, 0, tzinfo=utc)
summer = datetime(2016, 7, 24, 18, 0, 0, tzinfo=utc)
print winter.strftime(fmt)
print summer.strftime(fmt)
print winter.astimezone(est).strftime(fmt)
print summer.astimezone(est).strftime(fmt)
将打印:
2016-01-24 18:00:00 UTC+0000
2016-07-24 18:00:00 UTC+0000
2016-01-24 13:00:00 EST-0500
2016-07-24 14:00:00 EDT-0400
您需要使用'美国/东部'
而不是'EST的原因'
在输出的最后两行中得到了体现。
The reason why you'll need to use 'US/Eastern'
and not 'EST'
is exemplified in the last two lines of output.
这篇关于如何使用Python将UTC转换为EST并自动进行夏时制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!