B4J Question how sync system time with server time?

behnam_tr

Active Member
Licensed User
Longtime User
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
 
Last edited:
Solution
Now the only solution that comes to my mind and I did it is to define a variable to store the time difference and then use it in the whole program.

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))

Daestrum

Expert
Licensed User
Longtime User
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.

Just a thought.
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
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.

Just a thought.
thanks

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.
 
Upvote 0

emexes

Expert
Licensed User
I want to synchronize the system time with server time

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?

Or at least I can add that value to the final time

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.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
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.
 
Upvote 0

behnam_tr

Active Member
Licensed User
Longtime User
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.
I get server time with rest api, I just want to sync system time with server time, 100ms difference is acceptable but not more

Now the only solution that comes to my mind and I did it is to define a variable to store the time difference and then use it in the whole program.

dim diff as int= datatime.now - servertime
dim synctime= datatime.now - diff
 
Upvote 0

emexes

Expert
Licensed User
Now the only solution that comes to my mind and I did it is to define a variable to store the time difference and then use it in the whole program.

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))
 
Upvote 0
Solution
Top