Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
67 lines (56 sloc) 2.29 KB
/*
* Copyright (c) 2009 Keith Lazuka
* License: http://www.opensource.org/licenses/mit-license.html
*/
#import "NSDateAdditions.h"
@implementation NSDate (KalAdditions)
- (NSDate *)cc_dateByMovingToBeginningOfDay
{
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents* parts = [[NSCalendar currentCalendar] components:flags fromDate:self];
[parts setHour:0];
[parts setMinute:0];
[parts setSecond:0];
return [[NSCalendar currentCalendar] dateFromComponents:parts];
}
- (NSDate *)cc_dateByMovingToEndOfDay
{
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDateComponents* parts = [[NSCalendar currentCalendar] components:flags fromDate:self];
[parts setHour:23];
[parts setMinute:59];
[parts setSecond:59];
return [[NSCalendar currentCalendar] dateFromComponents:parts];
}
- (NSDate *)cc_dateByMovingToFirstDayOfTheMonth
{
NSDate *d = nil;
BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&d interval:NULL forDate:self];
NSAssert1(ok, @"Failed to calculate the first day the month based on %@", self);
return d;
}
- (NSDate *)cc_dateByMovingToFirstDayOfThePreviousMonth
{
NSDateComponents *c = [[[NSDateComponents alloc] init] autorelease];
c.month = -1;
return [[[NSCalendar currentCalendar] dateByAddingComponents:c toDate:self options:0] cc_dateByMovingToFirstDayOfTheMonth];
}
- (NSDate *)cc_dateByMovingToFirstDayOfTheFollowingMonth
{
NSDateComponents *c = [[[NSDateComponents alloc] init] autorelease];
c.month = 1;
return [[[NSCalendar currentCalendar] dateByAddingComponents:c toDate:self options:0] cc_dateByMovingToFirstDayOfTheMonth];
}
- (NSDateComponents *)cc_componentsForMonthDayAndYear
{
return [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:self];
}
- (NSUInteger)cc_weekday
{
return [[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:self];
}
- (NSUInteger)cc_numberOfDaysInMonth
{
return [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self].length;
}
@end
Something went wrong with that request. Please try again.