是否有 "local" 时间戳?
Is there a "local" timestamp?
我可以从 UTC 时间戳转换,例如1417392000 到包括夏令时的本地日期时间对象,但是当我尝试将本地日期时间对象转换为 "local timestamp" 时,我得到与以前相同的 UTC 时间戳。
我是不是在兜圈子,他们是一样的?我应该从传入的 UTC 时间戳中保存 "local timestamp"。
这是我的代码
print("UTC timestamp %d" % hour[0])
day = self.get_day(hour)
month = self.get_month(hour)
year = self.get_year(hour)
tz = pytz.timezone('Europe/Stockholm')
utc_dt = datetime.utcfromtimestamp(int(hour[0])).replace(tzinfo=pytz.utc)
print("UTC datetime %s" % utc_dt)
dt = tz.normalize(utc_dt.astimezone(tz))
print("STO datetime %s" % dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print("STO ts %d" % int(time.mktime(dt.timetuple())))
print("STO timestamp %d" % utc_dt.astimezone(tz).timestamp())
day = int(dt.strftime('%d'))
month = int(dt.strftime('%m'))
year = int(dt.strftime('%Y'))
输出
UTC timestamp 1417395600
UTC datetime 2014-12-01 01:00:00+00:00
STO datetime 2014-12-01 02:00:00 CET+0100
STO ts 1417395600
STO timestamp 1417395600
所有"timestamps"(即时间的整数表示)都相同。是否可以制作 "local timestamp" ?数据类型应该是一个时间戳,它是一个本地时间的数字。
Unix time (also known as POSIX time or Epoch time) is a system for
describing instants in time, defined as the number of seconds that
have elapsed since 00:00:00 Coordinated Universal Time (UTC),
Thursday, 1 January 1970
因此无论您在哪个时区,纪元将始终以 UTC 时区计算。
出于显示目的,您可以将时间戳转换为本地时间,否则纪元的内部表示将始终采用 UTC 时区
正式时间戳
Unix timestamp 通常指自 UTC 纪元以来的秒数。该值对于时区是不变的。它允许全局事件排序但丢失时区信息。
保留时区
要保留时区信息,标准化格式为 RFC3339、Internet 上的日期和时间:时间戳。这只是一种编码日期+时间+时区的标准化格式。一些例子:
1985-04-12T23:20:50.52Z
1996-12-19T16:39:57-08:00
1990-12-31T23:59:60Z
1990-12-31T15:59:60-08:00
在不保留时区的情况下对时区进行规范化
不过,这可能取决于您的要求。我曾经想记录一些与 local-time-of-day 相关的事件,并且不介意丢失时区信息。我对本地时区中 1970-01-01T00:00:00 的时间戳进行了标准化。我现在对此有点不好意思,因为我认为它很容易引起混淆。
import time
# Number of seconds since 1970-01-01T00:00+LTZ (current timezone).
# unix timestamp - timezone offset in seconds
timestamp_localized = time.time() - time.mktime(time.gmtime(0))
然而,可以通过注意 Python 具有一些本地时间和 UTC 特定功能来简化此语法,也许会失去清晰度。
import time
import calendar
# Number of seconds since 1970-01-01T00:00+LTZ (current timezone).
# Interpret local date and time as if it were UTC
timestamp_localized = calendar.timegm(time.localtime())
两者的区别在于日历转换截断到秒,而差值计算包括小数秒。
我可以从 UTC 时间戳转换,例如1417392000 到包括夏令时的本地日期时间对象,但是当我尝试将本地日期时间对象转换为 "local timestamp" 时,我得到与以前相同的 UTC 时间戳。
我是不是在兜圈子,他们是一样的?我应该从传入的 UTC 时间戳中保存 "local timestamp"。
这是我的代码
print("UTC timestamp %d" % hour[0])
day = self.get_day(hour)
month = self.get_month(hour)
year = self.get_year(hour)
tz = pytz.timezone('Europe/Stockholm')
utc_dt = datetime.utcfromtimestamp(int(hour[0])).replace(tzinfo=pytz.utc)
print("UTC datetime %s" % utc_dt)
dt = tz.normalize(utc_dt.astimezone(tz))
print("STO datetime %s" % dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print("STO ts %d" % int(time.mktime(dt.timetuple())))
print("STO timestamp %d" % utc_dt.astimezone(tz).timestamp())
day = int(dt.strftime('%d'))
month = int(dt.strftime('%m'))
year = int(dt.strftime('%Y'))
输出
UTC timestamp 1417395600
UTC datetime 2014-12-01 01:00:00+00:00
STO datetime 2014-12-01 02:00:00 CET+0100
STO ts 1417395600
STO timestamp 1417395600
所有"timestamps"(即时间的整数表示)都相同。是否可以制作 "local timestamp" ?数据类型应该是一个时间戳,它是一个本地时间的数字。
Unix time (also known as POSIX time or Epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970
因此无论您在哪个时区,纪元将始终以 UTC 时区计算。
出于显示目的,您可以将时间戳转换为本地时间,否则纪元的内部表示将始终采用 UTC 时区
正式时间戳
Unix timestamp 通常指自 UTC 纪元以来的秒数。该值对于时区是不变的。它允许全局事件排序但丢失时区信息。
保留时区
要保留时区信息,标准化格式为 RFC3339、Internet 上的日期和时间:时间戳。这只是一种编码日期+时间+时区的标准化格式。一些例子:
1985-04-12T23:20:50.52Z
1996-12-19T16:39:57-08:00
1990-12-31T23:59:60Z
1990-12-31T15:59:60-08:00
在不保留时区的情况下对时区进行规范化
不过,这可能取决于您的要求。我曾经想记录一些与 local-time-of-day 相关的事件,并且不介意丢失时区信息。我对本地时区中 1970-01-01T00:00:00 的时间戳进行了标准化。我现在对此有点不好意思,因为我认为它很容易引起混淆。
import time
# Number of seconds since 1970-01-01T00:00+LTZ (current timezone).
# unix timestamp - timezone offset in seconds
timestamp_localized = time.time() - time.mktime(time.gmtime(0))
然而,可以通过注意 Python 具有一些本地时间和 UTC 特定功能来简化此语法,也许会失去清晰度。
import time
import calendar
# Number of seconds since 1970-01-01T00:00+LTZ (current timezone).
# Interpret local date and time as if it were UTC
timestamp_localized = calendar.timegm(time.localtime())
两者的区别在于日历转换截断到秒,而差值计算包括小数秒。