DIY · TechIt · Uncategorized · Windows

Automatically update the System Date and System Time in Windws XP (and beyond!)

I was recently trying to find out how to automatically set the date on a windows machine(windows XP, but solution here should be generic I think).

I tried using the system date and time setting “Automatically Update Time”, but that failed because the date was different/wrong also (contrary to this info from MS (http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/windows_date_it_overview.mspx?mfr=true) it appears it would not update the time when the date was wrong).

If your computer can remember the correct date, it should work fine. But if like me, your computer keeps resetting back to the earliest date possible (because the internal clock or it’s battery are broken) then that won’t work for you.

Which brings us to another point that doesn’t help my particular scenario, that Automatic Update of the system time seems to only sync once a week.

Becuase this machines internal clock doesn’t remember anything, I actually need a way to set / reset the system time automatically, and every time the computer starts.

The reason for all of this is that :

  1. The clock battery in the PC (a very cheap third hand pentium p4 ) is gone.
  2. The attachment for it is broken so there is no option to replace. (short of doing some DIY electrical/electronic work perhaps)
  3. The power to the PC is cut regularly at night so the machine loses any previous date/time setting.
  4. The correct date / time is required for connecting to secure web sites ( as the certs used for secure sites are date/time limited ) so it needs to be correct for happy internet browsing.
  5. manually setting it is extra hassle for the non-techie user, and onerous to do every time the machine is started

Finding no other system workarounds, it was time obviously to throw a script together!

I soon found an Ntp package library in Perl that would take care of all the connecting to Ntp servers etc, but had a problem with the activestate Perl installation.

The ruby language has such a package also (http://rubygems.org/gems/net-ntp), and I had no issues with the installer from http://rubyinstaller.org/ as suggested from http://www.ruby-lang.org/en/downloads/

Firing up a text editor I saved the following as c:\scripts\ruby-test.rb and ran it with ruby c:\scripts\ruby-test.rb from a cmd prompt to sanity-check the ruby installation.


# Here's the code:
puts "Hello there!"

Then, to install the ntp library to do the heavy lifting for my time-setter script, type this into a cmp prompt

gem install net-ntp

In another text editor save the following as c:\scripts\update-system-time.rb.

It simply calls to the internet for the date and time (using a public time server) and then uses windows command line commands to set the computer clock the the correct date and time for today.

require 'net/ntp'

Net::NTP.get('1.us.pool.ntp.org')
t = Net::NTP.get.time

# set the date via the windows date command
# per Roberts comment,
# swap the day and month in the below if required
# Thanks Robert!
system("date #{t.day}-#{t.month}-#{t.year}")

# set the time via the windows time command
system("time #{t.hour}:#{t.min}:#{t.sec}")

Setting the wrong date/time and then running this script with ruby c:\scripts\update-system-time.rb proved it works

(Oh, it doesn’t work ? Does the account your using have administrator rights ?)

To run it at boot up, I did’t bother with registry settings or anything like that. I just created a batch file to run the script and saved it as c:\scripts\set-time.bat

rem set the time as the motherboard has no clock battery
ruby c:\scripts\update-system-time.rb

Then create a scheduled task to run on startup.

In the control panel click “Schedule Tasks“. Click “Add scheduled task“. In the pop-up “Scheduled Task Wizard” click browse and locate and select the batch file c:\scripts\set-time.bat
Give it a name, and for the “Perform this task:” option select “When my computer starts“.

Next you will have to give it a user name and password so it can run. You may need to run this as a user with admin rights in order change the date/time. (I did use an account with administrator rights for the task and testing).

That was it, the computer is unplugged regularily at which point the bios and clock are reset, the clock to sometime in 2001.

But when the machine eventually boots up, this little script is run and the clock is updated from the internet. 🙂

Happy browsing!

Advertisements

2 thoughts on “Automatically update the System Date and System Time in Windws XP (and beyond!)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s