Hello everyone, I want to synchronize the system time with server time, I receive the server time in milliseconds,
how can I sync the system time with this time??
They are about 250 milliseconds apart and I want them to be exactly the same because this is very important for my app.
I don't want to use datetime.now increment or decrement method!!
Looking for a better solution if there is one
That indeed sounds like the easiest solution here. ?
B4X:
''They are about 250 milliseconds apart'' per original post
Dim ServerOffsetTicks As Int = 234 'eg server clock 234 ms ahead of device clock
Dim LocalTicks As Long = DateTime.Now
Dim ServerTicks As Long = LocalTicks + ServerOffsetTicks
DateTime.TimeFormat = "HH:mm:ss.SSS"
Log("Device clock = " & DateTime.Time(LocalTicks))
Log("Server clock = " & DateTime.Time(ServerTicks))
I don't think you will ever be able to get them synchronized as the delay from the the internet is not constant.
When you request the time from a time server, you only get the time when it actioned the request, not the current time, which then takes time to come back to you.
I suppose the closest you can get is request the time. Do something like tracert/ping on the server and use the average response time to modify the time given.
I don't think you will ever be able to get them synchronized as the delay from the the internet is not constant.
When you request the time from a time server, you only get the time when it actioned the request, not the current time, which then takes time to come back to you.
I suppose the closest you can get is request the time. Do something like tracert/ping on the server and use the average response time to modify the time given.
The app runs on a vps server and according to my measurements, the ping is about 5 milliseconds, and I think it can be ignored. Or at least I can add that value to the final time when calculating the difference between the two times.
Even better: add half of the round-trip time, since when you receive the server time reply, it is telling you the server time when it sent you that reply one network trip ago.
The problem for you here is that the server time and the device time are already synchronised - they are both working on UTC time. As @emexes suggests, a better approach would be to make an estimate using the delay time which is in any case likely to vary.
Do you *really* need to change the system time of the local device, or is it enough to just know what the server time currently is?
Even better: add half of the round-trip time, since when you receive the server time reply, it is telling you the server time when it sent you that reply one network trip ago
Do you *really* need to change the system time of the local device, or is it enough to just know what the server time currently is?
Even better: add half of the round-trip time, since when you receive the server time reply, it is telling you the server time when it sent you that reply one network trip ago.
That indeed sounds like the easiest solution here. ?
B4X:
''They are about 250 milliseconds apart'' per original post
Dim ServerOffsetTicks As Int = 234 'eg server clock 234 ms ahead of device clock
Dim LocalTicks As Long = DateTime.Now
Dim ServerTicks As Long = LocalTicks + ServerOffsetTicks
DateTime.TimeFormat = "HH:mm:ss.SSS"
Log("Device clock = " & DateTime.Time(LocalTicks))
Log("Server clock = " & DateTime.Time(ServerTicks))