Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the current time is within a specified range in ios? [duplicate]

Possible Duplicate:
Determine if current local time is between two times (ignoring the date portion)

In iOS, how can I do the following:

I have two NSDate objects that represent the opening and closing times for a store. The times within these objects are accurate but the date is unspecified (the store opens and closes at the same time regardless of the date). How can I check if the current time falls between in this time frame?

Note, if it would help for the opening and closing times to be in another format other than NSDate objects, I'm fine with that. Currently, I'm just reading in a date string such as "12:30" from a file and using date formatter to create a matching NSDate object.

like image 486
Nosrettap Avatar asked Dec 07 '22 11:12

Nosrettap


1 Answers

Update: Note that this solution is specific to your case and assumes that store opening hours don't span two days. For example it won't work if the opening hour goes from Monday 9pm to Tuesday 10am. Since 10pm is after 9pm but not before 10am (within a day). So keep that in mind.

I cooked up a function which will tell you if the time of one date is between two other dates (it ignores the year, month and day). There's also a second helper function which gives you a new NSDate with the year, month and day components "neutralized" (eg. set to some static value).

The idea is to set the year, month and day components to be the same between all dates so that the comparison will only rely on the time.

I'm not sure if it's the most efficient approach, but it works.

- (NSDate *)dateByNeutralizingDateComponentsOfDate:(NSDate *)originalDate {
    NSCalendar *gregorian = [[[NSCalendar alloc]
                              initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

    // Get the components for this date
    NSDateComponents *components = [gregorian components:  (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate: originalDate];

    // Set the year, month and day to some values (the values are arbitrary)
    [components setYear:2000];
    [components setMonth:1];
    [components setDay:1];

    return [gregorian dateFromComponents:components];
}

- (BOOL)isTimeOfDate:(NSDate *)targetDate betweenStartDate:(NSDate *)startDate andEndDate:(NSDate *)endDate {
    if (!targetDate || !startDate || !endDate) {
        return NO;
    }

    // Make sure all the dates have the same date component.
    NSDate *newStartDate = [self dateByNeutralizingDateComponentsOfDate:startDate];
    NSDate *newEndDate = [self dateByNeutralizingDateComponentsOfDate:endDate];
    NSDate *newTargetDate = [self dateByNeutralizingDateComponentsOfDate:targetDate];

    // Compare the target with the start and end dates
    NSComparisonResult compareTargetToStart = [newTargetDate compare:newStartDate];
    NSComparisonResult compareTargetToEnd = [newTargetDate compare:newEndDate];

    return (compareTargetToStart == NSOrderedDescending && compareTargetToEnd == NSOrderedAscending);
}

I used this code to test it. You can see that the year, month and days are set to some random values and don't affect the time checking.

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"yyyy:MM:dd HH:mm:ss"];

NSDate *openingDate = [dateFormatter dateFromString:@"2012:03:12 12:30:12"];
NSDate *closingDate = [dateFormatter dateFromString:@"1983:11:01 17:12:00"];
NSDate *targetDate = [dateFormatter dateFromString:@"2034:09:24 14:15:54"];

if ([self isTimeOfDate:targetDate betweenStartDate:openingDate andEndDate:closingDate]) {
    NSLog(@"TARGET IS INSIDE!");
}else {
    NSLog(@"TARGET IS NOT INSIDE!");
}
like image 53
nebs Avatar answered Jan 05 '23 00:01

nebs