为什么将此日期时间转换为 Epoch 关闭 30 分钟?
Why is converting this datetime to Epoch off by 30 minutes?
我有以下表示 UTC 时间戳的字符串:2017-12-03T20:38:00.971261Z
我想将它转换成 Posix 时间戳(IE:自纪元以来的秒数)
使用这个在线转换器 (https://www.epochconverter.com/) 我知道答案是 1512333480
但是当我执行以下代码时,结果偏离了 1800 秒 -- 30 分钟:
>>> temp_time1 = datetime.datetime.strptime('2017-12-03T20:38:00.971261Z', '%Y-%m-%dT%H:%M:%S.%fZ')
>>> ctime = int(datetime.datetime(temp_time1.year,
temp_time1.month,
temp_time1.day,
temp_time1.hour,
temp_time1.minute,
temp_time1.second,
temp_time1.microsecond,
pytz.timezone('Europe/London')).strftime('%s'))
>>> print ctime
1512351480
有人知道我在这里错过了什么吗??
您创建了一个新时间戳并将其置于Europe/London时区。这与 UTC 不同。 PyTZ 数据库中的 Europe/London 时区包括 历史 偏移量,这些会影响 datetime.datetime()
解释时区的方式。
只需在您已经从字符串解析的 datetime
对象上使用 datetime.timestamp()
method:
>>> import datetime
>>> temp_time1 = datetime.datetime.strptime('2017-12-03T20:38:00.971261Z', '%Y-%m-%dT%H:%M:%S.%fZ')
>>> temp_time1.timestamp()
1512333480.971261
您原来的 temp_time1
日期时间对象与时区无关,因此 timestamp()
对象已经假定无需进行时区转换。
如果出于任何原因必须首先应用 Europe/London
时区,那么至少使用 timezone.localize()
method 来应用正确的偏移量:
>>> import pytz
>>> pytz.timezone('Europe/London').localize(temp_time1)
datetime.datetime(2017, 12, 3, 20, 38, 0, 971261, tzinfo=<DstTzInfo 'Europe/London' GMT0:00:00 STD>)
>>> pytz.timezone('Europe/London').localize(temp_time1).timestamp()
1512333480.971261
见How to make an unaware datetime timezone aware in python
对于 Python 2 和 Python 3.0、3.1 或 3.2,其中 datetime.timestamp()
不可用,减去纪元日期:
>>> (temp_time1 - datetime.datetime(1970, 1, 1)).total_seconds()
1512333480.971261
在处理时区感知 datetime
实例时添加 UTC
时区:
>>> (pytz.timezone('Europe/London').localize(temp_time1) - datetime.datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
1512333480.971261
合并成一个函数:
def datetime_to_timestamp(dt, epoch=datetime.datetime(1970, 1, 1)):
if dt.tzinfo is not None:
epoch = pytz.utc.localize(epoch)
return (dt - epoch).total_seconds()
我有以下表示 UTC 时间戳的字符串:2017-12-03T20:38:00.971261Z
我想将它转换成 Posix 时间戳(IE:自纪元以来的秒数)
使用这个在线转换器 (https://www.epochconverter.com/) 我知道答案是 1512333480
但是当我执行以下代码时,结果偏离了 1800 秒 -- 30 分钟:
>>> temp_time1 = datetime.datetime.strptime('2017-12-03T20:38:00.971261Z', '%Y-%m-%dT%H:%M:%S.%fZ')
>>> ctime = int(datetime.datetime(temp_time1.year,
temp_time1.month,
temp_time1.day,
temp_time1.hour,
temp_time1.minute,
temp_time1.second,
temp_time1.microsecond,
pytz.timezone('Europe/London')).strftime('%s'))
>>> print ctime
1512351480
有人知道我在这里错过了什么吗??
您创建了一个新时间戳并将其置于Europe/London时区。这与 UTC 不同。 PyTZ 数据库中的 Europe/London 时区包括 历史 偏移量,这些会影响 datetime.datetime()
解释时区的方式。
只需在您已经从字符串解析的 datetime
对象上使用 datetime.timestamp()
method:
>>> import datetime
>>> temp_time1 = datetime.datetime.strptime('2017-12-03T20:38:00.971261Z', '%Y-%m-%dT%H:%M:%S.%fZ')
>>> temp_time1.timestamp()
1512333480.971261
您原来的 temp_time1
日期时间对象与时区无关,因此 timestamp()
对象已经假定无需进行时区转换。
如果出于任何原因必须首先应用 Europe/London
时区,那么至少使用 timezone.localize()
method 来应用正确的偏移量:
>>> import pytz
>>> pytz.timezone('Europe/London').localize(temp_time1)
datetime.datetime(2017, 12, 3, 20, 38, 0, 971261, tzinfo=<DstTzInfo 'Europe/London' GMT0:00:00 STD>)
>>> pytz.timezone('Europe/London').localize(temp_time1).timestamp()
1512333480.971261
见How to make an unaware datetime timezone aware in python
对于 Python 2 和 Python 3.0、3.1 或 3.2,其中 datetime.timestamp()
不可用,减去纪元日期:
>>> (temp_time1 - datetime.datetime(1970, 1, 1)).total_seconds()
1512333480.971261
在处理时区感知 datetime
实例时添加 UTC
时区:
>>> (pytz.timezone('Europe/London').localize(temp_time1) - datetime.datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
1512333480.971261
合并成一个函数:
def datetime_to_timestamp(dt, epoch=datetime.datetime(1970, 1, 1)):
if dt.tzinfo is not None:
epoch = pytz.utc.localize(epoch)
return (dt - epoch).total_seconds()