views:

122

answers:

5

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.

+7  A: 

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.

paxdiablo
+1  A: 

This feels like something the OS should do on its own...

There is a protocol on the internet called NTP that returns time from timeservers.

You might want to try looking for a library/class that can use NTP to retrieve time for you.

Or you could try looking at the source code for ntpclient.

sheepsimulator
A: 

Usually, most operating systems embeds such a functionality.

Both Windows and Linux can sync with NTP servers.

If really want to let the user change its timezone, you should look for OS specific API's instead of communicating directly with the time servers then changing the system time. This would be way nicer.

ereOn
A: 

Maybe you can take a look at this code and get some inspiration:

http://web.abnormal.com/~thogard/ntp/ntpdate.c

Alexander Kjäll
+1  A: 
Jerry Coffin