TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
TimeRange provide check if an NSDate occurs between two other NSDates.
12:00
is contained 10:00 ~ 20:00
)You can try to:
pod try "TimeRange"
@interface TimeRange : NSObject
// CAUTION : You should not access directory these property.
@property(nonatomic, readonly) NSDate *aDate;
@property(nonatomic, readonly) NSDate *bDate;
+ (instancetype)rangeWithStartDate:(NSDate *) startDate endDate:(NSDate *) endDate;
- (BOOL)contains:(NSDate *) targetData;
- (BOOL)overlaps:(TimeRange *) anotherTimeRange;
- (NSString *)description;
@end
Check if an NSDate is contained TimeRange
/*
20:00 ~ 04:00
target date is 01:00
It's contained the time range.
*/
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
[formatter setDateFormat:@"HH:mm:ss"];
NSDate *aDate = [formatter dateFromString:@"20:00"];
NSDate *bDate = [formatter dateFromString:@"04:00"];
TimeRange *timeRange = [TimeRange rangeWithStartDate:aDate bDate:bDate];
NSDate *targetDate = [formatter dateFromString:@"01:00"];
[timeRange contains:targetDate];// => YES
Compare TimeRange
s.
/*
04:00 ~ 10:00
17:00 ~ 23:00
are doesn't overlap.
*/
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
[formatter setDateFormat:@"HH:mm:ss"];
NSArray *aRangeData = @[@"04:00:00", @"10:00:00"];
NSArray *bRangeData = @[@"17:00:00", @"23:00:00"];
TimeRange *aRange = [TimeRange rangeWithStartDate:[formatter dateFromString:aRangeData[0]] bDate:[formatter dateFromString:aRangeData[1]]];
TimeRange *bRange = [TimeRange rangeWithStartDate:[formatter dateFromString:bRangeData[0]] bDate:[formatter dateFromString:bRangeData[1]]];
[aRange overlaps:bRange];// => NO
git checkout -b my-new-feature
git commit -am 'Add some feature'
git push origin my-new-feature
MIT