-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDateAdditions.m
executable file
·67 lines (56 loc) · 2.26 KB
/
NSDateAdditions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* 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];
c.month = -1;
return [[[NSCalendar currentCalendar] dateByAddingComponents:c toDate:self options:0] cc_dateByMovingToFirstDayOfTheMonth];
}
- (NSDate *)cc_dateByMovingToFirstDayOfTheFollowingMonth
{
NSDateComponents *c = [[NSDateComponents alloc] init];
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