Let’s see here, what’s the currently supported way to round a datetime to the nearest hour in Cocoa?
// Round the current date to the nearest hour
NSDate * currentDateTime = [NSDate date];
NSCalendar * currentCalendar = [NSCalendar currentCalendar];
NSDateComponents * dateComponents = [currentCalendar components:NSEraCalendarUnit |
NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit
fromDate:currentDateTime];
if ([dateComponents minute] >= 30)
[dateComponents setHour:[dateComponents hour] + 1];
[dateComponents setMinute:0];
NSDate * roundedDate = [currentCalendar dateFromComponents:dateComponents];
The API seems a little excessive. I can appreciate what Apple has tried to do in making dates more flexible and easier to internationalize, but should basic manipulation really be that cumbersome?