cocoa - How to create a specific date in the distant past, the BC era -
i’m trying create date in bc era, failing pretty hard. following returns ‘4713’ year, instead of ‘-4712’:
nscalendar *calendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsdatecomponents *components = [nsdatecomponents new]; [components setyear: -4712]; nsdate *date = [calendar datefromcomponents:components]; nslog(@"%d", [[calendar components:nsyearcalendarunit fromdate: date] year]);
any idea i’m doing wrong?
update: working code
nscalendar *calendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsdatecomponents *components = [nsdatecomponents new]; [components setyear: -4712]; nsdate *date = [calendar datefromcomponents:components]; nsdatecomponents *newcomponents = [calendar components:nseracalendarunit|nsyearcalendarunit fromdate:date]; nslog(@"era: %d, year %d", [newcomponents era], [newcomponents year]);
this prints 0 era, ben explained.
your code working fine. since there’s no year zero, -4712 year 4713 bc. if check era component you’ll see it’s zero, in gregorian calendar indicates bc. flip negative sign , you’ll see 4712 ad (era 1).
Comments
Post a Comment