Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display accurate local time on web site?

I've been asked to display the 'correct' time on our website which I frankly feel is rather pointless as 'correct' can be interpretted in such a variety of ways.

Our current method definately results in an inaccurate time as it uses a server control rendering JavaScript that runs onload using the datetime from the server as a parameter to create a clock object in JavaScript that finally renders on the page and then starts incrementing the clock. Between the server processing, network latency and client-side performance (there's plenty other stuff running onload) the clock ends up way off the actual server time and who knows compared to the client PC.

So to get the 'correct' time shown I could;

  • Use local PC time and pass new Date() to the JavaScript clock object. Pros: Should be as close to the PC clock as possible. Cons: Not sure how accurate the PC clock is let alone in which timezone.
  • Use web service for TCP request to NTP server to update clock on web page. Pros: If local PC also sync'd to NTP will be accurate and best possible match. Cons: Will have to handle all the timezone adjustments relative to our servers. If PC clock is out will still have mismatch.

Do I implement my own web service or use something like; Earth Tools or World Time Web Service (EDIT: link removed - now 404)

Here's a blog post from Jon Galloway on Atomic Clock Web Service which is pretty old and yet ranks high when I google and he doesn't reach a conclusion.

Hoepfully I can win the argument with management why syncing to our server clock (GMT) doesn't makes sense if your not in that timezone and why we even need to match a local PC.

Any angles on this I'm missing?

like image 783
Dave Anderson Avatar asked Oct 28 '08 17:10

Dave Anderson


People also ask

How do I display a clock on my website?

ClockLink.com provides fashionable clocks that you can easily embed in your web page. All you need to do is simply paste the tag on your web page. Our clock will display the city name of your choice if you choose. You can also choose a time zone for your clock so it will show the correct time.

How do you show time based on location?

Open your phone's Clock app . Tap Clock. At the bottom, tap Add . Type the name of a city in the search bar, then tap the city you want to add.

Is Timeanddate com free?

timeanddate.com is described as 'Free Clock for your website or blog!


2 Answers

I needed to show the accurate time to clients in an auction web app. You can send the current server time with the page and have the javascript initialize right away without waiting for the rest of the page to load. So the, you only are dealing with network latency which in the worst case is not likely to be more than a couple of seconds.

After that, you're pretty darn close to accurate time. As long as your Javascript timer code is written properly, you're not going to far out of sync before the next page load. But I have seen a lot of bad JS clock code. (Hint: Date() good, setTimeout() bad.)

If you have an application that users are going to be sitting on for a long time, just refresh your time sync either by reloading the page or Ajax.

I wouldn't worry about time zones, just use UTC time so there is no confusion about what time things will happen.

like image 79
Sal Avatar answered Nov 02 '22 20:11

Sal


First, be certain that your client is aware that Windows, Linux, and OSX all have built-in clocks that are almost always visible to the users (or made visible very easily). Also, be certain that your client is aware of physical clocks that are often located near any kiosks that might be setup to hide the built in clock from the operating system.

If you know that, and your client still wants a clock on your website, have your client define "correct" time, then implement the solution that matches their definition (both of your solutions seem like they would take care of the two most-likely definitions).

like image 27
Illandril Avatar answered Nov 02 '22 18:11

Illandril