objective c - Adding year component to islamic calendar fails -
following code shows problem: advancing full year first day of year 1435 not result in first day of 1436.
any ideas i'm missing?
nsdatecomponents *components = [[nsdatecomponents alloc] init]; [components setday:1]; [components setmonth:1]; [components setyear:1435]; nscalendar *islamic = [[nscalendar alloc] initwithcalendaridentifier:nsislamiccalendar]; nsdate *date = [islamic datefromcomponents:components]; nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setcalendar:islamic]; [dateformatter settimestyle:nsdateformatternostyle]; [dateformatter setdatestyle:nsdateformattermediumstyle]; nslog(@"%@", [dateformatter stringfromdate:date]); // -> 01.01.1435 nsdatecomponents *offsetcomponents = [[nsdatecomponents alloc] init]; [offsetcomponents setyear:1]; nsdate *datewithoffset = [islamic datebyaddingcomponents:offsetcomponents todate:date options:0]; nslog(@"%@", [dateformatter stringfromdate:datewithoffset]); // -> 30.12.1435 ... why not 01.01.1436 ????
my suspicion because of summertime/wintertime (daylight savings time) difference. muh. 1, 1435 falls on november 5, 2013, while muh. 1, 1436 falls on october 25, 2014. first date during wintertime, second during summertime.
the first nsdate created november 5, 2013 00:00 (at midnight). "datebyaddingcomponents:" works converting components seconds, , adding first date. in case, result october 24, 2014 23:00, because of summertime.
this mean results different different people around world because of daylight saving time differences between timezones.
you can prevent problem setting first date mid-day, instead of midnight (which in general idea when working pure dates):
nsdatecomponents *components = [[nsdatecomponents alloc] init]; [components setday:1]; [components setmonth:1]; [components setyear:1435]; [components sethour:12]; now, whether correct behaviour of "datebyaddingcomponents" question.
Comments
Post a Comment