Last active
August 29, 2015 14:12
-
-
Save spennyf/9ac06298d7d5da334b0c 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
| #import <UIKit/UIKit.h> | |
| typedef struct | |
| { | |
| NSInteger openTime; | |
| NSInteger closeTime; | |
| } ShopHours; |
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
| #define TO_MINUTES(hour, min) (hour * 60 + min) | |
| (void)viewWillAppear:(BOOL)animated { | |
| if (option1) { | |
| ShopHours WeekSchedule[] = | |
| { | |
| {0, 0}, // index 0 - ignore | |
| { TO_MINUTES(9, 0), TO_MINUTES(24, 0) }, // index 1 - Sunday | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, // index 2 - Monday | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, //TUESDAY | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, //WEDNESDAY | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, //THURSDAY | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, //FRIDAY | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, //SATURDAY | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) } //SUNDAY | |
| }; | |
| } | |
| else { | |
| ShopHours WeekSchedule[] = | |
| {0, 0}, // index 0 - ignore | |
| { TO_MINUTES(9, 0), TO_MINUTES(24, 0) }, // index 1 - Sunday | |
| { TO_MINUTES(8, 30), TO_MINUTES(24, 0) }, // index 2 - Monday | |
| { TO_MINUTES(10, 30), TO_MINUTES(24, 0) }, //TUESDAY | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, //WEDNESDAY | |
| { TO_MINUTES(13, 30), TO_MINUTES(24, 0) }, //THURSDAY | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, //FRIDAY | |
| { TO_MINUTES(7, 30), TO_MINUTES(24, 0) }, //SATURDAY | |
| { TO_MINUTES(14, 30), TO_MINUTES(24, 0) } //SUNDAY | |
| }; | |
| } | |
| [self shopState:date array:WeekSchedule]; | |
| } | |
| - (NSString *) shopState:(NSDate *)dateAndTime array:(ShopHours*)WeekSchedule | |
| { | |
| // break out the weekday, hours and minutes... | |
| // your code from the question | |
| NSCalendar *calendar = [NSCalendar currentCalendar]; | |
| const NSCalendarUnit units = NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit; | |
| NSDateComponents *comps = [calendar components:units fromDate:dateAndTime]; | |
| NSInteger weekday = comps.weekday; | |
| NSInteger minutes = TO_MINUTES(comps.hour, comps.minute); | |
| if (minutes >= WeekSchedule[weekday].openTime && minutes <= WeekSchedule[weekday].closeTime) | |
| { | |
| // shop is open... | |
| // determine if its closing within 30 mins and return an appropriate string | |
| NSLog(@"OPEN"); | |
| return @"Open"; | |
| } | |
| else | |
| { | |
| // shop is closed... | |
| NSLog(@"CLOSED"); | |
| return @"Closed"; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment