Date-Time

The date-time library retrieves the date-time information and stores it as Zephyr time and modem time.

Overview

Date-time information update can be triggered to this library by the following means:

When this library retrieves the date-time information, it is fetched in the following prioritized order:

  1. The library checks if the current date-time information is valid and relatively new. If the date-time information currently set in the library was obtained sometime during the interval set by the CONFIG_DATE_TIME_TOO_OLD_SECONDS option, the library does not fetch new date-time information. In this way, unnecessary update cycles are avoided.

  2. If the check fails and the CONFIG_DATE_TIME_MODEM option is set, the library requests time from the nRF91 Series modem.

  3. If the time information obtained from the nRF91 Series modem is not valid and the CONFIG_DATE_TIME_NTP option is set, the library requests time from an NTP server.

  4. If the NTP time request does not succeed, the library tries to request time information from a different NTP server, before it fails.

If all time requests fail and the CONFIG_DATE_TIME_RETRY_COUNT Kconfig option is greater than zero, the entire process listed above will be retried. The interval before each retry is defined by the CONFIG_DATE_TIME_RETRY_INTERVAL_SECONDS Kconfig option, and the CONFIG_DATE_TIME_RETRY_COUNT Kconfig option sets how many consecutive retries are attempted before giving up. If the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS Kconfig option is greater than zero, periodic updates at the defined interval resume after all retries have been attempted.

The current date-time information is stored as Zephyr time when it has been retrieved and hence, applications can also get the time using the POSIX function clock_gettime. It is also stored as modem time if the modem does not have valid time.

Implementation

The date_time_set() function can be used to obtain the current date-time information from external sources independent of the internal date-time update routine. Time from GNSS can be such an external source.

The option CONFIG_DATE_TIME_AUTO_UPDATE determines whether date-time update is triggered automatically when the LTE network becomes available. Libraries that require date-time information can just enable this library to get updated time information. Applications do not need to do anything to trigger time update when they start because this library handles it automatically.

Retrieving date-time information using the POSIX function clock_gettime is encouraged when feasible. You can obtain the information also from the library by calling either the date_time_uptime_to_unix_time_ms() function or the date_time_now() function. See the API documentation for more information on these functions.

Note

It is recommended to set the CONFIG_DATE_TIME_AUTO_UPDATE option to trigger a time update when the device has connected to LTE. If an application has time-dependent operations immediately after connecting to the LTE network, it should wait for a confirmation indicating that time has been updated. If the CONFIG_DATE_TIME_AUTO_UPDATE option is not set, the first date-time update cycle (after boot) does not occur until the time set by the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS option has elapsed.

Note

Exceptions to the regular date-time update interval set by the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS Kconfig option occur when the date_time_update_async() function is called and a new date-time update is triggered and scheduled. Either retry or regular update interval is used depending on the outcome of the date-time update procedure. Date-time update from modem through an AT%XTIME notification, or from the client through the date_time_set() function does not disturb the regular update interval.

Configuration

Configure the following Kconfig options to enable this library and its main functionalities:

Configure the following options to fine-tune the behavior of the library:

Samples using the library

The following nRF Connect SDK samples and applications use this library:

Limitations

The date-time library can only have one application registered at a time. If there is already an application handler registered, another registration will override the existing handler. Also, using the date_time_update_async() function will override the existing handler.

Dependencies

API documentation

Header file: include/date_time.h
Source files: lib/date_time/src/
Date Time Library