Created
December 30, 2014 18:23
-
-
Save spennyf/56c7001d0c959b09f9a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| NSArray *shopHours = @[WTMakeRange(Monday, 7, 30, Monday, 23, 20), | |
| WTMakeRange(Tuesday, 7, 30, Wednesday, 0, 0), | |
| WTMakeRange(Wednesday, 7, 30, Thursday, 0, 0), | |
| WTMakeRange(Thursday, 7, 30, Friday, 0, 0), | |
| WTMakeRange(Friday, 7, 30, Friday, 22, 0), | |
| WTMakeRange(Saturday, 9, 0, Saturday, 22, 0), | |
| WTMakeRange(Sunday, 9, 0, Sunday, 14, 0), | |
| WTMakeRange(Sunday, 22, 0, Sunday, 23, 0) | |
| ]; | |
| NSCalendar *calendar = [NSCalendar currentCalendar]; | |
| const NSCalendarUnit units = NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit; | |
| NSDateComponents *comps = [calendar components:units fromDate:date]; | |
| NSInteger weekday = comps.weekday; | |
| NSInteger hour = comps.hour; | |
| NSInteger minute = comps.minute; | |
| Weekday currentDay = (int) weekday; | |
| NSLog(@"DAY: %ld, HOUR: %ld, MINUTE: %ld", (long)weekday, (long)hour, (long)minute); | |
| WeekTime *mondayElevenPM = [[WeekTime alloc] initWithWeekday:currentDay hour:hour minute:minute]; | |
| WeekTimeRange *openRange = WTFindOpenRangeIncluding(shopHours, mondayElevenPM); | |
| NSString *result = WTStatus(openRange, mondayElevenPM); | |
| NSLog(@"RESULT: %@", result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| typedef enum { | |
| Sunday, | |
| Monday, | |
| Tuesday, | |
| Wednesday, | |
| Thursday, | |
| Friday, | |
| Saturday | |
| } Weekday; | |
| @interface WeekTime : NSObject | |
| - (instancetype)initWithWeekday:(Weekday)weekday hour:(NSInteger)hour minute:(NSInteger)minute; | |
| @property (nonatomic, readonly) NSInteger minutesSinceStartOfWeek; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "WeekTime.h" | |
| @implementation WeekTime | |
| - (instancetype)initWithWeekday:(Weekday)weekday hour:(NSInteger)hour minute:(NSInteger)minute { | |
| NSParameterAssert(weekday >= Sunday && weekday <= Saturday); | |
| NSParameterAssert(hour >= 0 && hour < 24); | |
| NSParameterAssert(minute >= 0 && minute < 60); | |
| self = [super init]; | |
| if (self != nil) { | |
| _minutesSinceStartOfWeek = weekday * 24 * 60 + hour * 60 + minute; | |
| } | |
| return self; | |
| } | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <Foundation/Foundation.h> | |
| #import "WeekTime.h" | |
| @interface WeekTimeRange : NSObject | |
| @property (nonatomic, readonly) WeekTime *start; | |
| @property (nonatomic, readonly) WeekTime *end; | |
| - (instancetype)initWithStart:(WeekTime *)start end:(WeekTime *)end; | |
| - (BOOL)contains:(WeekTime *)time; | |
| @end | |
| /** Convenience to make a WeekTimeRange */ | |
| WeekTimeRange *WTMakeRange(Weekday startDay, NSInteger startHour, NSInteger startMinute, | |
| Weekday endDay, NSInteger endHour, NSInteger endMinute); | |
| /** Return the first range in ranges that includes time. If no ranges | |
| include time, then nil. */ | |
| WeekTimeRange *WTFindOpenRangeIncluding(NSArray *ranges, WeekTime *time); | |
| /** Figure out the Open/Closed/Closing string */ | |
| NSString *WTStatus(WeekTimeRange *range, WeekTime *requestedTime); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "WeekTimeRange.h" | |
| @implementation WeekTimeRange | |
| - (instancetype)initWithStart:(WeekTime *)start end:(WeekTime *)end { | |
| NSInteger startMinutes = start.minutesSinceStartOfWeek; | |
| NSInteger endMinutes = end.minutesSinceStartOfWeek; | |
| NSParameterAssert(startMinutes != endMinutes); | |
| self = [super init]; | |
| if (self != nil) { | |
| _start = start; | |
| _end = end; | |
| } | |
| return self; | |
| } | |
| - (BOOL)contains:(WeekTime *)time { | |
| NSInteger queryMinutes = time.minutesSinceStartOfWeek; | |
| NSInteger startMinutes = self.start.minutesSinceStartOfWeek; | |
| NSInteger endMinutes = self.end.minutesSinceStartOfWeek; | |
| if (startMinutes < endMinutes) { | |
| return (startMinutes <= queryMinutes && | |
| queryMinutes < endMinutes); | |
| } else { | |
| return (endMinutes < queryMinutes && | |
| queryMinutes <= startMinutes); | |
| } | |
| } | |
| @end | |
| WeekTimeRange *WTMakeRange(Weekday startDay, NSInteger startHour, NSInteger startMinute, | |
| Weekday endDay, NSInteger endHour, NSInteger endMinute) { | |
| return [[WeekTimeRange alloc] initWithStart:[[WeekTime alloc] initWithWeekday:startDay hour:startHour minute:startMinute] | |
| end:[[WeekTime alloc] initWithWeekday:endDay hour:endHour minute:endMinute]]; | |
| } | |
| WeekTimeRange *WTFindOpenRangeIncluding(NSArray *ranges, WeekTime *time) { | |
| for (WeekTimeRange *range in ranges) { | |
| if ([range contains:time]) { | |
| return range; | |
| } | |
| } | |
| return nil; | |
| } | |
| NSString *WTStatus(WeekTimeRange *range, WeekTime *requestedTime) { | |
| if (range != nil) { | |
| NSInteger minutesLeft = range.end.minutesSinceStartOfWeek - requestedTime.minutesSinceStartOfWeek; | |
| if (minutesLeft <= 30) { | |
| return [NSString stringWithFormat:@"Closing in %ld minutes", (long)minutesLeft]; | |
| } else { | |
| return @"Open"; | |
| } | |
| } else { | |
| return @"Closed"; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment