Question

I can't seem to figure out anywhere in the documentation or online how long an NSCalendarUnitEra. I'm assuming longer than a year, or can the length be specified? Can anyone help?

Was it helpful?

Solution

The length of a calendrical era varies according to how it is defined by the calendar which references it.

For example the Gregorian calendar uses BCE and AD as its two eras. So if you were to consider time as infinite, you could imagine any year number could exist either counting backwards in the BCE era or forward in the AD era. So each era could be thought of as a timeline which has one end and stretches out in the other direction for (essentially) infinity.

While many calendars use the BCE/AD era system, some have only one era (Hebrew & Islamic for example). So obviously, if there is only one era the era lasts for all time. (quite long)

The Japanese calendar is the most peculiar - of those in the framework. There era is tied to the life of the emperor. The NSCalendar framework has 236 eras listed for the Japanese calendar. Many of these eras are quite short "Anna (968-970)" for one example.

Apple has a couple very good WWDC talks on working with dates:

  • WWDC 2010 - Session 117 - Performing Calendar Calculations (23m - Japanese calendar era discussed)
  • WWDC 2013 - Session 227 - Solutions to Common Date and Time Challenges

How long is an NSCalendarUnitEra?

It depends on the calendar.

I'm assuming longer than a year

A fairly safe assumption - save for a very unlucky emperor.

To see all of the eras used in all of the calendars simply enumerate the calendars and access the longEraSymbols array.

note: the NSLog() console cannot display the Japanese characters correctly - but it keeps the code simple.

NSArray *calendarIdentifiers =
    @[NSCalendarIdentifierGregorian,
      NSCalendarIdentifierBuddhist,
      NSCalendarIdentifierChinese,
      NSCalendarIdentifierCoptic,
      NSCalendarIdentifierEthiopicAmeteMihret,
      NSCalendarIdentifierEthiopicAmeteAlem,
      NSCalendarIdentifierHebrew,
      NSCalendarIdentifierISO8601,
      NSCalendarIdentifierIndian,
      NSCalendarIdentifierIslamic,
      NSCalendarIdentifierIslamicCivil,
      NSCalendarIdentifierJapanese,
      NSCalendarIdentifierPersian,
      NSCalendarIdentifierRepublicOfChina];
for (NSString *calendarIdentifier in calendarIdentifiers) {
    NSCalendar *cal = [NSCalendar calendarWithIdentifier:calendarIdentifier];
    NSLog(@"Name:%@ NumberOfEras:%li Eras:%@",calendarIdentifier,cal.longEraSymbols.count,cal.longEraSymbols);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top