[maemo-users] digital clock to 0.1s accuracy

From: Andrew Daviel advax at triumf.ca
Date: Sat Mar 14 19:35:33 EET 2009
Last week the US and Canada changed to Daylight Savings Time.
Usually, I set my wristwatch from my computer (which uses NTP)
then wander around the house resetting all the clocks, correcting any 
drift accumulated over the last 6 months.

This year I thought it would make more sense to use my tablet, which uses 
NTP directly. Normally I type "date" in an xterm. Then I was thinking 
"what if it says 09:04:03 when it's really 09:04:03.99 ?". (which the GUI 
clock apps presumably handle).
Then  "Can I display better than 1 second accuracy ?"

So, after rejecting a version that just sat in a tight loop, I have one 
that microsleeps until the next 1/10th second.


use Time::HiRes qw( gettimeofday usleep )  ;
$| = 1 ;
system("ntptrace -n -m 1"); # check accuracy before starting
while (1) {
   $t = gettimeofday() ;
   $t2 = $t * 10 ;
   $it = 1 + int($t2) - $t2 ;  # time left till next tick
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($t);
   $dsec = $sec + $t-int($t) ;
   printf("%2.2d:%2.2d:%04.1f\r",$hour,$min,$dsec) ;
   $dt = usleep ($it*100000) ;

I can't remember where ntpdate came from. One of the repositories, I 
think. ntptrace is written in Perl and was missing Getopt/Std.pm 
out-of-the-box. I found it in scratchbox and copied it across.
Also Time/HiRes.pm, Time/HiRes/HiRes.so.
/etc/ntp.conf seemed to be missing from ntpdate, and I think needs to be
set for ntpd to lock (unless it can find a multicast stream, maybe).
I have
driftfile /var/lib/ntp/drift
server 0.ca.pool.ntp.org dynamic
server 1.ca.pool.ntp.org dynamic

Maybe this has all been done before in a nice GUI. I didn't look; 
simple things I find it easier to just script.

It also works as a kind of split timer - hitting "return" when it's 
running leaves the time on the previous line.

Andrew Daviel, TRIUMF, Canada

More information about the maemo-users mailing list