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, 5 Feb 2007 14:42:01 -0800
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. 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. It also looks like NSCalendarDate is
applying daylight savings time incorrectly.
If you pass a year more recent than the 16th century, you should get
results that match. Either that, or use NSCalendar and
NSDateFormatter to display the date, rather than [NSDate description].
Deborah Goldsmith
Internationalization, Unicode liaison
Apple Inc.
email@hidden
On Feb 3, 2007, at 11:11 AM, Sean McBride wrote:
Hi all,
I have been switching from NSCalendarDate to NSDateComponents as a
first
step to supporting non-Gregorian calendars. But I don't understand
this
behaviour:
NSCalendar* cal = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar]];
NSDateComponents* comps = [[NSDateComponents alloc] init];
[comps setDay:27];
[comps setMonth:1];
[comps setYear:100];
NSDate* date = [cal dateFromComponents:comps];
NSLog (@"date %@", date);
NSCalendarDate* calDate = [NSCalendarDate dateWithYear:100
month:1 day:27 hour:0 minute:0 second:0 timeZone:nil];
NSLog (@"calDate %@", calDate);
Outputs:
date 100-01-24 23:00:00 -0500
calDate 100-01-27 00:00:00 -0500
Why do I get the wrong date with NSDateComponents? The day should
be 27
not 24.
Thanks,
Sean
--
1908's Model T Ford: 25 miles per gallon. Today's Ford Expedition:
15.5 mpg.
_______________________________________________
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
_______________________________________________
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