Have I ever mentioned how much I like my n900? If not, let me tell you that I love this device... mostly. GNU Linux Operating System, the ability to write apps in the programming language of my choice; what is there to not like about this four year old marvel? Setting the time.
On the N900, setting the time can be done manually, or automatically when the device is used as a mobile phone with a SIM card. Since I don't use the device as a mobile phone (to me it is the ultimate pocket computer), I need to update the time manually, and the time seems to drift by a few minutes after a couple of months of uptime.
Recently, the USB port on my N900 broke, and I've been using a battery charger to keep a collection of batteries charged for the device. When a battery gets drained, a simple swap of the battery has me up and running in no time. Unfortunately this means that I need to set to manually set the date every time I swap a battery. Oooof, I'm too lazy for that crap, time for a bit of code!
The plan was fairly simple, write a script to:
- fetch time data from somewhere
- use the date command to set the date
Unfortunately, I couldn't find a really easy way to get the current time from somewhere on the internet. How is that possible?
Fortunately, a bit of code written in PHP and hosted on one of my websites will display the current UNIX Time
The PHP code looks like this :
//[MMDDhhmm[[CC]YY][.ss]]
echo date('mdHiY');
?>
Alright, the date data is available online, now on to the script to read the data.
Since this script is for an N900, I have a choice of programming languages, and for this script I chose Ruby. After a quick look at the language documentation, the following was hammered out on the N900.
require 'net/http'
#create a URL for our url //haha
uri = URI('http://www.jezra.net/date.php')
#get the response from the server
res = Net::HTTP.get_response(uri)
#get the date from the response, and remove non-numeric characters
date_string = res.body.gsub(/[^\d]/,'')
#set the date command
cmd = "date -u #{date_string}"
#run the command
`#{cmd}`
Nice and simple.... ;)
Now quit reading, and go solve a problem.