gpt4 book ai didi

cocoa-touch - 从 1600 年到 NSDate 的日期?

转载 作者:行者123 更新时间:2023-12-01 18:33:39 25 4
gpt4 key购买 nike

我有一个存储为自 1600 年 1 月 1 日以来我需要处理的天数的日期。这是我需要在我的应用程序中多次阅读的遗留日期格式。

以前,我一直在创建日历、空日期组件和根日期,如下所示:

self.gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar
] autorelease];
id rootComponents = [[[NSDateComponents alloc] init] autorelease];
[rootComponents setYear: 1600];
[rootComponents setMonth: 1];
[rootComponents setDay: 1];
self.rootDate = [gregorian dateFromComponents: rootComponents];
self.offset = [[[NSDateComponents alloc] init] autorelease];

然后,为了稍后将整数转换为日期,我使用这个:
[offset setDay: theLegacyDate];
id eventDate = [gregorian dateByAddingComponents: offset
toDate: rootDate
options: 0];

(我从不在其他任何地方更改任何偏移值。)

问题是我的 rootDate 时间不同在 iOS 与 Mac OS X 上。在 Mac OS X 上,我要到午夜了。在 iOS 上,我得到 8:12:28。 (到目前为止,这似乎是一致的。)当我添加我的天数后,奇怪的时间仍然存在。

操作系统 |遗留日期 |根日期 |事件日期
======== | ========== | ===========================|======================= ===
Mac OS X | 143671 | 1600-01-01 00:00:00 -0800 | 1993-05-11 00:00:00 -0700
iOS | 143671 | 1600-01-01 08:12:28 +0000 | 1993-05-11 07:12:28 +0000

在我的产品之前的版本中,我并不关心时间;现在我知道了。为什么在 iOS 上出现奇怪的时间,我该怎么办? (我假设小时差是 DST。)

我尝试将 rootComponents 的小时、分钟和秒设置为 0。这没有影响。如果我将它们设置为 0 以外的值,它会将它们添加到 8:12:28。我一直想知道这是否与闰秒或其他累积时钟变化有关。

或者这完全是在 iOS 上使用的错误方法?

最佳答案

我想你对闰秒/累积时钟变化解释时间问题是正确的。您正在处理的日期实际上是过去,还是纯粹是一个任意的时代?

无论哪种情况,您都可以尝试定义一个更接近现代的新纪元(例如, cocoa 纪元)。计算新纪元和旧纪元之间的天差,并将其保存为常数。当您需要处理日期时,将此增量应用于日期,然后使用您现有的 NSCalendar 技术,但使用新纪元而不是旧纪元。这有望避免您看到的时钟漂移问题。

关于cocoa-touch - 从 1600 年到 NSDate 的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4664582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com