My application needs to obtain the day of year for a given date. In the past, this was easily done with an NSCalendarDate.
// NSCalendarDate faces deprecation int dayOfYearForDate1(NSDate *_date) { NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; NSCalendarDate *calendarDate = [_date dateWithCalendarFormat:nil timeZone:gmtTimeZone]; int day = [calendarDate dayOfYear]; return day; } NSCalendarDate faces deprecation: "Use of NSCalendarDate strongly discouraged. It is not deprecated yet, however it may be in the next major OS release after Mac OS X v10.5. For calendrical calculations, you should use suitable combinations of NSCalendar, NSDate, and NSDateComponents, as described in Calendars in Dates and Times Programming Topics for Cocoa." The above advice led to two alternate functions: int dayOfYearForDate2(NSDate *_date) { NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:gmtTimeZone]; NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:_date]; int year = [components year]; components = [[NSDateComponents alloc] init]; [components setYear:year -1]; [components setMonth:12]; [components setDay:30]; [components setHour:23]; [components setMinute:59]; [components setSecond:59]; NSDate *lastYear1230 = [calendar dateFromComponents:components]; [components release]; components = [calendar components:NSDayCalendarUnit fromDate:lastYear1230 toDate:_date options:0]; int day = [components day]; [calendar release]; return day; } int dayOfYearForDate3(NSDate *_date) { NSTimeZone *gmtTimeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:gmtTimeZone]; NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:_date]; int year = [components year]; NSString *dateString = [NSString stringWithFormat:@"%d-12-30 23:59:59 -0000", year - 1]; NSDate *lastYear1230 = [NSDate dateWithString:dateString]; components = [calendar components:NSDayCalendarUnit fromDate:lastYear1230 toDate:_date options:0]; int day = [components day]; [calendar release]; return day; } To decide whether to use dayOfYearForDate2() or dayOfYearForDate3() in my application, I benchmarked all three functions: dayOfYearForDate1(): 8.76544 microseconds dayOfYearForDate2(): 46.9595 microseconds dayOfYearForDate3(): 74.5191 microseconds (The above times include 0.4 microseconds attributable to the testing overhead.) Since Apple's engineers would not throw away a perfectly good object without providing something better, I must be doing things the hard way. What is the easy way? Thanks in advance. ++ Tom Tom Bernard [EMAIL PROTECTED] _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]