0

In my iPhone application, I need to calculate the time difference between the time a message was created on the server, and the time my phone received it.

The server (Java) puts in a number returned by System.currentTimeMillis() as metadata along with the message.

How do I compare this number with the current time on the device? Could not find a suitable NSDate method to do this comparison.

Thanks in advance!

2 Answers2

1

You can use (NUInteger) ([[NSDate date] timeIntervalSince1970] * 1000)

gvaish
  • 9,374
  • 3
  • 38
  • 43
  • But note that the "time interval" methods return a `double` value in seconds. You have to do some (very minor) conversions to get milliseconds. – Hot Licks Sep 26 '13 at 17:41
  • And note too that there are methods to go the other way -- convert a time interval into an NSDate. – Hot Licks Sep 26 '13 at 17:41
  • Multiply by 1000 and type case to long or NUInteger – gvaish Sep 26 '13 at 20:25
  • I can't find an exact reference for the exact time difference between Apple's 'GMT' (UT1, I assume) 1 January 1970 reference date and Java's UTC but it'll have been at most 900ms and likely much less. I guess as the author is taking the time from two different clocks anyway the difference is safe to ignore? – Tommy Sep 26 '13 at 23:13
  • Epoch / 1970 time = 1st Jan 1970 at 00:00hrs. – gvaish Sep 27 '13 at 17:03
1

You might take a look at this SO answer and the -timeIntervalSinceDate: method.

Community
  • 1
  • 1
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345