时区/ DST不可知时间戳与NSDate [英] Timezone/DST agnostic timestamps with NSDate
问题描述
我正在创建将发送到服务器的对象,并且我想确保我考虑到时区和夏令时问题,因此我希望我的时间戳为自1970年以来的秒数,而不考虑时区等。
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.
是 [NSDate timeintervalSinceReferenceDate]
足够吗?在docs我知道它说自从1970年1月1日00:00:00 GMT时间,这是我想要的,但NSDate如何处理时区?如果用户发送一个设备设置为EST的对象,然后将其设备更改为PST,那么这对时间戳有什么影响?我的目标是让这没有什么区别,我似乎不知道从文档中。
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.
推荐答案
文档说 timeIntervalSinceReferenceDate
timeIntervalSince1970
返回自1970年以来的时间。
The documentation says timeIntervalSinceReferenceDate
gives the time since 2001. timeIntervalSince1970
returns the time since 1970.
但是无论如何, NSDate
描述了一个时间点。用户的时区仅影响向用户显示日期的方式(例如,通过NSDateFormatter)。所以你不必担心时区。
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.
这篇关于时区/ DST不可知时间戳与NSDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!