Created
March 23, 2009 17:18
-
-
Save jnjosh/83667 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSInteger)daysLeftTillChristmas { | |
// prepare to clean up | |
NSAutoreleasePool *daysPool = [[NSAutoreleasePool alloc] init]; | |
// get day | |
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDate *today = [NSDate date]; | |
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:today]; | |
// test for after 12/25 | |
//NSDate *today = [NSDate dateWithString:@"2008-12-25 22:00:00 -0500"]; | |
//NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:today]; | |
NSLog(@"%@", today); | |
// end test | |
// get integer days for calculating | |
NSInteger month = [weekdayComponents month]; | |
NSInteger day = [weekdayComponents day]; | |
NSInteger year = [weekdayComponents year]; | |
// check for next christmas | |
if (month == 12 && day > 25) { | |
year++; | |
} | |
// get christmas day | |
NSDateComponents *christmasComponents = [[NSDateComponents alloc] init]; | |
[christmasComponents setMonth:12]; | |
[christmasComponents setDay:25]; | |
[christmasComponents setYear:year]; | |
[christmasComponents setHour:0]; | |
[christmasComponents setMinute:0]; | |
NSDate *christmasDay = [gregorian dateFromComponents:christmasComponents]; | |
// calculate days | |
NSTimeInterval secondsPerDay = 24 * 60 * 60; | |
NSTimeInterval daysTilChristmas = [christmasDay timeIntervalSinceDate:today]; | |
NSInteger totalDaysLeft = (NSInteger) (daysTilChristmas / secondsPerDay) + 1; | |
if (month == 12 && day == 25) { | |
totalDaysLeft = 0; | |
} | |
// memory cleaning... | |
[christmasComponents release]; | |
[gregorian release]; | |
[daysPool release]; | |
NSLog(@"Days Left: %d", totalDaysLeft); | |
//totalDaysLeft = 18; | |
// return | |
return totalDaysLeft; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment