Yes, folks, it’s Happy Fun Bug Time where I talk about what has been making me tear my hair out recently. In this installment, we talk about NSCalendar.
NSCalendar has a method called rangeOfUnit:inUnit:forDate:
. What this method is supposed to do calculate how many of one time unit are in another. For instance, how many days are in a particular month. Since date calculations can get tricky, with daylight savings, leap years and all, this method is quite handy.
Or, it would be handy if it didn’t suffer from some pretty bad bugs on Leopard. At first, I tried something like the following to calculate the number of days in a given year:
range = [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:[NSDate date]];
NSLog(@"%@", NSStringFromRange(range));
Given that this is a leap year, I’d expect “{1, 366}”. What do I get? “{1, 31}”. I suspect that wires got crossed with the days-in-a-month calculation. Oh, but the fun doesn’t stop there. Try this at home (kids: do not try this at home):
NSCalendarDate *date;
int i;
date = [NSCalendarDate dateWithYear:2007 month:1 day:1 hour:0
minute:0 second:0 timeZone:nil];
for (i = 0; i < 365; i++)
{
range = [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit
inUnit:NSWeekCalendarUnit
forDate:date];
if (range.length != 7)
{
NSLog(@"Date %@ has week of length %d", date, range.length);
}
date = [date dateByAddingYears:0 months:0 days:1 hours:0 minutes:0 seconds:0];
}
This cycles through each day of last year (2007) and calculates the number of days that week. Now, having just recently lived through that year, I think I can say with some certainty that no week had any more or less than 7 days in it. The above code prints out any oddballs. If you run it yourself, you find that quite a few days are in weeks exceeding 7 days. Upon closer inspection, you find that any day in a week straddling two months reports the number of days in the month, not the week. On weeks fully contained within a month, it reports 7 days. Again we are seeing a tendency towards the days-in-a-month calculation. Someone really likes computing that.
I've filed a bug report (rdar://5704940 for you Apple folks). Unless I'm doing something really wrong here or there were some time distortions last year that I was unaware of, you may want to avoid using NSCalendar's rangeOfUnit:inUnit:forDate:
on Leopard for the time being. Thinking about it though, the time distortions would explain a couple weekends…
Update (Feb. 1, 2008):
I strongly recommend reading the comments. Thanks to Chris Suter for explaining what Apple's logic is in doing it this way. We all thought he was nuts at first but it appears that he was just explaining Apple's craziness.
In short, things probably are working as designed by Apple. I still think the design is flawed and that it is horrendous API. It seems that, in Apple's mind, Leopard is correcting a bug in Tiger (in other words, the intuitive and useful behavior in Tiger was a bug).
The original point stands that one needs to take care with -rangeOfUnit:inUnit:forDate:
. It seems to be only useful for specific combinations of units and given the change in behavior between Tiger and Leopard, it becomes even more of a headache. If Apple is going to continue with this interpretation, then they should just treat these computations as undefined since the results are misleading.