我有以下脚本来定义tzinfo
对象:
import time
from datetime import datetime, timedelta, tzinfo
class ManilaTime(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=8)
def tzname(self, dt):
return "Manila"
manila = ManilaTime()
现在,我要说
t = datetime(tzinfo=manila, *time.gmtime()[:-3])
print t
这让我
2011-07-24 12:52:06+08:00
问题:什么是12:52:06+08:00
?我想学习如何根据标准读取包含UTC偏移量的时间信息。请忽略我使用了time.gmtime()
。假设我只得到了时间字符串本身。我怎么看?你知道吗
A.我需要执行加法以获得时间。读了这篇文章,我要算算一下,然后说
It's
12:52:06
in Greenwich, which I should offset by+08:00
. Meaning, it is20:52:06
in Manila.
B.我会按表面价值来判断,然后说
It's
12:52:06
in Manila, and it's offset from UTC by+08:00
. Meaning, it is04:52:06
in Greenwich.
哪个是正确的?A或B?你知道吗
12:52:06+08:00
通常意味着它是一个时区中比UTC早8小时的给定时间。所以B是正确的。你知道吗但是,您生成的时间字符串不正确。
time.gmtime()
只返回一个时间,没有任何时区,您告诉datetime()
时间在Manilla时区。所以对于这种特殊情况,A是正确的。你知道吗注意:
datetime.strptime
不适用于时区您可以为datetime.strftime
使用%z
格式代码,但不能与strptime
一起使用。如果需要这样做,请使用dateutil
,请参见How to parse dates with -0400 timezone string in python?相关问题 更多 >
编程相关推荐