Re: Strange results with NSDateComponents vs NSCalendarDate
Re: Strange results with NSDateComponents vs NSCalendarDate
- Subject: Re: Strange results with NSDateComponents vs NSCalendarDate
- From: Deborah Goldsmith <email@hidden>
- Date: Mon, 12 Feb 2007 17:50:51 -0800
On Feb 11, 2007, at 6:58 PM, Sean McBride wrote:
The reason they differ is that NSCalendarDate uses the Gregorian
calendar for all of time, even before it was actually adopted.
NSCalendar's version of the Gregorian calendar uses the Julian
calendar before October 4, 1582.
I see! Is this documented? I can't find anything about it.
I don't think it's documented anywhere.
Since you're converting from a date
to an absolute time using NSCalendar, and then converting back to a
date using NSCalendarDate (implicitly in the case of your call to
[NSDate description] via NSLog), and you're passing a date that's
around 100 AD, you get a different result. Since the Gregorian and
Julian calendars differ by one day every 400 years, and the date you
give is about 1483 years before the changeover date, you're getting a
difference of three days.
Is there a way to get the pure-Gregorian behaviour with NSCalendar/
NSDateComponents? Or must I use old NSCalendarDate? I was hoping to
purge all NSCalendarDate usage.
The underlying ICU library allows you to choose the calendar cutover
date, but there is no exported API for that.
The problem comes just from using [NSDate description] to display the
date/time. The value is correct; it's just the way you're formatting
it. If you use NSDateFormatter and NSCalendar for everything
(including formatting dates), you should be fine.
Let me explain what I am really doing. AddressBook insists that
birthdays are fully specified (year, month, day) (<rdar://2970531>)
but
in in real life people often only know the month and day. As a
workaround, my app has set the year to 100 if the user does not
know the
year. Up to now I have been using the pure-Gregorian NSCalendarDate.
Now that I have changed to NSCalendar/NSDateComponents this has
apparently come back to bite me. Do you have any advice?
Well, if your data is already out in the field in year 100, just make
sure you don't use NSCalendarDate or [NSDate description] for
anything. It would be better to use something like 1900 or 2000 than
100 if you still have a choice.
Deborah Goldsmith
Internationalization, Unicode liaison
Apple Inc.
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden