forked from robbiehanson/AlarmClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendarAdditions.m
195 lines (164 loc) · 6.13 KB
/
CalendarAdditions.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#import "CalendarAdditions.h"
@implementation NSDate (CalendarAdditions)
// FOR COMPARING DATES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
Returns whether or not the current calendarDate is earlier then the given calendarDate.
Note: If they represent the same date in time, this method returns false.
Note: Doesn't take into effect the timeZone representation. Just the internal NSDate absolute time.
**/
- (BOOL)isEarlierDate:(NSDate *)anotherDate
{
return [self timeIntervalSinceDate:anotherDate] < 0;
}
/**
Returns whether or not the current calendarDate is later then the given calendarDate.
Note: If they represent the same date in time, this method returns false.
Note: Doesn't take into effect the timeZone representation. Just the internal NSDate absolute time.
**/
- (BOOL)isLaterDate:(NSDate *)anotherDate
{
return [self timeIntervalSinceDate:anotherDate] > 0;
}
// FOR EXTRACTING PRECISION DATE COMPONENTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
Returns an NSTimeInterval within the minute.
This can be used to determine both the number of seconds, and milliseconds, of the time within the current minute.
IE - if the time is 5:42:36.238 AM, this method would return 36.238.
typedef double NSTimeInterval: Always in seconds; yields submillisecond precision...
**/
- (NSTimeInterval)intervalOfMinute
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitSecond|NSCalendarUnitNanosecond fromDate:self];
double sec = components.second;
double nano = components.nanosecond;
NSTimeInterval result = sec + (nano * 1.0e-9);
//NSLog(@"intervalOfMinute: %f", result);
return result;
}
/**
Returns an NSTimeInterval within the day.
This can be used to determine both the number of seconds, and milliseconds, of the time within the current day.
IE - if the time is 12:01:02.003 AM, this method would return 62.003.
typedef double NSTimeInterval: Always in seconds; yields submillisecond precision...
**/
- (NSTimeInterval)intervalOfDay
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitNanosecond fromDate:self];
double sec = components.hour * 3600 + components.minute * 60 + components.second;
double nano = components.nanosecond;
NSTimeInterval result = sec + (nano * 1.0e-9);
//NSLog(@"intervalOfDay: %f", result);
return result;
}
// FOR LAYING OUT CALENDARS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
Returns the number of days in the current month for this calendarDate.
**/
- (NSUInteger)daysInMonth
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange days = [calendar rangeOfUnit:NSCalendarUnitDay
inUnit:NSCalendarUnitMonth
forDate:self];
return days.length;
}
/**
Returns the number of months in the current year for this calendarDate.
Fixme: do we need it for some exotic calendar?
**/
- (NSUInteger)monthsInYear
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange months = [calendar rangeOfUnit:NSCalendarUnitMonth
inUnit:NSCalendarUnitYear
forDate:self];
return months.length;
}
/**
Returns the weekday of the first day of the month for this calendarDate.
**/
- (NSUInteger)startingWeekdayOfMonth
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *firstDayOfMonth;
[calendar rangeOfUnit:NSCalendarUnitMonth
startDate:&firstDayOfMonth
interval:nil
forDate:self];
NSUInteger weekday = [calendar component:NSCalendarUnitWeekday fromDate:firstDayOfMonth];
return weekday - 1;
}
// FOR ALTERING DATES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
Rolls the current time by the specified amount.
Rolling means that if the time overflows for one field, it loops, and doesn't affect the other fields.
IE - if the time was 4:55, and you rolled the time 6 minutes, it would now be 4:01
**/
- (NSDate *)dateByRollingYears:(int)year months:(int)month days:(int)day hours:(int)hour minutes:(int)minute seconds:(int)second
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay |
NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond
fromDate:self];
// Make the common case fast.
// Common case: only one variable is being rolled.
// Roll seconds (always 60)
if(second != 0)
{
components.second = (components.second + second) % 60;
}
// Roll minutes (always 60)
if(minute != 0)
{
components.minute = (components.minute + minute) % 60;
}
// Roll hours (always 24)
if(hour != 0)
{
components.hour = (components.hour + hour) % 24;
}
// Roll days (variable, starts @ 1)
if(day != 0)
{
NSUInteger d = [self daysInMonth];
components.day = ((components.day - 1) + day) % d + 1;
}
// Roll months (variable??, starts @ 1)
if(month != 0)
{
NSUInteger m = [self monthsInYear];
components.month = ((components.month - 1) + month) % m + 1;
}
return [calendar dateFromComponents:components];
}
/**
Returns a calendar date with the same local time as this one, for the new time zone.
That is, if the local time for this date is 10:00AM, the local time for the new date will be 10:00AM in it's time zone.
**/
- (NSDate *)dateBySwitchingToTimeZone:(NSTimeZone *)newTimeZone
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay |
NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond |
NSCalendarUnitTimeZone
fromDate:self];
if (components.timeZone != newTimeZone) {
components.timeZone = newTimeZone;
}
return [calendar dateFromComponents:components];
}
@end