Timezone/DST agnostic timestamps with NSDate
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 do开发者_StackOverflow中文版cs 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.
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.
精彩评论