为什么 Swift 的日期时间间隔忽略闰秒?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Apple 日期和时间 API 和系统实现的基础是 ICU 库,以及大多数您看到的结果取决于 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:
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.