16

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.

  • possible duplicate of How accurate is Raspberry Pi's timekeeping? – Piotr Kula Aug 14 '12 at 17:34
  • 4
    If time.now < than year 2010 - then time is not set. You do not need to know any thing else. Time is not saved on power downs and will be 1970 as per UNIX/POSIX specification. To find out if time is inaccurate you need to query NTP and compare local time. You cannot determine if time is out of synch without asking a time server. – Piotr Kula Aug 14 '12 at 17:58
  • 4
    if time.now < 2010 will not work as the RPi sets the current time from it's last shutdown time. This is why I want to know how the time is set so that I can disable this behaviour if necessary. – Guy Aug 14 '12 at 20:13
  • pumkin - how is this question related to the drift of the internal clock? – Guy Aug 14 '12 at 20:14
  • 1
    Maybe the title of this question should be more specific to avoid overlap between these questions. – XTL Aug 15 '12 at 7:53
20

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.

  • Good guess Jivings! :-) – Piotr Kula Aug 15 '12 at 7:55
  • @asalamon74: can you add how to disable fake-hwclock or would it need to be uninstalled? – Guy Aug 15 '12 at 11:17
2

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.

1

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

-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.

  • 2
    Incorrect. The RPi sets the current time from it's last shutdown time when there is no network connection. So the clock is always some random time earlier than the correct time, not neccesarily Unix epoch. – Guy Aug 14 '12 at 22:22
  • 2
    That's certainly not true on my Pi. If I boot it without a network connection then the time is the epoch. I can see this right now. – Jivings Aug 14 '12 at 22:28
  • 2
    It probably depends on the distribution. – Alex Chamberlain Aug 15 '12 at 6:03
  • 1
    @AlexChamberlain Seems a strange discrepancy. – Jivings Aug 15 '12 at 6:40
  • 1
    @Jivings If there is a piece of software on Raspbian called fake-hwclock, it probably won't be on Arch. Sounds like the kind of fluff Arch ignores. – Alex Chamberlain Aug 15 '12 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.