In most sensor applications, it is important to accurately keep track of time. This may be necessary for the purpose of logging data, for detecting patterns in sensor data, or for reacting to sensor data at specific times.
The simplest solution is to make the centralized data storage server responsible for timekeeping. Servers in the cloud normally have a very accurate measure of time due to synchronization with Network Time Protocol (NTP) servers. In such a scenario, IoT sensor nodes would periodically wake up, take a measurement, and push that data to a central server. The server can then put that data along with a timestamp into a database. This solution is simple and cheap, as no additional hardware needs to be included in the sensor nodes. However, this is only possible in specific scenarios where cloud infrastructure is available. Data.sparkfun.com is a simple data logging platform that can be used to experiment with this paradigm. Phant, the software behind data.sparkfun.com, can also be hosted on third-party servers.
Sometimes, it is necessary to have accurate timekeeping on the device itself. A realtime clock (RTC) can be used for this. RTCs typically have an alternative power source, so that they can continue to track time while the system is turned off. The power source is usually either a small coin-cell battery or a supercapacitor. RTCs exist as discrete chips (e.g. DS1307) or alternatively, as extra functionality integrated into a microcontroller chip (e.g. STM32Fxx [PDF]). RTCs can also be used to wake up the main processor at specific times, allowing the main processor to spend most of the time in a low-power sleep mode. While RTCs can keep accurate time over considerable timespans (years), it is important to consider that the time still needs to be set at a certain point. One option is to allow the time to be set manually through a user interface. Another option is to periodically synchronize the RTC with a different source of timekeeping.
There are multiple options for authoritative time sources. To begin, the network can be used to contact a timekeeping server. For instance, WiFi can be used to connect to a Network Time Protocol (NTP) server. Another option is to use GPS. Due to the way GPS works, the system returns not only absolute position, but also time. Finally, clocks can be synchronized to low-frequency radio time signals. These terrestrial signals are transmitted by time-keeping stations around the world. A list of radio time sources can be found here. Note that not all regions are covered by a LF radio time signal tower.