0

Currently working on finding a solution to find the closest date compared to the current date using milliseconds. I understand part of the logic that needs to happen, but still a bit fuzzy about it all.

Looking to display the next tide relate to the current time. Get the index and display that data.

I know I would find the delta and compare the 2 times and the the one with the smallest delta correct? And iterate over them all.

Below is my code so far that works, I just need the conditional and logic worked out

NSNumber *tideDateEPOCH = [eachTideSummary valueForKeyPath:@"date.epoch"];

NSTimeInterval dateDeltaInterval = [tideDateEPOCH doubleValue];

NSTimeInterval dateDelta = dateDeltaInterval -  todayInEPOCH;

NSLog(@"tideDateEPOCH: %@", tideDateEPOCH);
NSLog(@"dateDelta: %f", dateDelta);

// Perform if conditional to see if the tide time array is the closest to the current date
// Return that index, then grab that index, display data within that index to the user (this is the next time information)

Below is a sample of my tideSummary array being returning. See date.epoch

"tideSummary": [
            {
                "date": {
                    "pretty": "11:58 AM PST on December 19, 2013",
                    "year": "2013",
                    "mon": "12",
                    "mday": "19",
                    "hour": "11",
                    "min": "58",
                    "tzname": "America/Los_Angeles",
                    "epoch": "1387483136"
                },

Logic I have in mind (needs some help and refining)

  • Perform a for loop to loop through each index of the tideSummary array
  • Inside of the loop find out the difference in time from the currentTime to tideSummary.date.epoch
  • This is where I am confused. We have the difference between the 2 times above. Do I want to save the smallest time out of all the indexes and return that min index value
  • Once we return the smallest delta index value from the array. We can then grab this index with say: [tideSummaryArray objectAtIndex:nextTideIndex];

The data returning looks like it is sorted. Does that sound right? I found other posts that deal with NSDate - but if I have my time in EPOCH wouldnt that be easier/faster? Thoughts?

  • I'm a bit confused on the logic too. What is it you want to do? What is `EPOCH`? – Hot Licks Jan 02 '14 at 01:12
  • Sure thing, see here: http://stackoverflow.com/questions/358207/iphone-how-to-get-current-milliseconds –  Jan 02 '14 at 01:19
  • But your EPOCH appears to have very little to do with that. And converting between NSDate and "time in epoch" is very simple -- use `timeIntervalSince...` and `dateWithTimeIntervalSince...`. – Hot Licks Jan 02 '14 at 01:23
  • @HotLicks I was using that post as an example. I can get the current date/time and convert but its the logic of finding the closest index of the dates that matches the current date/time. –  Jan 02 '14 at 01:24
  • (Understand that the general meaning of "epoch" is a point in time that the clock references a time zero. For the Western calendar that's Jan 1, 0001AD. For UNIX I'm thinking it's Jan 1, 1970. Others reference 1960, 2000, etc.) – Hot Licks Jan 02 '14 at 01:25
  • See ```tide.tideSummary``` and see the ```epoch``` - https://gist.github.com/ryancoughlin/8043604 - thats what I a can compare against. I want to display the next tide –  Jan 02 '14 at 01:26
  • That site appears to abuse the term "epoch" to mean "time relative to our epoch". Hard to say for sure what their epoch might be, but 1970 would be a good first guess. – Hot Licks Jan 02 '14 at 01:28
  • [This converter](http://www.epochconverter.com/) will convert back and forth between UNIX epoch and "normal" time. – Hot Licks Jan 02 '14 at 01:31
  • As to finding the closest time, that's for you to figure out. If you have a limited number of values to work with it's a simple matter of constructing an array and searching it. – Hot Licks Jan 02 '14 at 01:33
  • Sure I may have just not clarified my question enough. I can go back and edit. I want to find the closest index of my array to display the next tide in relation to the current time using ```ms``` –  Jan 02 '14 at 01:34
  • @HotLicks Similar to this but using milliseconds - http://stackoverflow.com/questions/14194870/nsdate-finding-nearest-date-to-today?rq=1 - my json object returns epoch time in milliseconds in ```tide.tideSummary.date.epoch``` - then id like to display that closest tide to the current time. –  Jan 02 '14 at 01:36
  • So, construct the array and search it. What is the issue? You still haven't asked a real question. – Hot Licks Jan 02 '14 at 01:42
  • @HotLicks That is just it. The question is what I stated above: Using the current time in ms and get the ```tideSummary.date.epoch``` find the delta and return the index - this will display the next tide time. –  Jan 02 '14 at 01:43
  • There is this operation that is generally referred to as "subtraction". Look it up. – Hot Licks Jan 02 '14 at 01:45
  • I am well aware of the subtraction. I have the delta returning in the code block posted. No help posting ignorant comments. Posted this and made edits to my question to help clarify. Appreciate the maturity. –  Jan 02 '14 at 01:46
  • (Maybe one point you're missing is that what Objective-C refers to as a "time interval" is a `double` expressed in seconds, while the "UNIX epoch time" is generally expressed in milliseconds.) – Hot Licks Jan 02 '14 at 01:47
  • Yes, I was also reading through this post: http://stackoverflow.com/questions/14194870/nsdate-finding-nearest-date-to-today/14194989#14194989 - I saw it was using NSDate to find the ```timeIntervalSinceDate``` - but the json returned from the server is in ```ms``` so I was wondering what is the best way to use that form and if it is faster than ```NSDate``` –  Jan 02 '14 at 01:50
  • Let's see: You say above "The data returning looks like it is sorted. Does that sound right?" Yet you do not even begin to define what "data returning" means. You misuse the term "epoch" (which is OK for a novice, but it doesn't help you to be understood). You say "Find the delta of current date - date from json index", which implies that you know how to do that. And you insult the only person who has bothered to try to help you. ***What is it you want to know?*** – Hot Licks Jan 02 '14 at 01:52
  • By no means trying to insult you. Trying here @HotLicks - and yes, I misunderstood the term EPOCH, I should have used time in ms if thats more accurate. Believe me, I am 100% appreciative of you helping me with this. Let me clarify my question with my flow more. One sec... –  Jan 02 '14 at 01:55
  • It is totally up to you whether you use NSTimeIntervals (floating-point seconds), UNIX epoch time values (integer milliseconds), or NSDates. The conversions between the three forms are trivial, so it makes little difference which you use. (Though frankly I don't like comparing NSDates because you always have to think backwards to interpret the result from `compare`.) – Hot Licks Jan 02 '14 at 01:55
  • Note that that site appears to always give the date twice, once in UTC and once in the local timezone. Objective-C NSDate values are always UTC, so you should probably use "utcdate". – Hot Licks Jan 02 '14 at 01:58
  • Yes, totally. I was thinking using integer milliseconds would be easier since its performing subtraction to get the delta of the 2 times (the current date - the index of the array ```tideSummary.date.epoch```. –  Jan 02 '14 at 01:59
  • @HotLicks Updated my question with a bit more info. Let me know if that helps clarify. I am basically trying to do the same thing that other question I linked does but using ms. Thoughts? Sorry again for the confusion. –  Jan 02 '14 at 02:03
  • @HotLicks Ok great, good to know about the "utcdate" - didnt know that. I was using "date" previously. –  Jan 02 '14 at 02:04

1 Answers1

0
NSInteger now = (NSInteger) ([[NSDate date] timeIntervalSince1970] * 1000);
NSArray* tideSummary = tide[@"tideSummary"];
for (NSDictionary* tideEntry in tideSummary) {
    NSDictionary* utcTime = tideEntry[@"utcdate"];
    NSString* timeString = utcTime[@"epoch"];
    NSInteger time = timeString.integerValue;
    NSInteger deltaTime = time - now;  // I'll let you define "closest"
}

(And it's just about as easy to use NSTimeIntervals instead.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • Once I get the ```deltaTime``` - ill have a conditional to check ```if deltaTime is less than now``` -> return closest time - but that doesnt necessarily return only 1 index value. It can return many. –  Jan 02 '14 at 02:19
  • You would almost need to do go through all of the ```deltaTime``` and find the smallest value, right? Then that value would be your index of the closest time? –  Jan 02 '14 at 02:21
  • Closest time meaning, the smallest ms difference between ```time - now``` or ```deltaTime``` –  Jan 02 '14 at 02:24
  • Note that for "smallest difference" you probably want the absolute value -- `abs(deltaTime)`. And, yes, it's up to you to decide how to iterate through the values and pick the closest. – Hot Licks Jan 02 '14 at 02:41