Home > Back-end >  UTC Date Time not matching between two machines
UTC Date Time not matching between two machines

Time:10-14

I have two projects, one written in JS and another in Python. I have a functionality that requires to calculate the current UTC time difference coming from both projects. When I test the current UTC time I notice there is always the exact difference, and I'm expecting it to be almost equal.

The time difference calculation is done in the JS project. Using a websocket, I'm getting the UTC time from the Python project and subtract Math.round(new Date().getTime()) from it. The difference is almost always the same, but also unacceptable.

This is how I'm getting the UTC time from Python:

time = datetime.datetime.now(datetime.timezone.utc)
time = time.replace(tzinfo=datetime.timezone.utc)
time = time.timestamp()

I'm running the JS project on my windows WSL environment. When I run the python project from a local (WSL) docker container I get a difference of 7.02 seconds. When instead I run the python project from an LXC container on my raspberry pi, I'm getting around 3.04 seconds.

I need to have this difference equal down to the second. Is there something I can do about it?

CodePudding user response:

I solved this using ntpdate

I have installed this package apt install ntpdate and ran ntpdate -u pool.ntp.org. What this command does it to sync your system's clock. I did this on both of my systems and I had no issues with mismatch utc timestamp.

Of course I wonder if, when JS is ran on and android device, would that system be syncronized too? I'm assuming yes for now :)

  • Related