使用 NSDate 与时区/DST 无关的时间戳

发布于 2024-09-11 12:18:26 字数 349 浏览 2 评论 0原文

我正在创建将发送到服务器的对象,并且我试图确保考虑到时区和夏令时问题,因此我希望我的时间戳为“自 1970 年以来的秒数”,而不管时区等。

[NSDate timeintervalSinceReferenceDate] 对此足够吗?在文档中,我知道它说的是自 January 1 1970 00:00:00 GMT 以来的时间,这就是我想要的,但是 NSDate 如何处理时区?如果用户在其设备设置为 EST 的情况下发送对象,然后将其设备更改为 PST,这会对时间戳产生什么样的影响?我的目标是让这没有什么区别,而且我似乎无法从文档中收集到这一点。

日期编程是一个棘手的问题,我想确保我能做对。

I'm creating objects which will be sent to a server, and I'm trying to ensure I account for timezone and Daylight Savings issues, so I want my timestamps to be "seconds since 1970" regardless of timezone, etc.

Is [NSDate timeintervalSinceReferenceDate] sufficient for this? In the docs I know it says time since January 1 1970 00:00:00 GMT, which is what I want, but how does NSDate handle timezones? If a user sends a object with their device set to EST and then changes their device to PST, what sort of implications will this have on the timestamps? My goal is for this to make no difference, and I can't seem to glean that from the documentation.

Date programming is a tricky problem and I want to make sure I get it right.

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

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

发布评论

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

评论(1

旧时浪漫 2024-09-18 12:18:26

文档说 timeIntervalSinceReferenceDate 给出自 2001 年以来的时间。timeIntervalSince1970 返回自 1970 年以来的时间。

但无论哪种方式,NSDate 都描述了一个时间点。用户的时区仅影响向用户显示日期的方式(例如通过 NSDateFormatter)。所以你不必担心时区。

但也许你确实需要担心用户的时钟出错。用户可以将时钟设置为他们想要的任何值,因此您不应该信任它来完成​​任何关键任务。请改用服务器时钟。

The documentation says timeIntervalSinceReferenceDate gives the time since 2001. timeIntervalSince1970 returns the time since 1970.

But either way, NSDate describes a point in time. The user's time zone only affects the way the date is displayed to the user (e.g. by NSDateFormatter). So you don't have to worry about time zones.

But maybe you do have to worry about the user's clock being wrong. The user can set their clock to anything they want, so you shouldn't trust it for anything mission-critical. Use the server clock instead.

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