I know that Raspberry Pi does not come with a system clock to save money. How can I keep system time though?
The most popular method of keeping system time is via the Network Time Protocol daemon (NTPd).
NTPd is the most common method to synchronize the software clock of a GNU/Linux system with internet time servers using the Network Time Protocol.
NTPd should already be installed and set up on the default operating system, Debian Squeeze. You will need to install and configure it manually though for Arch Linux.
First you should install NTP.
$ sudo pacman -S ntp
Now you can manually update it via the command line.
$ sudo ntpd -qg
Or you can add it to the list of daemons on start up by adding it to your
/etc/rc.conf file. Like so.
# /etc/rc.conf DAEMONS=(!hwclock ntpd)
Note that you need to disable
hwclock by preceding it with an exclamation mark (
!) in the daemons list.
Protip from mlp: If you add
ntpdate to your list of daemons the time will be corrected after boot much faster. So your daemons list would look like so.
# /etc/rc.conf DAEMONS=(!hwclock ntpd ntpdate)
If you want to guarantee that the time on the RPi is always correct or want to maintain the time without a network connection, you will need to buy an expansion board with a Real Time Clock (RTC) on it: there is at least one available and at least one in development.
Available RTC Expansion Boards
RTC Expansion Boards in Development
For non-networked applications (or where network time might not be good enough), you can set your RTC clock (@Alex Chamberlain's answer) with time derived from a GPS receiver or a VLF receiver (in NW Europe this would be Rugby MSF). Both could be excellent solutions to field-deployed devices (robots, field monitoring, etc).
Both solutions would also make good hobby projects. I remember building a Rugby MSF receiver in the 1980s. An off the shelf GPS receiver ("puck") would give the time and date in the NMEA 0183 sentences - it would then be a simple coding project to extract this information.
Install the NTP daemon on Arch Linux with systemd:
sudo pacman -S ntp sudo systemctl enable ntpd sudo systemctl start ntpd
Also make sure your timezone is set correctly, in my case:
sudo rm /etc/localtime sudo ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
If you don't want to have NTP running on your system the whole time you can use one of a few different options to set the time:
- ntpdate - Once installed it sets the system up to obtain the time when any interface comes up (although 'deprecated' it's still fairly widely used).
- rdate - Simpler and lighter weight approach, though a bit less accurate.
- tlsdate - A more secure option which uses an TLS/SSL server as a time source.
Obviously your clock wont be as accurate as using NTP since all clocks do drift, and also since these apps only use a single time source they can potentially result in setting the incorrect time if their chosen source has a problem.