Monday, March 12, 2012

Setting the PC Time under Unix

Howdy.


Today, we are going to see how to setup your computer's time. There are cases where your PC's CMOS Battery is dead or not saving the data properly. So, you would either have to configure the time by going to your BIOS setup or set that inside your OS. Either ways, it's difficult to have it setup everytime  you login to your PC because it's just too much work to do or you can make a silly mistake. Or you can make your PC connect to the Webservice to provide the correct time (as per your local timezone). What if this can be automated? Well, this is what this article is about.


We'll write a simple unix shell script that would connect to some webservice address that will provide the current time given the position's co-ordinates. The following is a sample.

#!/bin/sh
wget http://www.earthtools.org/timezone/xx.xxx/yy.yyyy -O time.xml;
timetag=`cat time.xml|grep -i localtime`;
rm time.xml;
timetag=`echo $timetag|sed 's/<localtime>//'`;
currenttime=`echo $timetag|sed 's/<\/localtime>//'`;
echo Setting date to $currenttime;
sudo date -s "$currenttime";


Illustration:


a) I've taken earthools' simple Webservice which takes the position's geographics co-ordinates (lat/long) and provides a response XML which contains the Time we're looking for. The URL format is http://www.earthtools.org/timezone/lat/long. wget downloads the response of this URL and saves it as time.xml for later processing. You've to edit this command to use your location.
 This is how a sample response XML looks like:


<?xml version="1.0" encoding="ISO-8859-1" ?>
<timezone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.earthtools.org/timezone-1.1.xsd">
  <version>1.1</version>
  <location>
    <latitude>xx.xxx</latitude>
    <longitude>yy.yyy</longitude>
  </location>
  <offset>z.z</offset>
  <suffix>A*</suffix>
  <localtime>12 Mar 2012 01:16:51</localtime>
  <isotime>2012-03-12 01:16:51 +0530</isotime>
  <utctime>2012-03-11 19:46:51</utctime>
  <dst>Unknown</dst>
</timezone>


b) Then, we extract the locatime tag by 'grep'ing it.

c) Once we have got the localtime, we remove the XML wget downloaded.

d) Then the next two sed commands remove the Start and End XML tags.

e) Now we got the actual time we're looking for.

f) Then we feed the value to the date command, which actually sets the time on the PC.

That's it. Done!

Notes:

a) The earthtools webservice is just an example. There are a whole lot of other services too. Please contact the author or owner of those sites before you use their services heavily. You should not be violating their Terms and Conditions.

b) The XML parsing shown here (using sed) is very primitive. Make it efficient as necessary.

c) If you can find a similar way of doing this on Windows platforms, please feel free to add them or paste the links to pages where this is already done.

Once you've the script ready, try running it manually every time you login or even better, automate it  by adding this script to your startup list!

Thanks for reading this. :-) See you in the next post!