Sign in to follow this  
Triad_prague

getting time from a time server

Recommended Posts

Hi, my CMOS battery socket is damaged and so, my PC won't show the correct time every time I start it up (it will always show 01/01/2002 00:00 AM which I think is the time and date the motherboard created). I found it quite annoying to have to set the time to correct value every time I boot my PC, and I came up with an idea to make a small app to get time from a time server and set the system time accordingly. thing is, I forgot how to do that. I remember ever creating such a program when I was learning networking (Winsock) for the first time. IIRC, it used UDP socket, I forgot the port number, and I forgot the time server address. So in my humble confusion I beg you, if anyone can, to give hints on how it can be done.

to sum it up, what I need is a guide to:
-how to get time from the time server, and the requirements (what message to send, etc)

Thanks :D

Share this post


Link to post
Share on other sites
[quote name='Triad_prague' timestamp='1318488054' post='4872107']
... thing is, I forgot how to do that. I remember ever creating such a program when I was learning networking (Winsock) for the first time. IIRC, it used UDP socket, I forgot the port number, and I forgot the time server address. So in my humble confusion I beg you, if anyone can, to give hints...
[/quote]

Maybe you're referring to NTP? [url="http://www.nist.gov/pml/div688/grp40/its.cfm"]http://www.nist.gov/pml/div688/grp40/its.cfm[/url]

If so, googling a few terms from there should lead you to numerous examples and information - more than we can suggest here :)

Share this post


Link to post
Share on other sites
I solved it. I use the simplistic TIME protocol by:
-connect()-ing to time server on port 37
-recv() 4 bytes and copy it to 32 bit unsigned integer and translate the byte order using ntohl()
-since the returned value is seconds since 1 jan 1900, convert it to suits my need and I'm done

I hope it can be helpful for those who seek similar information, thanks and see ya :wink:

Share this post


Link to post
Share on other sites
@ApochPiQ, the built-in time synchronization in windows only works if the date/time isn't too much different. It doesn't work if my PC date is 9 years left behind (which will always be since the CMOS battery socket's blasted)...that's why I needed to create my own tool for that :)

Share this post


Link to post
Share on other sites
[quote name='Triad_prague' timestamp='1318570835' post='4872419']
@ApochPiQ, the built-in time synchronization in windows only works if the date/time isn't too much different. It doesn't work if my PC date is 9 years left behind (which will always be since the CMOS battery socket's blasted)...that's why I needed to create my own tool for that :)
[/quote]

Cygwin comes with ntpdate. You can add this to a startup script:

ntpdate -q pool.ntp.org

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this