开发者

iPhone - comparing dates not working as expected

开发者 https://www.devze.com 2023-03-29 18:17 出处:网络
I have an event that is set to a particular date. I want to check if the date is within 5 minutes range of the current date. For example: if the event is set to 9:10 pm, and now is 9:02 pm. I want to

I have an event that is set to a particular date. I want to check if the date is within 5 minutes range of the current date. For example: if the event is set to 9:10 pm, and now is 9:02 pm. I want to see if the current time is between 9:05 and 9:15 pm.

So, I do...

NSDate *currentDate ... this variable contains the current date
NSDate *plusFive = [eventDate da开发者_开发百科teByAddingTimeInterval:300];
NSDate *minusFive = [eventDate dateByAddingTimeInterval:-300];

NSComparisonResult result1 = [eventDate compare:minusFive];
NSComparisonResult result2 = [eventDate compare:plusFive];


if ( (result1 == NSOrderedAscending) && (result2 == NSOrderedDescending) ) { 
    // if the current date is not inside the 5 minutes window
    // or in other words, if the currentDate is lower than minusFive and bigger
    // than plusFive
    [self currentDateIsOutsideTheWindow];
} else {
    [self currentDateIsInsideTheWindow];
}

the problem is that result1 and result2 always gives me NSOrderedAscending as a result, for any date.

Any clues?

thanks.


Try the following code.

NSTimeInterval ti = [eventDate timeIntervalSinceDate:currentDate];
if (ti > 0 && ti < 300) ...
else if (ti < 0 && ti > -300) ...


I think you should switch around eventDate and currentDate in your calculation.


I think EmptyStack's solution is the best for this problem, but I'd just simplify it as follows:

if( fabs([eventDate timeIntervalSinceNow]) <= 300.0 ) {
  // within the window
} else {
  // outside the window
}

I was curious about this... so I wrote a quick test and everything works as expected for me. Given the sample code below, in the first run, the plus five is NSOrderedAscending when compared with now, as expected, and the minus five is NSOrderedDescending. In the second run, plus five is NSOrderedAscending and minus five is NSOrderedDescending when compared with a date three minutes from now. Finally, plus five is NSOrderedDescending and minus five is also NSOrderedDescending when compared with a date twenty minutes from now, again, all as expected.

// datetest.m
// clang -o datetest datetest.m -framework foundation

#import <Foundation/Foundation.h>

void test_compare(NSDate* event_date)
{
    NSDate* dateNow = [NSDate date];

    NSDate* minusFive = [event_date dateByAddingTimeInterval:-300.0];
    NSDate* plusFive = [event_date dateByAddingTimeInterval:300.0];

    NSComparisonResult minusFiveResult = [dateNow compare:minusFive];
    NSComparisonResult plusFiveResult = [dateNow compare:plusFive];

    NSLog(@"P5: %ld, M5: %ld; dates (now, event, p5, m5):\n\t%@\n\t%@\n\t%@\n\t%@", (long int)plusFiveResult, (long int)minusFiveResult, 
        dateNow, event_date, plusFive, minusFive);  
}

int main (int argc, char const *argv[])
{
    NSAutoreleasePool *pool = [NSAutoreleasePool new];

    // test for an event that's "now"
    NSDate* now = [NSDate date];
    test_compare(now);

    // test for an event that's three minutes from now
    NSDate* threeMinutesLater = [now dateByAddingTimeInterval:180.0];
    test_compare(threeMinutesLater);

    // test for an event that's 20 minutes ago
    NSDate* twentyMinutesBefore = [now dateByAddingTimeInterval:-1200.0];
    test_compare(twentyMinutesBefore);

    [pool drain];
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消