Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to sync computer time with an internet time server?

I need to get the current time from one of internet time server in my desktop application. I suppose I need something like a request string and a regular expression to get time from any site that user wants (may be with several predefined sites).

Or may be there are some free libraries exist?

Thanks.

like image 240
SKINDER Avatar asked Aug 17 '10 14:08

SKINDER


2 Answers

This is what the Network Time Protocol was built for. But it's probably something best left to your operating system, lest you end up with duelling applications using different, not-quite-synchronised servers.

See the headings in the link above for UNIX and Windows implementations.

like image 178
paxdiablo Avatar answered Sep 29 '22 19:09

paxdiablo


There are free libraries and specifications for how to retrieve time, and the format in which you receive it (so REs are generally unnecessary). You choice depends on the level of precision/accuracy you want.

RFC 868 gives time to the second, which is entirely adequate for a lot of people's purposes. If it's good enough, it's a lot simpler to implement than the others listed below.
RFC 5905 defines the Network Time Protocol. As long as you only want to get the time, not provide it for anybody else, NTP is probably overkill though.
RFC 4330 defines SNTP (Simple NTP), which is a simplified version of NTP for computers that act as "leaf nodes" -- i.e., they retrieve time from elsewhere, but nothing else retrieves the time from them.

The NTP project has free NTP libraries for a number of systems.

like image 41
Jerry Coffin Avatar answered Sep 29 '22 18:09

Jerry Coffin