1

I am trying to get time from the internet and do not want want to use the device time. I am doing the following sntp client request for time, but as expected the time received is not accurate due to request - response time. How do I overcome the latency and get a maximum variation of +/- 20ms??

 Long time=0; 
    while (true) {
     SntpClient client = new SntpClient();
     if (client.requestTime("time-a.nist.gov",6000)) {
       long now = client.getNtpTime() + SystemClock.elapsedRealtime()
                        - client.getNtpTimeReference();
       Log.d("Time Difference", time-now+"");
       time=now;
       Thread.sleep(5000);
     }
    }

The log file is as follows:

- 02-10 23:27:16.125: E/Time Difference(15566): -5804
- 02-10 23:27:21.449: E/Time Difference(15566): -5272
- 02-10 23:27:26.774: E/Time Difference(15566): -5358
- 02-10 23:27:32.206: E/Time Difference(15566): -5365
- 02-10 23:27:37.528: E/Time Difference(15566): -5328
- 02-10 23:27:43.056: E/Time Difference(15566): -5429
- 02-10 23:27:48.892: E/Time Difference(15566): -5694
- 02-10 23:27:54.730: E/Time Difference(15566): -5972
- 02-10 23:28:00.563: E/Time Difference(15566): -5679
- 02-10 23:28:06.096: E/Time Difference(15566): -5686

The time difference is expected to be constant 5000 at perfect execution.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
gaurav
  • 3,583
  • 2
  • 13
  • 6
  • 2
    "The time difference is expected to be constant 5000 at perfect execution" -- Android is not a RTOS. Furthermore, `sleep()` is not accurate "at perfect execution" -- all it does is arrange for the thread to be available for scheduling after that number of milliseconds. When the thread *will* start executing instructions is up to the OS, and it will depend upon what else is going on with the device. – CommonsWare Feb 10 '14 at 18:06
  • So can I assume variations in my values are due to the sleep() and not latency? – gaurav Feb 10 '14 at 18:24
  • I am saying that relying on `sleep()` to be accurate is a flawed approach to your testing. – CommonsWare Feb 10 '14 at 18:26
  • Check out [this question][1]. There are several methods listed [1]: http://stackoverflow.com/questions/13064750/how-to-get-current-time-from-internet-in-android – Sam Feb 10 '14 at 19:23

3 Answers3

0

How do I overcome the latency and get a maximum variation of +/- 20ms??

I don't know about +/- 20ms, but assuming the Android device in question has GPS, you could could get the time from the GPS sub-system, as GPS time is (has to be) very precise. You can use requestSingleUpdate for that as discussed in this question and its answers.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • "did you manage to sync with GPS? As far as I know, Location.getTime provides the system-time, when the location update was obtained, i.e. no GPS clock is involved on this layer". This is one of the answer and as i said I dont want system-time – gaurav Feb 10 '14 at 18:22
  • @user1980743: Do we know that answer is correct? But yeah, it does seem like even if the GPS clock is involved, it's still going to be a translation of that against the device's current date/time, which may not be helpful... But if you just need 5000ms spacing, don't use `getTime`, use [`getElapsedRealtimeNanos`](http://developer.android.com/reference/android/location/Location.html#getElapsedRealtimeNanos()). I didn't say "use `getTime`". :-) – T.J. Crowder Feb 10 '14 at 18:26
  • no its not about 5000ms, 5000ms is for my testing. I need a reference clock over two or more devices to be same. – gaurav Feb 10 '14 at 18:43
0

client.requestTime is a network operation. I can never take less than a certain amount of time, over which you have absolutely no control.

The variations you observe are dictated by fluctuations in connexion time, network quality...

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • So there is no way of getting accurate time on the android device? – gaurav Feb 10 '14 at 18:50
  • `client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference()` is quite accurate. NtpTimeReference is, IIUC, the time of the system when the time was update from the network. `SystemClock.elapsedRealtime() - client.getNtpTimeReference()` tells you how close you are from that moment. – njzk2 Feb 10 '14 at 18:54
-1

In java. For sleep I use this bit.

long start = System.currentTimeMillis();
long current = start;
while(current - start < 5000){
    current = System.currentTimeMillis();     
}

that 5 lines is my "Sleep" code. try using that and see what results you get.

Dave P
  • 156
  • 1
  • 8
  • no improvement in the result 02-11 00:09:46.519: E/Time Difference(17610): -5716 02-11 00:09:51.950: E/Time Difference(17610): -5378 02-11 00:09:57.374: E/Time Difference(17610): -5460 02-11 00:10:02.850: E/Time Difference(17610): -5523 02-11 00:10:08.439: E/Time Difference(17610): -5575 02-11 00:10:13.783: E/Time Difference(17610): -5269 02-11 00:10:19.184: E/Time Difference(17610): -5438 – gaurav Feb 10 '14 at 18:41
  • Are u trying to get results like 5020 or less? – Dave P Feb 10 '14 at 19:30
  • I just want the time received to be consistent. – gaurav Feb 11 '14 at 05:18
  • So ya. Nothing wrong with your code. It just the network that messing you up. OR find a better / faster server or build one on your computer. – Dave P Feb 13 '14 at 21:41