The source code of all examples can be downloaded from here.
NTP Time Synchronization
When the Linux operating system of the Raspberry Pi is booting, it sets the current date and time to what was saved at the last run. But what you need is the current date/time and the Linux tries to get this information from one of the Internet time servers using the Network Time Protocol (NTP). There are hundreds of NTP servers around the world providing time services to hundred of millions of systems. At boot time a service is started and the Raspberry Pi tries to connect to one of the NTP servers configured in the file /etc/ntp.conf file. If successful, the system clock is automatically set to the current Universal Time (UTC). Provided that your time zone is correctly selected (see below), the date command returns the local time. Of course, if the Raspberry Pi is not connected to the Internet at boot time, the time is erroneous.
In order to set up NTP time synchronization open a terminal and type
sudo nano /etc/ntp.conf
Then replace the lines like
with the time servers for your region that you find here. For instace in Germany and Switzerland:
Comment out (by adding a leading # in the lines):
Save the file and set the correct time zone by running
sudo cp /usr/share/zoneinfo/<zone> /etc/localtime
Replace <zone> with your time zone. Check about the possible values, by looking in /usr/share/zoneinfo. For instance for Switzerland type:
sudo cp /usr/share/zoneinfo/Europe/Zurich /etc/localtime
Restart the time synchronization service (ntp) by typing:
sudo /etc/init.d/ntp restart
to check if the time is correct. If this is not the case, force the synchronization by typing:
sudo ntpd -gq
Real-time Clock Based On The DS1307 Chip
In many real-time applications the Raspberry Pi cannot access the Internet at startup. Like in any PC a battery powered on board clock should be used to deliver the current time. A small Realtime Clock Module (RTC) based on the DS1307 chip using the I2C protocol can do the job. RTC modules are available from different sources found on the Internet. Some of them are breakout boards that are inserted directly into the GPIO header. But you may also build your RTC from the Maxim DS1307 chip on a breadboard.
If you inspect the datasheet of DS1307 you realize that the data exchange is extremely simple. 8 byte registers at 0x00 to 0x06 holds the seconds, minutes, hours, day_of_week, day, month, year of the current date/time in Binary-Coded-Decimal (BCD) format (two decimals coded as binary 0000 to 1001, one in the lower nibble, one in the higher nibble, e.g. decimal 41 is binary 01000001). The clock is set to a specific date/time by writing into the registers. As long as the chip is powered by the battery, the current date/time can be requested by reading the registers. Register at address 0x07 is a control register. It is not used in normal operation.
After installation, you should setup the RTC to the current date/time. The following program will do it.
Highlight program code
Whenever you need the current date/time you can use getDate() or getDateStr().
Highlight program code
This program will not synchronize the Linux based clock; its left to you to add some additional lines of code for this task.