Re: NSCalendar/NSDate - generating all months/days in a year
Re: NSCalendar/NSDate - generating all months/days in a year
- Subject: Re: NSCalendar/NSDate - generating all months/days in a year
- From: Ken Tozier <email@hidden>
- Date: Tue, 23 Dec 2008 04:21:33 -0500
On Dec 22, 2008, at 2:30 PM, mmalc Crawford wrote:
No!
This is precisely the direction in which not to go:
"Important: 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 Date and Time Programming Guide for
Cocoa."
<http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSCalendarDate_Class/Reference/Reference.html
>
mmalc
Taking the mmalc challenge I came up with the following. Seems to work
with all calendar systems and languages I tried. Obviously I could be
more rigorous about checking return values, but this was just an
exercise so I didn't feel the need.
Ball is in your court sir :)
// -----------------------------------------------------
// Main method
// -----------------------------------------------------
- (NSDictionary *) dates:(int) inYear
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
NSMutableArray *days = [NSMutableArray array];
NSDictionary *locale = [[NSUserDefaults standardUserDefaults]
dictionaryRepresentation];
NSDateComponents *comp = [self dateComponentsWithYear: inYear
month: 1 day: 1],
*inc = [self dateComponentsWithYear: 0 month: 0 day: 1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *date;
NSDateFormatter *keyFormatter = [self dateFormatterWithFormat:
@"MMMM"];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit;
int month = 0;
// init date
date = [calendar dateFromComponents: comp];
while ([comp year] == inYear)
{
if ([comp month] != month)
{
month = [comp month];
days = [NSMutableArray array];
[result setObject: days forKey: [keyFormatter stringFromDate:
date]]; // use localized "month" name for key
}
// add day to day list
[days addObject: [date descriptionWithLocale: locale]];
// increment the date
date = [calendar dateByAddingComponents: inc toDate: date options:
0]; // option:0 increments higher level fields on overflow
// update components
comp = [calendar components: unitFlags fromDate: date];
}
return result;
}
// -----------------------------------------------------
// Convenience methods
// -----------------------------------------------------
- (NSDateComponents *) dateComponentsWithYear:(int) inYear
month:(int) inMonth
day:(int) inDay
{
NSDateComponents *result = [[[NSDateComponents alloc] init]
autorelease];
[result setYear: inYear];
[result setMonth: inMonth];
[result setDay: inDay];
return result;
}
- (NSDateFormatter *) dateFormatterWithFormat:(NSString *) inFormat
{
NSDateFormatter *result = [[[NSDateFormatter alloc] init]
autorelease];
[result setFormatterBehavior: NSDateFormatterBehavior10_4];
[result setDateFormat: inFormat];
return result;
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden