2013-10-30

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:

  1. fetch time data from somewhere
  2. 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 :

<?php
//[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.

#!/usr/bin/env ruby
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.

Comments
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
6 minus 4
Answer:
required
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008