ios - extract NSTimeZone from string like '+-hh:mm' -


in app download data json api contains entry local time:

"local_date" : "2015-07-08t13:18:14+02:00" 

the json data parsed nsdictionary* information:

nsdateformatter* dateformatter= [nsdateformatter new]; dateformatter.timezone = [nstimezone timezonewithname:@"utc"]; dateformatter.dateformat = @"yyyy-mm-dd't'hh:mm:ssz"; nsdate *date = [self.dateformatter datefromstring:information[@"local_date"]]; 

the date correctly set in utc (which need computations) display local data in user interface need display date in correct time zone.

my question ist: how can extract nstimezone string? looking nstimezone* timezone = [nstimezone parsefromstring:@"+02:00"];

i've read documentation of nsdateformatter, nscalendar , nstimezone didn't find out how timezone string mine.

thanks in advance!

again: when "in local time", mean local returned string, not user? sent date germany, have enough information both turn nsdate and create nsdateformatter recreated original string output wanted?

first of all: time zone isn't in string supplied. says +02:00 tells offset gmt. doesn't tell time zone. e.g. offset of +02:00 right sast, south african time zone, eet, eastern european time zone, cat, central african time zone, etc. there's therefore no direct way map offset zone.

supposing want preserve offset can apply later, luk2302's suggestion in right direction i'd go opposite way around (also i'd pay attention qa1480):

nsstring *time = @"2015-07-08t13:18:14+02:00"; // or whatever  nsdateformatter *dateformatter= [nsdateformatter new]; dateformatter.locale = [nslocale localewithlocaleidentifier:@"en_us_posix"];  dateformatter.dateformat = @"yyyy-mm-dd't'hh:mm:ssz"; nsdate *correctdate = [dateformatter datefromstring:time];  nsdate *datewithoutoffset = correctdate; nsrange rangeofplus = [time rangeofstring:@"+"]; if(rangeofplus.location != nsnotfound) {     dateformatter.timezone = [nstimezone timezonewithname:@"utc"];     dateformatter.dateformat = @"yyyy-mm-dd't'hh:mm:ss";     datewithoutoffset = [dateformatter datefromstring:[time substringtoindex:rangeofpls.location]];      }  nslog(@"offset in date %0.0f seconds", [datewithoutoffset timeintervalsincedate:correctdate]); 

because you're doing obtuse , unusual — normal apps deal fixed date format strings, dates without time zones (as within nsdates), , displaying things in user's local time — you'll need store , deal offsets manually.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -