Naked Science Forum

Non Life Sciences => Geek Speak => Topic started by: remotemass on 19/06/2022 23:57:19

Title: Why different computers/devices may show different times (at seconds precision)?
Post by: remotemass on 19/06/2022 23:57:19
Hello,

I have a website that uses javascript to get the operating system time and display a clock on the browser. It is a completely client-side page but the problem I notice is that the operating systems in different devices will have different times. Apparently, you can use a software add-in (no advertising!) that makes your windows PC time quite accurate but to be honest I cannot trust this too much since different servers of time seem to produce different times if you want a great precision.
It seems it is definitely not possible to have milliseconds of precision in our systems these days, which is quite puzzling considering the sophistication of all our computers. Is there any reason why we are still in the stone age of precise clocks in our computers when we have the internet and so many sophisticated technologies?
I don't seem to be able to sync my static webpages that use the operating system clock when using different devices, like PC and iPhones...  Any help?

- remotemass
(My mentioned javascript static page at:
BeOmni_org)
Removed reference to a software product of unknown security... -mod
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: evan_au on 20/06/2022 09:58:33
If you have clients in multiple timezones, you need to ensure that you are using a common time reference eg UTC (Coordinated Universal Time), rather than the local timezone, otherwise there will be time differences of hours!

Modern server-based LINUX systems have different system calls that will retrieve the system time accurate to microseconds.
- Older operating systems had system calls that only retrieved the time in operating system "ticks" of around 10ms
- Use the most appropriate system calls available on each operating system

That is fine if you are the only application running, but in most modern systems, there are many processes competing for CPU attention, so your program may be suspended for a few milliseconds (or even seconds!) while other programs get their turn.
- You can ensure that reading the time and writing database records are placed as close as possible in code, to minimize the chance of these interruptions
- On some operating systems, you can elevate the priority of your process during these time-critical sections - providing this isn't done by all processes, all the time!
- But if your database is on the other side of the world, you can expect delays of hundreds of milliseconds between the time you initiate the transaction, and the time it completes.

Most of the time-keeping protocols like NTP (Network Time Protocol) and PTP (Precision Time Protocol, IEEE1588) assume that the networks are symmetrical. If the network is not symmetrical, then the time will be off, perhaps by milliseconds. This can occur due to:
- An asymmetric network, eg Mobile phones, HFC or DSL networks, where upstream & downstream directions operate at different speeds
- It can also happen if the network is more heavily loaded downstream than upstream (although there are some mechanisms in the protocols to to detect and correct for this)

An exercise for the reader: You could write a little utility that repeatedly calls the system time function, and see what is the typical interval between returned values
- You may find that it retrieves many sequential timestamps with 0μs interval, followed by one of 10,000μs
- You may find that it normally returns values that are a few μs apart, but when the system is busy, there are gaps of many ms.
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: alancalverd on 20/06/2022 09:59:45
Internet is not instantaneous. The displayed time is probably within a millisecond of the time the server compiled it, but its route from there to your computer is not defined.

Your best estimate of UTC will be the radio signal from your national standards laboratory.
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: Bored chemist on 20/06/2022 12:31:19
Your best estimate of UTC will be the radio signal from your national standards laboratory.
Sort of...
I'm about 200 Km from Anthorn so the signal will get here about 0.66 mS late.
Delays on the internet will be much longer (and more variable), but my computer is clever.
It can send signals back and to and thus measure the delay, and then it can allow for it.

It's possible that my computer's idea of the time is more accurate than my "radio controlled" clock's version.
I can't compare them to anything better than 1 second or so, and they usually agree.
But both drift a bit and so, if I really want them to tally, I have to force them to "calibrate".

Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: Deecart on 20/06/2022 18:12:11
Not sure why you want to set the time of every computer using an internet connection.
If the computers are at the same local network, use one computer to get the internet time.
On this computer mount some Time servicing service and let the other computers use this value.

Here is the procedure to create your own NTP service :
https://techlibrary.hpe.com/docs/otlink-wo/How-to-Configure-a-Local-NTP-Server.html
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: evan_au on 21/06/2022 03:26:28
Often your Internet Service Provider (ISP) will have an NTP server, whose time is set accurately across high-speed links.
- You can run NTP protocol to the ISP's NTP server. Most of the variation will occur in your connection to your ISP, which runs at the lowest speed on the path.

NTP has various tricks like:
- Measuring the time delay both ways (as mentioned by BC) and taking an average. Unfortunately, most internet access networks are not symmetrical, so the average might have an error of a few ms.
- Sending multiple messages both ways, and using the one(s) that experience the lowest delays
- Repeat this process multiple times per day. The results at 4am are likely to be more accurate than 8pm (fewer NETFLIX streams at 4am...).

There is another anomaly that affects computers occasionally: Leap Seconds
- A number of security protocols rely on accurate timestamps (to discourage replay attacks), and a leap second can make it look like a timestamp is out of date
- NTP includes advance warning of a leap second, but Leap Seconds occur so infrequently that most programmers ignore it
- To avoid this problem, Google decided to spread the leap second over 24 hours (a "leap smear"), instead of being concentrated in 1 second. That meant that Google Time was up to 0.5 seconds different from every other computer. It may have helped Google, but it probably confused some computers talking to Google!
https://developers.google.com/time/smear
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: SeanB on 23/06/2022 06:30:45
Accurate time on a PC is hard. The internal clock is run by the kernel, using a routine that is called by the 18.2Hz timer 1 interrupt, hard coded into the South bridge chipset now, but before done by a counter timer chip on the ISA bus, and with this incrementing the internal clock. This runs off the main clock, used to be 4.77MHz, derived from a roughly 14MHz clock crystal, and this would slowly drift.

Later on with Win9x you could get utilities that used NTP to slave this clock to the particular NTP pool you desired, and with XP this was built in, but by default not enabled ( you had to select it to be on), and from Win7 Microsoft ran their own NTP pool worldwide to serve a reasonably accurate clock to all computers, more for their own benefit so as to get upgrading to occur on time, and also keep Microsoft email servers synchronised, along with the mail clients.

NTP is subject to jitter, for the most case the most accurate clocks now are based on using a heavily smoothed GPS receiver at a fixed location, as GPS is by definition time based, and thus if your location is not moving, the time code received, once decoded and used to determine location, can be used to have an accurate local clock that is pretty much phased locked to the hydrogen MASER clocks that together set UTC.You use a very heavy filtering, and then use this smoothed clock to slowly trim the output of your clock, getting a time accurate to microseconds after a day or three of running, so that the residual errors can be averaged out. Used, along with a Cesium or Rubidium oscillator, to accurately set frequencies and timing for cellular networks, and also sent as part of the housekeeping data on mobile phone networks, so ironically your phone clock is the most accurate one you have.
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: alancalverd on 23/06/2022 22:48:21
And don't forget that not all countries subscribe to UTC. The legal time in Denmark, I understand, is not exactly UTC+1. 
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: evan_au on 24/06/2022 04:13:16
The legal time in Darwin (which I am planning to visit next week) is not exactly UTC+9.
- It's UTC+9.5

Adelaide is also UTC+9.5
- In 6 months time, it will be UTC+10.5
- This is not due to continental drift; 6 months ago it was also UTC+10.5
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: Bored chemist on 24/06/2022 08:59:12
The legal time in Denmark, I understand, is not exactly UTC+1. 
It was when I went to Legoland.
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: nicephotog on 11/08/2022 15:29:46
If you mean the difference between your PC and your mobile, it comes down to how and where your mobile service provider for the mobile sources its time to set in your phone, which, probably will be its' main network service computer, The main service server (at first full run of the OS install) may be set once by a special call to an internet clock or purely private line through to a clock such as in a NASA network service, time then on a mobile is set to the location timezone of the phone.
Some servers are simply a conversion from satellite spoken information time to the timezone for a service companies main server at OS install first run startup.
Internet web page javascript does have a USE ONCE ONLY call to an inter-networking clock after which the use of a javascript timer setTimeout(); should be used to tick over the time after the contents return of the call is extracted into hours minutes seconds, that javascript time call is returned in UTC so must be shifted into the correct timezone and perhaps 12 hour am pm time.
There may be apps out there that will mutually synchronize OS time with connected telephone time or other devices but will probably be the bias of the computer not the phone to commit the linkage and adjustment, though it may accept the phone time as the choice.
"Time" in computers is "relate"d to "Locale" in a detachable way(able to be Swahili language but located in France so in a French timezone though the language is African for location of timezone to be "assumed/expected")
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: syhprum on 16/08/2022 14:29:04
I do not normally need time displayed more accurately than plus/minus one second so I use this software that allows me to use various time servers with frequent up dates dsc400r-x64 Duality Software
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: SeanB on 16/08/2022 17:21:00
Most modern computer OS's will set time automatically, along with pretty every mobile phone as well, relying on accurate atomic clocks that are used to give an accurate clock. No need for any software these days, as the PC clock and phone will be slaved to an accurate reference regularly.
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: evan_au on 16/08/2022 22:53:18
Quote from: SeanB
No need for any software these days
Perhaps what you mean is that the software to set the time is built into the device.

