views:

407

answers:

1

I want to be able to convert between the Gregorian and Julian calendars (note: not "julian date") using NSCalendar on the iPhone. How can I subclass NSCalendar to use the Julian calendar? I want to do this without regards for 1582 or 1752 or any given year.

A: 

While you can certainly have a go at it, but I suspect that NSCalendar is tricky to subclass. It's toll-free bridged, which complicates things; is entwined with NSLocale, which complicates things; it's initialized with identifiers rather than being subclassed, which complicates things; and gives no guidance on which methods are its primitives which... complicates things...

Do you need the subclass in order to plug into an existing architecture? If not, it would probably be easiest to create an object from scratch that has a similar interface. You may be able to use an NSGregorianCalendar to do most of the work for you and just offset the date by the requisite days before calculating date components. You'll need special logic for 1700, 1800 and 1900 (and of course 2100, etc.) since NSGregorianCalendar won't let you create date components for a leap day in those years, but that's still probably easier than doing all the date component calculations by hand.

Just curious what the goal of such a calendar would be?

Rob Napier
My church uses the Julian calendar in our observances. I'm looking into writing a program that will list upcoming observances. For example, December 25 on the Julian calendar is currently 13 days later (January 7) on the Gregorian calendar. I could easily write something that would take the current date according to the Gregorian calendar and subtract 13 days to give the date on the Julian calendar. The problem is, the day of the week would be wrong (Wed, Jan 7, 2009 on the Gregorian calendar is Wed Dec 25, 2008 on the Julian calendar, not Thurs). I guess I'd have to just hack it by hand.
mlv
I assume you won't have to worry about the next divergent leap year (in Feb 2100), so it sounds like you could calculate the day of week from the Gregorian calendar, and then subtract 13 days to get the rest of the date components of the Julian date. Not elegant, but effective for a few decades at least. And quick to code.
Rob Napier