ios - NSDate created from NSDateComponents incorrect? -
i created nsdate
datefromcomponents
, using nscalendar
nsgregoriancalendar
identifier, here's strange part: date incorrect if it's before point in time before 1900/12/31
nscalendar *calendar = [[nscalendar alloc] initwithcalendaridentifier:nscalendaridentifiergregorian]; nsdatecomponents *components = [[nsdatecomponents alloc] init]; components.year = 1900; components.month = 12; components.day = 31; nsdate *date = [calendar datefromcomponents:components]; components.year = 1901; components.month = 1; components.day = 1; nsdate *date2 = [calendar datefromcomponents:components]; nslog(@"%@",calendar.timezone.description); nslog(@"%@",date); nslog(@"%@",date2);
the log be:
2016-05-25 14:58:21.014 date[79754:2192157] asia/shanghai (gmt+8) offset 28800 2016-05-25 14:58:21.015 date[79754:2192157] 1900-12-30 15:54:17 +0000 2016-05-25 14:58:21.015 date[79754:2192157] 1900-12-31 16:00:00 +0000
as can see, there 5 minutes gap during day. however, if set timezone [nstimezone timezoneforsecondsfromgmt:]
, same seconds deviation - 28800, normal. cause of this?
no, date isn't incorrect. instead, nscalendar code knows things calendars wouldn't dream about, calendars changing time offsets @ points in time in past.
you asked asia/shanghai calendar convert 2 dates, 1 on day before changed time zone, 1 on day after changed time zone, , both times converted correctly. night in shanghai had adjust watches.
Comments
Post a Comment