forked from mipstian/catch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSDate+TimeOfDayMath.m
41 lines (33 loc) · 1.68 KB
/
NSDate+TimeOfDayMath.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
#import "NSDate+TimeOfDayMath.h"
@implementation NSDate (TimeOfDayMath)
- (BOOL)isTimeOfDayBetweenDate:(NSDate *)from andDate:(NSDate *)to {
NSCalendar *calendar = NSCalendar.currentCalendar;
// Get minutes and hours from each date
NSDateComponents *dateComp = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit
fromDate:self];
NSDateComponents *fromComp = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit
fromDate:from];
NSDateComponents *toComp = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit
fromDate:to];
BOOL timeRangeCrossesMidnight = fromComp.hour > toComp.hour || (fromComp.hour == toComp.hour && fromComp.minute > toComp.minute);
if (timeRangeCrossesMidnight) {
// Time range crosses midnight (e.g. 11 PM to 3 AM)
if ((dateComp.hour > toComp.hour && dateComp.hour < fromComp.hour) ||
(dateComp.hour == toComp.hour && dateComp.minute >= toComp.minute) ||
(dateComp.hour == fromComp.hour && dateComp.minute < fromComp.minute)) {
// We are outside of allowed time range
return NO;
}
}
else {
// Time range doesn't cross midnight (e.g. 4 AM to 5 PM)
if ((dateComp.hour > toComp.hour || dateComp.hour < fromComp.hour) ||
(dateComp.hour == toComp.hour && dateComp.minute >= toComp.minute) ||
(dateComp.hour == fromComp.hour && dateComp.minute < fromComp.minute)) {
// We are outside of allowed time range
return NO;
}
}
return YES;
}
@end