为什么 Swift 的日期时间间隔忽略闰秒?

发布于 2025-01-10 22:49:39 字数 168 浏览 2 评论 0原文

2012 年 6 月 30 日有闰秒。因此它持续了 86401 秒。
但是,在以下 Swift 代码中,timeInterval 为 86400。
怎么会?

30 June 2012 had a leap second. As such it lasted 86401 seconds.
However, in the following Swift code, timeInterval is 86400.
How come? ????

let formatter = ISO8601DateFormatter()
let date = formatter.date(from: "2012-06-30T00:00:00Z")!
let dayAfter = formatter.date(from: "2012-07-01T00:00:00Z")!
let timeInterval = date.distance(to: dayAfter)

(ps: in what context did I write this code? It was just about having fun with code. I was trying to write confusing code results, but I was disappointed by this attempt at trying to be smart)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2025-01-17 22:49:39

Apple 日期和时间 API 和系统实现的基础是 ICU 库,以及大多数您看到的结果取决于 ICU 的行为。值得注意的是,ICU 不支持也不考虑闰秒的存在:

由于历史原因,参考点是英国格林威治。格林威治当地时间被称为格林威治标准时间,或 GMT。 (这与通用协调时间(UTC)类似,但不完全相同。我们在 ICU 中互换使用这两个术语,因为 ICU 不关心闰秒或历史行为。)

因此,Apple 平台也不关心在 API 中以任何方式表示历史(或未来)闰秒;系统通过 NTP有效处理闰秒,并简单地设置时钟适当地,但是像这样查询闰秒不会产生任何结果。

The basis for Apple date and time APIs and system implementations is the ICU library, and the underpinnings for most of the results you see depends on ICU behavior. Notably, ICU doesn't support or consider the existence of leap seconds:

For historical reasons, the reference point is Greenwich, England. Local time in Greenwich is referred to as Greenwich Mean Time, or GMT. (This is similar, but not precisely identical, to Universal Coordinated Time, or UTC. We use the two terms interchangeably in ICU since ICU does not concern itself with either leap seconds or historical behavior.)

As such, Apple platforms also don't represent historical (or future) leap seconds in any way in APIs; systems effectively deal with leap seconds via NTP, and simply set their clocks appropriately, but querying for leap seconds like this won't yield anything.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文