0

I have some data like these:

Monday: from 8.00am to 13.pm; 14:00 pm to 20.00pm 
Tuesdays: from 8.00am to 10 am; 13.00 pm to 17.00 pm; from 17.00 pm to 23.00 pm;
.....

Now I would to check if a NSDate (for example now NSDate) is contained in some interval... Is there something for this case?

Safari
  • 11,437
  • 24
  • 91
  • 191
  • 1
    You'd store the start and end dates and then compare the "test" date with each. What's so hard about that? – trojanfoe Jun 09 '14 at 15:33
  • efficiency..for example.. – Safari Jun 09 '14 at 15:37
  • How so? You need to define those ranges so you need to store start/end and then you need to compare each range. I think that's the only way it can be done. – trojanfoe Jun 09 '14 at 15:38
  • 1
    I agree with @trojanfoe. There is no O(1) way or any convenient methods for this. The best way is to store your interval in some kind of structure that help you search through it fast. – 3329 Jun 09 '14 at 15:43

1 Answers1

0

NSDate is a very thin wrapper on a double precision floating point value (the number of seconds since January 1, 2001.)

NSDate comparisons are very fast.

If you're really worried about it, convert your dates to NSTimeInterval values (doubles) and then do numerical comparisons:

NSTimeInterval startInterval = [startDate timeIntervalSinceReferenceDate];
NSTimeInterval endInterval = [endDate timeIntervalSinceReferenceDate];

NSTimeInterval someDateInterval = [someDate timeIntervalSinceReferenceDate];

if (someDateInterval >= startInterval && someDateInterval <= endInterval)
  //the date someDate is between startDate and endDate.

Obviously, you'd get the starting and ending time intervals once, then use those to compare each candidate date.

Duncan C
  • 128,072
  • 22
  • 173
  • 272