简体   繁体   中英

Comparison between two times in objective-c

I have array with times like below

         "00:00",
         "01:25",
         "02:00",
         "02:35",
         "04:35",
         "05:00",
         "06:00",
         "07:00",
         "09:00",
         "16:30",
         "17:30",
         "18:00",
         "18:30",
         "19:30",
         "21:30",
         "22:00",
         "22:30",
         "23:00"

how get index of object which time near to current time?

You can get the time difference like this

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinCalendarUnit) fromDate:today];

[components setHour:otherHour];
[components setMinute:otherMinute];

NSDate *otherDate = [gregorian dateFromComponents:components];

NSTimeInterval timeDifferenceBetweenDates = [today timeIntervalSinceDate:otherDate];

do that for all your hours/minutes and take the minimum absolute time interval.

I haven't had very much experience in date and time programming in ObjC, but I think you could find relevant stuff in the Calendrical Calculations part of Date and Time Programming Guide. Also check out the documentation of NSDateComponents .

If you are trying to do this with strings array use NSDateFormatter to convert this strings to NSDate objects. After that you can compare values with current date. You can do something like this:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
NSDate *formattedCurrentDate = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]];

for(NSString *stringTime in times) {
    NSDate *dateFromString = [dateFormat dateFromString:stringTime];
    if([formattedCurrentDate earlierDate:dateFromString]) {
        // current time is earlier that  time
        (...)
    }
}

You might want to use the timeIntervalSinceDate method. Loop through your array and keep track of the minimum TimeInterval. Store the date with the minimum

NSTimeInterval *minimumInterval;
NSDate *closestDate;

forin(NSDate *someTime in timeArray)
{
    NSTimeInterval timeInt = [[NSDate date] timeIntervalSinceDate:someTime]; //someTime = any date from your array

    //compare timeInterval here
    if(minumumInterval > timeInt || minimumInterval == nil)
    {
        minimumInterval = timeInt;
        closestDate = someTime;
    }
}

At the end of this loop you will have the closest date from the array to the current time.

This will do the trick for you.

I'm not the best at C++, but I can give you a little hint in the direction I would follow:

This could be done like some of the bytes to kb/mb/gb parsers, the way I would do it, would be to change the times to "military" times instead, that way you can store the values as integers, like so:

0000,
0125,
0200,
0235,
...

this of course has the obvious flaw that 0000 is lower than 2300, but here goes:

if you then iteriate over the array and compare the values:

lets assume we have the time 12:00, that should of course translate to 1200, so:

int array_length = arr.length; // we store this for later use
int cur_time = 1200;

for (int i=0; i<array_length; i++)
{
  if (cur_time > arr[i] and < arr[i+1])
  {
    // this should be close enough. first time around.
    int array_index = i;
  }
}

you then know the index value of the "assumed" closest value and compare that to the lower and higher part of the index, of course check if it was either the first or the last value in the array:

int assumed_value = arr[array_index];
int higher_value = 0;

if (array_index == array_length) {
  // take the first value as the "highest"
  higher_value = arr[0];
} else {
  // otherwise just do your thing:
  higher_value = arr[array_index + 1];
}

we should then have (in our example):

assumed_value = 0900;
higher_value = 1630;
// and just for reference:
cur_time = 1200;

and now we find the value closest to the one we want:

int highest_value = higher_value - cur_time; // 430;
int lowest_value = cur_time - lower_value; // 300;

if (highest_value > lowest_value)
{
   // we have a winner, the assumed value was closest!
}
else
{
   // otherwise the highest_value is the closest one, but not in our use case
}

oh and btw. I think I just made up my own language there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM