22

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
  • 1,984
  • 2
  • 14
  • 17
Guy
  • 1,607
  • 1
  • 15
  • 18
  • possible duplicate of [How accurate is Raspberry Pi's timekeeping?](http://raspberrypi.stackexchange.com/questions/1397/how-accurate-is-raspberry-pis-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
  • because its related to time keeping - which requires the internal clock- the answers there cover everything you should know. – Piotr Kula Aug 14 '12 at 20:53
  • You think that that question contains everything I need to know about how time is kept on the RPi? Weird. – Guy Aug 14 '12 at 21:01
  • 1
    Maybe the title of this question should be more specific to avoid overlap between these questions. – XTL Aug 15 '12 at 07:53
  • The key to keeping good time is to get it from an authoritative source. Here that is time.nist.gov, as explained here: https://raspberrypi.stackexchange.com/questions/68811/how-do-i-set-raspbian-to-use-the-primary-time-server-time-nist-gov – SDsolar Jun 23 '17 at 00:50

4 Answers4

25

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
  • 3,918
  • 4
  • 27
  • 30
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,208
  • 11
  • 88
  • 138
  • 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 06:03
  • 1
    @AlexChamberlain Seems a strange discrepancy. – Jivings Aug 15 '12 at 06:40
  • Not at all. Most distributions are not specific to RPi or even similar systems. – XTL Aug 15 '12 at 07:54
  • 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 08:23