将本地 unix 时间转换为 UTC unix 时间的问题
Problem in converting local unix time to UTC unix time
我正在尝试将我本地系统上的 unix 时间转换为 unix 格式的 UTC 时间,但在转换之后,最终时间将减少 +1 小时。
为此,我编写了以下代码。
from dateutil import tz
import time
from time
import mktime
now_time = time.time()
print('current local unix time', now_time)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now_time)))
to_zone = tz.tzutc()
from_zone = tz.tzlocal()
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now_time))
utc = datetime.strptime(str(t), '%Y-%m-%d %H:%M:%S')
utc = utc.replace(tzinfo=from_zone)
central = utc.astimezone(to_zone)
print('Converted to UTC ',central)
unix_secs = mktime(central.timetuple())
print('Central unix time ',unix_secs)
print('central unix time to dattime ', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(unix_secs)))
输出结果如下
current local unix time 1563985835.3707478
2019-07-24 12:30:35
Converted to UTC 2019-07-24 16:30:35+00:00
Central unix time 1564003835.0
central unix time to dattime 2019-07-24 17:30:35
有人可以告诉我我做错了什么吗?我该如何解决这个问题?
我猜你在使用 time.strftime
和之后 datetime.strptime
或使用 mktime
将时间戳转换为 datetime
实例时错误地使用 TZ 信息转换日期时间。
无论如何,有更简单的方法来实现你想要的:
from datetime import datetime, timezone
# Get current timestamp
now_timestamp = time.time()
>> 1563987839.054703
# Get datetime of that timestamp but already in UTC.
# This preserves TZ information and allows
# to correctly do transition to the timestamp again.
utc_time = datetime.utcfromtimestamp(now_timestamp)
>> datetime.datetime(2019, 7, 24, 17, 3, 59, 54703)
# Convert UTC datetime back to timestamp
utc_timestamp = utc_time.timestamp()
>> 1563977039.054703
# Verify that this timestamp is indeed UTC
# I am in UTC+3 timezone now
datetime.now()
>> datetime.datetime(2019, 7, 24, 20, 4, 10, 500229)
datetime.fromtimestamp(utc_timestamp)
>> datetime.datetime(2019, 7, 24, 17, 3, 59, 54703)
Unix 时间戳始终采用 UTC。
是"the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, UTC"。
换句话说,就是时长。在何处进行此计算一点也不重要。减法在宇宙中无处不在。持续时间与时区完全无关。
我正在尝试将我本地系统上的 unix 时间转换为 unix 格式的 UTC 时间,但在转换之后,最终时间将减少 +1 小时。
为此,我编写了以下代码。
from dateutil import tz
import time
from time
import mktime
now_time = time.time()
print('current local unix time', now_time)
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now_time)))
to_zone = tz.tzutc()
from_zone = tz.tzlocal()
t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now_time))
utc = datetime.strptime(str(t), '%Y-%m-%d %H:%M:%S')
utc = utc.replace(tzinfo=from_zone)
central = utc.astimezone(to_zone)
print('Converted to UTC ',central)
unix_secs = mktime(central.timetuple())
print('Central unix time ',unix_secs)
print('central unix time to dattime ', time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(unix_secs)))
输出结果如下
current local unix time 1563985835.3707478
2019-07-24 12:30:35
Converted to UTC 2019-07-24 16:30:35+00:00
Central unix time 1564003835.0
central unix time to dattime 2019-07-24 17:30:35
有人可以告诉我我做错了什么吗?我该如何解决这个问题?
我猜你在使用 time.strftime
和之后 datetime.strptime
或使用 mktime
将时间戳转换为 datetime
实例时错误地使用 TZ 信息转换日期时间。
无论如何,有更简单的方法来实现你想要的:
from datetime import datetime, timezone
# Get current timestamp
now_timestamp = time.time()
>> 1563987839.054703
# Get datetime of that timestamp but already in UTC.
# This preserves TZ information and allows
# to correctly do transition to the timestamp again.
utc_time = datetime.utcfromtimestamp(now_timestamp)
>> datetime.datetime(2019, 7, 24, 17, 3, 59, 54703)
# Convert UTC datetime back to timestamp
utc_timestamp = utc_time.timestamp()
>> 1563977039.054703
# Verify that this timestamp is indeed UTC
# I am in UTC+3 timezone now
datetime.now()
>> datetime.datetime(2019, 7, 24, 20, 4, 10, 500229)
datetime.fromtimestamp(utc_timestamp)
>> datetime.datetime(2019, 7, 24, 17, 3, 59, 54703)
Unix 时间戳始终采用 UTC。
是"the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970, UTC"。
换句话说,就是时长。在何处进行此计算一点也不重要。减法在宇宙中无处不在。持续时间与时区完全无关。