处理Foursquare小时API以查明场地是打开还是关闭

时间:2014-09-04 22:45:23

标签: ios objective-c foursquare

我有一个foursquare小时数组(Foursquare API),用于存储特定场所开放时的小时数。它看起来像这样:

[{
    "days":[1,2,3,4,7],
    "includesToday":true,
    "open":[
        {"end":"+0200","start":"1000"}],
    "segments":[]},
{
    "days":[5,6]
    ,"open":[
        {"end":"+0300","start":"1000"}],
    "segments":[]}
]

如何确定当前场地是打开还是关闭?

1 个答案:

答案 0 :(得分:2)

我这样处理:4sq hours API gist

-(NSDictionary*)isVenueOpenDictionaryForHours:(NSArray*)hours{
  //  defaults and inits
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDictionary *lastSegmentYesterday = [[NSDictionary alloc] init];
    NSDate *dateNow = [NSDate date];
    NSString *venueOpenText = [[NSString alloc] init];
    NSString *venueOpen = @"no";

  //  get components for today
    NSDateComponents *compsNow = [gregorian components:NSWeekdayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit fromDate:dateNow];

  //  get weekday for today and yesterday so we can lookup 4sq API
    NSInteger weekday = [compsNow weekday];
    NSInteger weekdayYesterday = (weekday>1)?weekday-1:7;

  //  look for todays' segment
    NSMutableArray *venueOpenSegments = [[NSMutableArray alloc] init];   //  stores all the segments when the venue is open
    for (NSDictionary *segment in hours){
      //  get today's segment (if it exists)
        if ([segment[@"days"] containsObject:[NSNumber numberWithInteger:weekday]]){
          for (NSDictionary *dictOpen in segment[@"open"])
            [venueOpenSegments insertObject:@{@"end": [dictOpen[@"end"] mutableCopy], @"start":[dictOpen[@"start"] mutableCopy]}.mutableCopy atIndex:venueOpenSegments.count];
        }

      //  check the day before if the venue is open past midnight
        if (([segment[@"days"] containsObject:[NSNumber numberWithInteger:weekdayYesterday]] && [segment[@"open"] count])){
          //  get the last segment (that should be the one passing midnight)
            NSDictionary *tempSegment = [segment[@"open"] lastObject];

            //  if it has more than 4 characters it's after midnight ("+02:00"), also, ignore if it closes at midnight
              if ([tempSegment[@"end"] length] > 4 && ![tempSegment[@"end"]isEqualToString:@"+0000"]){
                //  create a new segment that starts at midnight and lasts till the time it closes (early AMs usually)
                  lastSegmentYesterday = @{@"start":@"0000", @"end":[tempSegment[@"end"] substringFromIndex:1]};
              }
        }
      }

  //  add last night segment that passes midnight as the first segment of today
    if (lastSegmentYesterday.count){
      [venueOpenSegments insertObject:lastSegmentYesterday atIndex:0];
    }

// go through all the segments and find out if the venue is closed or open
  if (venueOpenSegments.count){
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
    timeFormatter.dateFormat = @"HH:mm";    //  set time output format

    int segmentNumber = 0;
    for (NSMutableDictionary *segment in venueOpenSegments){
      segmentNumber++;

      //  confing start date
        [comps setDay:compsNow.day];
        [comps setMonth:compsNow.month];
        [comps setYear:compsNow.year];
        [comps setHour:[[segment[@"start"] substringToIndex:2] intValue]];
        [comps setMinute:[[segment[@"start"] substringFromIndex:2] intValue]];
        NSDate *dateStart = [[[NSCalendar currentCalendar] dateFromComponents:comps] copy];

      //  config end date
        //  check if the segment goes to next day
          BOOL closesTomorrow = NO;
          if ( [segment[@"end"] length]==5 ){
            segment[@"end"] = [segment[@"end"] substringFromIndex:1];
            closesTomorrow = YES;
          }

          [comps setHour:[[segment[@"end"] substringToIndex:2] intValue]];
          [comps setMinute:[[segment[@"end"] substringFromIndex:2] intValue]];
          NSDate *dateEnd = [[[NSCalendar currentCalendar] dateFromComponents:comps] copy];

          //  add a day if it closes tomorrow
            if (closesTomorrow){
              NSDateComponents *nextDayComponent = [[NSDateComponents alloc] init];
              nextDayComponent.day = 1;
              dateEnd = [gregorian dateByAddingComponents:nextDayComponent toDate:dateEnd options:0];
            }

        //  start checking if it's open or closed
          //  now < segment start
            if ([dateNow compare:dateStart] == NSOrderedAscending){
              venueOpenText = [NSString stringWithFormat:@"opens at %@",[timeFormatter stringFromDate: dateStart]];
              venueOpen = @"later";
              break;
            }

          //  segment end < now
            else if ([dateEnd compare:dateNow] == NSOrderedAscending){
              if (segmentNumber == venueOpenSegments.count){
                venueOpenText = [NSString stringWithFormat:@"closed since %@",[timeFormatter stringFromDate: dateEnd]];
                break;
              }
              continue;
            }

          //  segment start < now < segment end
            else if ([dateStart compare:dateNow] == NSOrderedAscending && [dateNow compare:dateEnd] == NSOrderedAscending){
              venueOpenText = [NSString stringWithFormat:@"open till %@",[timeFormatter stringFromDate: dateEnd]];
              venueOpen = @"yes";
              break;
            }

          //  rare but possible... last minute of the venue being open (I treat it as closed)
            else {
              venueOpenText = @"closing right now";
            }
        }
    }
    else venueOpen = @"closed today";    //  no segments for today, so it's closed for the dayæ

  //  return results
    return      @{@"open":venueOpen, @"string":venueOpenText};
}

我更新了我的UILabel:

NSDictionary *venueOpen = [self isVenueOpenDictionaryForHours:_arrayVenues[indexPath.row][@"hours"]];
label.text = venueOpen[@"string"];
if ([venueOpen[@"open"] isEqualToString:@"no"]){
    label.textColor = [UIColor colorWithHexString:@"b91d47" alpha:1];   // red
} else if ([venueOpen[@"open"] isEqualToString:@"yes"]) {
    label.textColor = [UIColor colorWithHexString:@"1e7145" alpha:1];   // green
} else if ([venueOpen[@"open"] isEqualToString:@"later"]) {
    label.textColor = [UIColor colorWithHexString:@"e3a21a" alpha:1];   // yellow
}

顺便说一句,我对pod 'HexColors'方法使用colorWithHexString