0

I get a Unix timestamp (Created at time) from server of which I get the NSDate object using :

NSTimeInterval interval = [str doubleValue];    
NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:interval];

I need to find the time difference between the above created time and current time and display in hh:mm:ss format. I coded it :-

    NSTimeInterval timeDiff = [agent.chatStartTimeStamp timeIntervalSinceNow];
    // NSDate *now = [NSDate date];
    // timeDiff = [now timeIntervalSinceDate:agent.chatStartTimeStamp];  // RETURNS NEGATIVE

    // Divide the interval by 3600 and keep the quotient and remainder
    div_t h = div(timeDiff, 3600);
    int hours = h.quot;
    // Divide the remainder by 60; the quotient is minutes, the remainder
    // is seconds.
    div_t m = div(h.rem, 60);
    int minutes = m.quot;
    int seconds = m.rem;

    NSString *str = [NSString stringWithFormat:@"%d:%d%d", hours, minutes, seconds];
    cell.timeLabel.text = str;

    NSLog(@"*********** CV CTRL - AGENT CHATSTARTTIME - %@  TIME DIFFERNCE = %f  STR = %@", agent.chatStartTimeStamp, timeDiff, str);

The logs for the above 2 codes -

AGENT CHATSTART TIME - 1403342129.980000  SET TIME - 2014-06-21 09:15:29 +0000

*********** CV CTRL - AGENT CHATSTARTTIME - 2014-06-21 09:15:29 +0000  TIME DIFFERNCE = 130.419857  STR = 0:210

The above code gives me results as - suppose the value is 0:2:10, then this value reduces to 0:1:40, 0:1:6, 0:-1....

What I am looking out is - the time difference should increase as the created at time will be something before/earlier current time only. So I want that value of startTime should be deducted from now i.e. now - startTime (time). And I believe this will give me results as I am expecting. I tried with [now timeIntervalSinceDate:agent.chatStartTimeStamp]; but that returns negative response.

UPDATE

This is how I convert the unix timestamp to loca time :-

+ (NSDate *)getNSDateFromUnixTimeStamp : (NSString *) unixTime {
NSString *str = [NSString stringWithFormat:@"%f",[unixTime doubleValue]/(double)1000];

NSTimeInterval interval = [str doubleValue];

NSDate *timeStamp = [NSDate dateWithTimeIntervalSince1970:interval];

str = nil;
unixTime = nil;

return timeStamp;

}

And log for the same :-

2014-06-23 12:24:38.046 MintChat[1021:70b] AGENT CHATSTART TIME - 1403506610.771000  SET TIME - 2014-06-23 06:56:50 +0000

My system time is 12:24:38 & the unixtimestamp is also the current time at just few secs before, so I guess shouldn't the unix time set should also have time as almost same.

Can anyone help me how to get this simple time difference. Where am I going wrong ? I searched a lot on the subject, but couldn't get the expected results.

Any help is highly appreciated.

Thanks

Tvd
  • 4,463
  • 18
  • 79
  • 125

1 Answers1

1

Your commented code, [now timeIntervalSinceDate:agent.chatStartTimeStamp]; is correct. From the NSDate documentation -

Return Value

The interval between the receiver and the current date and time. If the receiver is earlier than the current date and time, the return value is negative.

So, you can simply take the absolute value to get the number of seconds -

NSTimeInterval timeDiff = fabs([now timeIntervalSinceDate:agent.chatStartTimeStamp]);

Once you have the time interval you can use this answer to format it -

NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeDiff];    
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"hh:mm:ss %@", formattedDate);
Community
  • 1
  • 1
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Thanks for your guidance. Your code definetly helped me get rid of the negative results. Still the result is like 00:2:10...00:1:50...00:0:05...00:0:01....00:1:10... So from 2:10 it deducts till 0 and then again increases as per current time. This must be b'coz the unix time I get from server (aligned with NTP) must be greater than the current time. So, the simulator's time is behind can that be assumed. Can this happen with live device too ??? – Tvd Jun 21 '14 at 13:01
  • I wouldn't expect the simulator's time to be that far off - you should be able to verify from the clock on the titlebar. Assuming `agent.chatStartTimeStamp` remains fixed the time should increase. Have you logged the values? – Paulw11 Jun 21 '14 at 21:52
  • RCVD from unix time from server Log - 2014-06-23 12:15:04.262 MintChat[412:70b] AGENT CHATSTART TIME - 1403506036.960000 SET TIME - 2014-06-23 06:47:16 +0000 Log when displaying on screen - 2014-06-23 12:15:04.282 MintChat[412:70b] *********** CV CTRL - AGENT CHATSTARTTIME - 2014-06-23 06:47:16 +0000 NOW = 2014-06-23 06:45:04 +0000 TIME DIFFERNCE = 132.678435 STR = 00:02:12 – Tvd Jun 23 '14 at 06:47
  • See the log time of both logs in the same "" and also agent chatStartTime is same. NOW is the now time - NSDate *now = [NSDate date]; timeDiff = fabs([now timeIntervalSinceDate:agent.chatStartTimeStamp]); . – Tvd Jun 23 '14 at 06:50
  • That looks strange because your `[NSDate date]` is two minutes behind your chat start and I would also expect the local machine time (12:15) to have the same minutes, although if your local time zone is +5.5 hours from UTC then it looks OK - 12:15 vs 6:45 – Paulw11 Jun 23 '14 at 06:57
  • Hmm, something strange happening!!! Logs time is 12:15, title bar time on simulator also says 12:15, then how come the value of Now i.e NSDate has 06:45 Unix time also has 06:47. How come it is 06 when right now in noon 12:15 ??? – Tvd Jun 23 '14 at 06:58
  • Also, you can update your question rather than using a comment as it allows more space and better formatting – Paulw11 Jun 23 '14 at 06:59
  • What is your local time zone? – Paulw11 Jun 23 '14 at 07:01
  • IST - Mumbai, India. Right now its 12:34:56. – Tvd Jun 23 '14 at 07:03
  • Ok, that is utc+5 1/2 so that looks ok – Paulw11 Jun 23 '14 at 07:11
  • Then how to manage this 2:10 mins difference ? If the device time zone is something different then what it will show different results for different time zones ?? – Tvd Jun 23 '14 at 07:12
  • Oh, so the unix timestamp is converted in GMT and the current time I also get is in GMT. I need to convert the Unix timestamp in local time zone and also get current time in local time zone. May be that will solve the problem !!! – Tvd Jun 23 '14 at 07:49
  • Both times were reported in the same timezone - utc. So that isn't a problem. I could understand if the chat start time was 2 mins earlier than local time because that would indicate a chat that started two minutes ago. Perhaps you need to retrieve both chat start time and 'now' from your server and use that to calculate the offset between device time and server time – Paulw11 Jun 23 '14 at 07:53
  • I didn't get you clearly. >> to calculate the offset between device time and server time. You mean I should retrieve 'now' value also from server and not use local time thru simulator !! Is it so ? – Tvd Jun 23 '14 at 08:30
  • Any idea, how can I save these dates (unix timestamp & now) in local timezones ?? – Tvd Jun 23 '14 at 08:54
  • I am thinking that when you first connect to the server you can retrieve the servers current time and calculate the offset between device time and server time and use that in your calculations – Paulw11 Jun 23 '14 at 09:23