iphone - NSDateformatter Incorrect Time after daylight saving hours -


i have function produces current date depending on users location. provides correct date , time after daylight saving changes here in uk.

however when try create string date seems add hour on me...

e.g date stored in coredata 1/3/2013 12:00 when converted string appears 1/3/2013 13:00. ever change locale , timezones don't seem make difference.

any ideas how can fix this? might worth noting "local date" function have never creates date time correct timezone.

code:

+(nsdate*)localdate{  nsdate* date = [nsdate date];  nstimezone* sourcetimezone = [nstimezone timezonewithabbreviation:@"gmt"]; nstimezone* destinationtimezone = [nstimezone localtimezone];  nsinteger sourcegmtoffset = [sourcetimezone secondsfromgmtfordate:date]; nsinteger destinationgmtoffset = [destinationtimezone secondsfromgmtfordate:date]; nstimeinterval interval = destinationgmtoffset - sourcegmtoffset; return [nsdate datewithtimeinterval:interval sincedate:date]; 

}

formatted code:

-(nsstring*)datewithtime{ nsdateformatter*formatter = [[nsdateformatter alloc]init];  [formatter setdateformat:@"dd"]; nsinteger day = [[formatter stringfromdate:self]integervalue]; nsinteger fullday = day; nsstring* strday = @""; if(day>29){     day = day - 30; } else if(day>19){     day = day - 20; }  else if(day>9){     day = day - 10; }  if(fullday ==11){     strday = @"th"; } else if(fullday ==12){     strday = @"th"; } else if(fullday ==13){     strday = @"th"; } else if(day == 1){     strday = @"st"; } else if(day ==2){     strday = @"nd"; } else if(day ==3){     strday = @"rd"; } else{     strday = @"th"; }  [formatter setdateformat:@"mmmm"]; nsstring* month = [formatter stringfromdate:self];  [formatter setdateformat:@"hh:mm"]; nsstring* time = [formatter stringfromdate:self];  return [nsstring stringwithformat:@"%u%@ %@ %@ - %@",fullday,strday,month,[self yearstring],time];  } 

edit: nsdatecomponants returns incorrect time..

 nscalendar *gregorian = [nscalendar currentcalendar]; nsdatecomponents *datecomponents = [gregorian components:(nshourcalendarunit  |  nsminutecalendarunit | nssecondcalendarunit) fromdate:self]; 

please note printing nsdate's in console print date in utc.

simply store [nsdate date] in database. when user never changed it's time zone, reading date database , format return correct result.

nsdate objects in core data stored unix timestamp time zone utc. when reading , formatting date object current time zone of user applied.

when storing dates core data , having dependences different time zones way format dates correctly store appropriate time zone (name of time zone string) well. when reading , formatting date object have apply stored time zone dateformatter. you'll correct results.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -