[[NSDate date]timeIntervalSince1970] seems to be 1hr behind
I am writing an app that communicates with some web services. When a variable is updated on the server, it will return a unix timestamp to tell me when it was updated. I can then compare it using:
[[NSDate date]timeIntervalSince1970]
to keep things in sync.
开发者_高级运维The system time on the iPhone is correct. But unix time seems to be 1hr behind the server(and system time as shown on the clock). Why is this happening? Do I need to set a time zone?
timeIntervalSince1970
is defined to return:
The interval between the receiver and the reference date, 1 January 1970, GMT.
Is it possible you're comparing to a time that is defined to be from 1 January 1970 in some other timezone? If so, the easiest thing to do is to use timeIntervalSinceDate:
rather than timeIntervalSince1970
, possibly having used NSDate's dateByAddingTimeInterval:
to move from the reference date in GMT to the reference date in your ideal time zone.
You can use NSTimeZone's secondsFromGMTForDate:
property if you don't want to hard code things.
As it can be understood from your question, the problem is in the timestamp that returned by server. So the real question is if you have an access to that server. Or do you know in what timezone it is. If first, update time on server. Second - calculate difference between timezones (you say it's a hour) and add it to your NSDate instance.
You also may use NSCalendar as it have -(void)setTimeZone:(NSTimeZone *)timezone;
method.(thought it may be overhead for your needs)
精彩评论