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: WT <email@hidden>
- Date: Mon, 22 Dec 2008 14:19:25 +0100
Hi Keith,
Ken's suggestion is much more elegant than mine, but here's mine
anyway. I wrote a little Foundation Tool to test it and it works fine.
Note that the year (2009), and the fact that 2009 is not a leap year,
are hard-coded. You'll have to change that, of course.
Hope this helps.
Wagner
=== source ===
#import <Foundation/Foundation.h>
static int monthDays[] =
{
31, // Jan
28, // Feb - hard-coded for non-leap years
31, // Mar
30, // Apr
31, // May
30, // Jun
31, // Jul
31, // Aug
30, // Sep
31, // Oct
30, // Nov
31, // Dec
};
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *dayNames = [[NSArray alloc] initWithObjects:
@"Saturday",
@"Sunday",
@"Monday",
@"Tuesday",
@"Wednesday",
@"Thursday",
@"Friday",
nil];
NSArray *monthNames = [[NSArray alloc] initWithObjects:
@"January",
@"February",
@"March",
@"April",
@"May",
@"June",
@"July",
@"August",
@"September",
@"October",
@"November",
@"December",
nil];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear: 2009]; // hard-coded
[comps setMonth: 1];
[comps setDay: 1];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier: NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents: comps];
[comps release];
NSDateComponents *weekdayComponents =
[gregorian components: NSWeekdayCalendarUnit fromDate: date];
int weekday = [weekdayComponents weekday];
int daysInYear = 365; // hard-coded for non-leap years
int day = 0;
int month = 0;
for (int i = 0; i < daysInYear; ++i)
{
int dayOfWeek = (i + weekday) % 7;
++day;
if (day > monthDays[month])
{
++month; // go to next month
day = 1; // reset day to the start of the month
NSLog(@" "); // to separate months in the output
}
NSLog(@"%@, %@ %i %i",
[dayNames objectAtIndex: dayOfWeek],
[monthNames objectAtIndex: month],
day,
2009); // hard-coded
}
[dayNames release];
[monthNames release];
[pool drain];
return 0;
}
=== output ===
[Session started at 2008-12-22 14:02:20 +0100.]
2008-12-22 14:02:20.304 Calendrical[909:10b] Thursday, January 1 2009
2008-12-22 14:02:20.310 Calendrical[909:10b] Friday, January 2 2009
...
2008-12-22 14:02:20.343 Calendrical[909:10b] Friday, January 30 2009
2008-12-22 14:02:20.343 Calendrical[909:10b] Saturday, January 31 2009
2008-12-22 14:02:20.344 Calendrical[909:10b]
2008-12-22 14:02:20.344 Calendrical[909:10b] Sunday, February 1 2009
2008-12-22 14:02:20.345 Calendrical[909:10b] Monday, February 2 2009
...
2008-12-22 14:02:20.356 Calendrical[909:10b] Friday, February 27 2009
2008-12-22 14:02:20.356 Calendrical[909:10b] Saturday, February 28 2009
2008-12-22 14:02:20.357 Calendrical[909:10b]
2008-12-22 14:02:20.357 Calendrical[909:10b] Sunday, March 1 2009
2008-12-22 14:02:20.358 Calendrical[909:10b] Monday, March 2 2009
...
2008-12-22 14:02:20.373 Calendrical[909:10b] Monday, March 30 2009
2008-12-22 14:02:20.378 Calendrical[909:10b] Tuesday, March 31 2009
...
...
2008-12-22 14:02:20.700 Calendrical[909:10b] Wednesday, December 30 2009
2008-12-22 14:02:20.701 Calendrical[909:10b] Thursday, December 31 2009
_______________________________________________
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