简体   繁体   English

自 1970 年 1 月 1 日以来经过了多少秒,包括闰秒?

[英]How many seconds elapsed since 01/01/1970, leap-seconds included?

The Unix timestamp given by: Unix 时间戳由下式给出:

int(time.time())

gives the number of seconds elapsed since 01/01/1970, without leap-seconds.给出自 1970 年 1 月 1 日以来经过的秒数,没有闰秒。

Just out of curiosity, how to get the true number of seconds elapsed since this date, leap-seconds included?只是出于好奇,如何获得自该日期以来经过的真实秒数,包括闰秒? (ie the distance between these two events on a time axis) (即时间轴上这两个事件之间的距离)

Notes:笔记:

  • Example: the timestamp range 867715190.000.. 867715202.000 represents a "real-life duration" of 13 seconds (measured with a timer) because there was a leap-second this day of 1997, whereas the Unix timestamp has only increased of +12.示例:时间戳范围867715190.000.. 867715202.000表示 13 秒的“实际持续时间”(用计时器测量),因为 1997 年的这一天有一个闰秒,而 Unix 时间戳增加了 +12。

  • Example 2: the real-life time elapsed between 1/1/1970 and 1/1/2020 (12 leap-years in this 50-year interval) is (365*50+12)*24*3600 + number_leap_seconds and not (365*50+12)*24*3600 .示例 2:从 1970 年 1 月 1 日到 2020 年 1 月 1 日(这 50 年间隔中有 12 个闰年)之间经过的实际时间是(365*50+12)*24*3600 + number_leap_seconds不是(365*50+12)*24*3600 But we see datetime.datetime.utcfromtimestamp((365*50+12)*24*3600) is 2020-01-01 00:00:00 , so obviously, the leap-seconds haven't been taken in consideration.但是我们看到datetime.datetime.utcfromtimestamp((365*50+12)*24*3600)2020-01-01 00:00:00 ,所以显然没有考虑闰秒。

  • Linked to What does python return on the leap second , and Unix time and leap seconds链接到什么 python 在闰秒返回,以及Unix 时间和闰秒

The number of SI seconds between any two UTC timestamps since 1972-01-01 requires access to the list of leap seconds which have been introduced into UTC.自 1972-01-01 以来任何两个 UTC 时间戳之间的 SI 秒数需要访问已引入 UTC 的闰秒列表。 This list is available as part of the IANA tzdata distribution and it can also be obtained from other sources.此列表作为 IANA tzdata 分发的一部分提供,也可以从其他来源获得。

Caution is required because the number of SI seconds between what was known as 1970-01-01 and 1972-01-01 is 2x365x24x60x60 + 1.999918 SI seconds because at 1970 the official time was determined not by cesium atoms but by actually measuring the rotation of the earth, so the official seconds were mean solar seconds not SI seconds.需要小心,因为在 1970-01-01 和 1972-01-01 之间的 SI 秒数是 2x365x24x60x60 + 1.999918 SI 秒,因为在 1970 年,官方时间不是由铯原子确定的,而是通过实际测量的旋转来确定的。地球,所以官方秒是平均太阳秒而不是 SI 秒。

I ran into this same issue, and you can work around it by using "right" time:我遇到了同样的问题,您可以使用“正确”的时间来解决它:

import os; import time; import datetime;
tai_epoch=datetime.datetime(1970, 1, 1, 0, 0, 10)
timezone=time.tzname[0];os.environ['TZ']='right/UTC';time.tzset()
now=datetime.datetime.utcnow()
leap_seconds=int(now.timestamp())-int((now-tai_epoch).total_seconds())
right_time=int(now.timestamp())+leap_seconds
os.environ['TZ']=timezone;time.tzset()

You can compare this against the regular UTC time like so:您可以将其与常规 UTC 时间进行比较,如下所示:

utc_time=int(time.time())
print(right_time-utc_time)

which, at the moment, will give you 37 .目前,它会给你37

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM