标准库没有定义任何时区-
至少没有很好地定义(文档中给出的玩具示例无法处理此处提到的细微问题)。对于预定义的时区,请使用第三方pytz模块。
import pytzimport datetime as DTeastern = pytz.timezone('US/Eastern')utc = pytz.utctest = '2013-03-27 23:05'
这是一个“天真”的日期时间:
test2 = DT.datetime.strptime(test, '%Y-%m-%d %H:%M') print(test2)# 2013-03-27 23:05:00
这通过解释
test2好像在EST时区中来形成一个时区感知日期时间:
print(eastern.localize(test2))# 2013-03-27 23:05:00-04:00
这通过解释
test2好像在UTC时区中来形成时区感知日期时间:
print(utc.localize(test2))# 2013-03-27 23:05:00+00:00
或者,您可以使用以下
astimezone方法将一个时区感知日期时间转换为另一时区:
test2_eastern = eastern.localize(test2)print(test2_eastern.astimezone(utc))# 2013-03-28 03:05:00+00:00
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)