NSDate dateFromString, how to parse 'around' UTC, GMT and User locale?

Posted by RickiG on Stack Overflow See other posts from Stack Overflow or by RickiG
Published on 2010-03-28T22:04:30Z Indexed on 2010/03/28 22:43 UTC
Read the original article Hit count: 699

Filed under:
|

Hi

I parse some values from an xml file. There is a @"25-12-2010'T'23:40:00"string with the time and date and there is a string with the GMT offset like this @"+0200". So the above time is the 25. of December 23:40:00 in timeZone +0200 GMT. (or 21:40 UTC) I have lots of these dates with different GMT offsets. I have to display these dates as they are, i.e. They must not be changed to fit the locale of the user. So if time 1: is 22:45 +0500 then that is what I must show the user, even if the user is in a different timezone.

I have all sorts of trouble with displaying, calculating and parsing these strings.

If I use a dateFormatter and dateFromString the user specific GMT info will be included in the resulting NSDate meaning the above will be saved as 23:40:00 +0100 GMT because that is my phones setting and maybe 23:40:00 -0400 on a user from new New York's phone.

When I subsequently do subtraction, addition and comparisons between these dates I have to keep the GMT offset around and everything gets worse if the phone switches locale settings, from when the date was parsed to when the date is displayed...

Is there a way for me to extract this date from the string as UTC, then save it as an interval instead of an actual (timezone dependent) date. I know that is how dates are always saved internally. But I can't figure out how to do it with the separate GMT string and taking into account the users locale.

Cheers

© Stack Overflow or respective owner

Related posts about nsdate

Related posts about iphone