25

How is time kept on a Raspberry Pi with the latest Raspbian release? What sets the internal clock from an NTP server? What happens when no NTP server is available?

From my program I am trying to determine if the time has been set from NTP and ask the user to set it if it has not.

Peter Mortensen
  • 2,004
  • 2
  • 15
  • 18
Guy
  • 1,667
  • 1
  • 16
  • 18

4 Answers4

27

Raspbian has two software solutions for timekeeping. Since NTP requires network connection and it's quite useless if your Raspberry Pi is not connected to the network, it also uses fake-hwclock. It saves the current clock periodically and loads it at startup.

pi@raspberrypi ~ $ cat /etc/fake-hwclock.data 
2012-08-15 03:17:01

This is not too accurate, but will eliminate the problem of time traveling back to 1970 after each reboot.

asalamon74
  • 4,108
  • 4
  • 31
  • 31
3

You can log NTP statistics and then parse logs from your program.

NTP daemon settings are stored in file /etc/ntp.conf. Uncomment line with directive statsdir to enable NTP statistics logging. Make sure that referenced directory exists and is writable for user ntpd.

2

To check if time has been set by a NTP server you could try using ntpq -p you will get something like this*:

$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*223.212.138.2    .MRS.           1 u  424  512  377    4.080   -4.551  21.857
+ts0.tttsc.nvm.e  .GPS.           1 u  387  512  363    1.304   -7.563  28.405
+218.89.10.3      217.13.17.82    2 u  334  512  377    1.853   -4.562  19.474

The 'reach' will be >0 in some row if the NTP server has been reached and thus set the time. It is 8 bits octal sliding along i.e. 0,1,3,7,17,37,77,177,377

Or this - you could grep 'stratum' < 16 or sync_ntp (this is not on RPi obviously)

$ ntpq -c rl
associd=0 status=0615 leap_none, sync_ntp, 1 event, clock_sync,
version="ntpd 4.2.6p5@1.2349-o Mon Jul 18 09:22:49 UTC 2013 (1)",
processor="x86_64", system="Linux/2.6.32-431.29.2.el6.x86_64", leap=00,
stratum=4, precision=-21, rootdelay=40.242, rootdisp=315.102,
refid=192.168.1.123,
reftime=d82131cd.fbb96c5e  Thu, Nov 27 2014 13:14:53.983,
clock=d82138e6.fd03bdd1  Thu, Nov 27 2014 13:45:10.988, peer=61770, tc=9,
mintc=3, offset=5.214, frequency=52.475, sys_jitter=12.217,
clk_jitter=23.319, clk_wander=1.373
$

Alternatively (I think ntpstat not available immediately on RPi)

$ ntpstat
synchronised to NTP server (192.168.1.123) at stratum 4
   time correct to within 310 ms
   polling server every 512 s

and use exit codes too explained here http://www.cyberciti.biz/faq/linux-unix-bsd-is-ntp-client-working/ admittedly not RPi specific...

*Time server addresses are not real

d586
  • 216
  • 1
  • 5
-1

The timekeeping is all in software. Without using NTP there is no timekeeping.

If you want to check if the clock has been set then just use date to see if it shows the Unix epoch, which is what the Pi will default to.

Jivings
  • 22,656
  • 11
  • 94
  • 140