views:

35

answers:

1

Hi

Ok, I am not sure how to ask this question so that it can be understood...

I need to store some schedules on a server (Ruby on Rails) from a iPhone. On the iPhone device the time is relative to the device timezone settings, but on the server I need to store the data in the servers local timezone correctly so that the scheduled event will be fired at the correct time.

My idea was this: - Convert iPhone time to UTC timezone time, and send to server - On server, read all stored timestamps as UTC timezone - For data received from server, convert from UTC timezone time to local timezone time

Here is my code to convert to UTC time:

// First the NSDateformatter
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
    [dateFormatter setLocale:enUSPOSIXLocale];
    [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

// Now the conversion from local timezone to UTC
    NSTimeZone *srcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    NSTimeZone *destTimeZone = [NSTimeZone systemTimeZone];
    NSInteger sourceGMTOffset = [srcTimeZone secondsFromGMTForDate:srcDate];
    NSInteger destinationGMTOffset = [destTimeZone secondsFromGMTForDate:srcDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate *destDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:srcDate] autorelease];

The problem is, that if I try and change timezone on my iPhone in between two posts to the server, there is a time difference, so something is wrong.

So my question(s) is: 1) Is this a good approach? 2) What is wrong with my code?

Thank you Søren

+1  A: 

You shouldn't be fiddling with time zones except for the UI.

Instead, you should save all you times using timeIntervalSinceReferenceDate which is the NSDate primitive method that returns the number of elapsed seconds, to the microsecond, that have elapsed since 1 January 2001, GMT and the current GMT date and time. Save that number to the server and then you can convert it to dates with timezones as needed for display.

Data and time programming is deceptively complex. Keep everything as simple and fixed as possible in the core logic.

TechZen
So if someone in London submits 19:00 to the server and someone in Denmark submits 20:00 (there is one hour difference), these two NSDate objects has the same timeIntervalSinceReferenceDate number give or take some seconds?
Neigaard
Also on the server I need a real timestamp, as I need to get all schedules from the database that needs to be fired for a given time, so on the server I still need to store the timestamp in the database.
Neigaard
GMT/UTC is used as the basis for all time calculations. You have to have a fixed reference in order to translate between any two timezones and GMT/UTC is the same everywhere. Timezones are just translations of GMT/UTC to local astronomical conditions e.g. sunrise. It's always best to store times in GMT and then translate to local time as needed. Look at how the military handles time. That is what you need to duplicate.
TechZen