After crossing the international date line (pre-COVID), my phone found the correct local time and date as soon as I switched it out of flight mode - probably from the local cellular network.

My fitness-tracker watch took about 3 days to catch up with the changed time (I raised a fault ticket on their website, so maybe they have improved it by now?).
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: SeanB on 17/08/2022 06:20:10
Yes a bug in the firmware, but simple to solve by simply turning it off then on again, as it will then sync to the time from your phone, as a 24 hour clock adjustment is disregarded as implausible in the majority of cases for correction. Yes the software is part of the OS these days, normally running as part of the kernel functions, as an accurate clock is needed for many processes in the computer file system, and thus got pulled out of user space.
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: nicephotog on 17/08/2022 08:28:29
I think you may be better looking at this
https://www.ntppool.org/en/
https://www.ntppool.org/en/join.html
In my Linux Slackware distro the setting board for the Date Time can set automatically from NTP Network Time Protocol from a server.
When i first learned acquiring time in javascript tutorials they simply teach you load the page and start a setTimeout() method counter and translate it from the moment it starts.
Most trouble in web pages arises with forms the user needs to synchronize over https and ssl that requires the clocks to be properly synchronized between the https / ssl server and the user machine clock on its m-board translated into the OS setting.
When you think about it, it may be a BIOS/UEFI setting being in the way of what you set for the software OS at install.
With web pages, largely the user must setup their OS and machine time properly themself if it will be tagged to its time zone, the problem too can be the timezone for most purpose can be set different to the physical location of the user machine and the SSL or HTTPS check will reveal a different location over the hops in the ISP's against their time and reject the information.

If you want to understand time better "download" and read this page part
Java JVM Time Zones (config and specs) and time manipulation methods
https://drive.google.com/file/d/1gbz54O8aOUEFOdRa40ZlRM7AtpS4Uj_C/view?usp=sharing
Though it is for "Java" the time aspect is standard IETF international ISO.
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: evan_au on 17/08/2022 10:25:24
As this graph shows, the slowly lengthening of the day is coming to a pause - perhaps in future the chronologists may need to insert a an extra second?
But it will probably need to be removed again, so why bother?
https://en.wikipedia.org/wiki/Day_length_fluctuations#/media/File:Deviation_of_day_length_from_SI_day.svg

Some of the proposed explanations include melting ice-caps on land (eg Greenland or Antarctica), which I expect would redistribute water towards the equator, slowing down the Earth's rotation (not speeding it up).
- Perhaps the gradual deposition and crystallisation of the liquid iron core onto the solid iron core has changed?
- Or the core (which seems to rotate at a different rate than the Earth's crust) might be coupling differently to the crust?
- Laboratory models of the geomagnetic dynamo seem to imply that you need differential rotation of the liquid metal core. So perhaps the slow weakening of the Earth's magnetic field might be related to the slowing of Earth's rotation? You would expect this to show a trend over the past 1,000 years... unfortunately, we only have detailed daily records back to the 1970s.
Title: Re: Why different computers/devices may show different times (at seconds precision)?
Post by: nicephotog on 18/08/2022 11:01:23
perhaps in future the chronologists may need to insert a an extra second?
Java 2 does adhere to the requirement of leap seconds
Leap second insertion is only used to bring clocks back into sync when that problem has become, and it is also based on the uneven 365/24hr cycle over time denoted by planetary orbit timing.
https://www.thecodeteacher.com/question/140639/datetime---How-does-the-(Oracle)-Java-JVM-know-a-leap-second-is-occurring?
In short, it is possible for Date Time objects to have a setting of 61 seconds even 63.
One thing about Java ZonedDateTime object methods for addSeconds() .... addYears() and the minus for the methods or time segment quantity extraction from two end points is the integer argument is calculated using the timezone and timezone history IANA database control file and other legal ISO shifts of national declared time change NOT simple linear addition subtraction , so it can be easy to expect an outcome by perfect mental arithmetic but the IANA database rules shift out or put in extra quantities of time for the changes e.g. DST.