• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]


  • Subject: Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]
  • From: Keith Blount <email@hidden>
  • Date: Mon, 22 Dec 2008 08:34:20 -0800 (PST)

Hi,

Many thanks to both of you for your very helpful replies - much appreciated! I've gone with Ken's solution, which works perfectly for what I need. For the sake of the archives, I've attached the method I created based on Ken's code, which just creates a hierarchical dictionary of objects with the hierarchy Year->Month->Day, with a "Title" key for each object, so that it can be used easily enough in the data source an NSOutlineView (which is how I'll be using it).

Thanks again - and happy holidays.

All the best,
Keith

-- Ken's code modified for use with an NSOutlineView data source or suchlike --

- (NSMutableDictionary *)diarySpacesForYear:(int)year
{
	NSString *yearStr = [NSString stringWithFormat:@"%i", year];

	NSDate *startDate = [NSDate dateWithNaturalLanguageString:[NSString stringWithFormat:@"January 1 %i", year]];
	NSDate *endDate = [NSDate dateWithNaturalLanguageString:[NSString stringWithFormat:@"January 1 %i", year+1]];

	NSMutableArray *newDiarySpaces = [NSMutableArray array];

	NSMutableDictionary *currentMonth = [NSMutableDictionary dictionaryWithObjectsAndKeys:
										 NSLocalizedString(@"January",nil), @"Title",
										 [NSMutableArray array], @"Days",
										 yearStr, @"Year",
										 [NSNumber numberWithInt:NSMonthCalendarUnit], @"CalendarUnit",
										 nil];
	int secondsPerDay    = 60 * 60 * 24;

	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
	//[dateFormatter setDateStyle:NSDateFormatterFullStyle];
	//[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	[dateFormatter setDateFormat:@"d - EEEE"];

	// init list.
	[newDiarySpaces addObject:currentMonth];

	NSMutableDictionary *day = [NSMutableDictionary dictionaryWithObjectsAndKeys:
								[dateFormatter stringFromDate:startDate], @"Title",
								[NSNumber numberWithInt:NSDayCalendarUnit], @"CalendarUnit",
								nil];
	[[currentMonth objectForKey:@"Days"] addObject:day];

	// Loop until interval >= 0.
	while ([startDate timeIntervalSinceDate:endDate] < 0)
	{
		// Get the next day.
		startDate = [startDate addTimeInterval:secondsPerDay];

		// Check we are still in the year, so that we don't add 1st Jan of the next year.
		NSString *checkYear = [startDate descriptionWithCalendarFormat:@"%Y"
															  timeZone:[NSTimeZone localTimeZone]
																locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
		if ([checkYear isEqualToString:yearStr] == NO)
			break;	// We're into another year, so go no further.

		// Get month from date.
		NSString *checkMonth = [startDate descriptionWithCalendarFormat:@"%B"
															   timeZone:[NSTimeZone localTimeZone]
																 locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];

		// If month is different add it to the list of months.
		if ([checkMonth isEqualToString:[currentMonth objectForKey:@"Title"]] == NO)
		{
			currentMonth = [NSMutableDictionary dictionaryWithObjectsAndKeys:
							checkMonth, @"Title",
							[NSMutableArray array], @"Days",
							yearStr, @"Year",
							[NSNumber numberWithInt:NSMonthCalendarUnit], @"CalendarUnit",
							nil];
			[newDiarySpaces addObject:currentMonth];
		}

		// Add date to list of days in this month.
		NSMutableDictionary *day = [NSMutableDictionary dictionaryWithObjectsAndKeys:
									[dateFormatter stringFromDate:startDate], @"Title",
									[NSNumber numberWithInt:NSDayCalendarUnit], @"CalendarUnit",
									nil];
		[[currentMonth objectForKey:@"Days"] addObject:day];
	}

	[dateFormatter release];

	return [NSMutableDictionary dictionaryWithObjectsAndKeys:
			yearStr, @"Title",
			newDiarySpaces, @"Months",
			[NSNumber numberWithInt:NSYearCalendarUnit], @"CalendarUnit",
			nil];
}





_______________________________________________

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

  • Follow-Ups:
    • Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]
      • From: mmalc Crawford <email@hidden>
  • Prev by Date: Re: NSMutableArray sorting
  • Next by Date: Re: Thread crashing problem
  • Previous by thread: Re: Configuring NSNumberFormatter non-programmatically
  • Next by thread: Re: NSCalendar/NSDate - generating all months/days in a year [SOLVED]
  • Index(es):
    • Date
    • Thread