Date-Time

The date-time library maintains the current date-time information in UTC format. The option CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS determines the frequency with which the library updates the date-time information.

The information 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 CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS, the library does not fetch new date-time information. In this way, unnecessary update cycles are avoided.

  2. If the aforementioned check fails, the library requests time from the onboard modem of nRF9160.

  3. If the time information obtained from the onboard modem of nRF9160 is not valid, the library requests time from NTP servers.

  4. If the NTP time request does not succeed, the library tries to request time information from several other NTP servers, before it fails.

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 GPS can be such an external source.

To get date-time information from the library, either call 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

The first date-time update cycle (after boot) does not occur until the time set by the CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS has elapsed. It is recommended to call the date_time_update() function after the device has connected to LTE, to get the initial date-time information.

Configuration

CONFIG_DATE_TIME_UPDATE_INTERVAL_SECONDS

Configure this option to control the frequency with which the library fetches the time information.

API documentation

Header file: include/date_time.h
Source files: lib/date_time/src/
group date_time

Library that maintains the current date time UTC which can be fetched in order to timestamp data.

Functions

void date_time_set(const struct tm *new_date_time)

Set the current date time.

Parameters
  • [in] new_date_time: Pointer to a tm structure.

int date_time_uptime_to_unix_time_ms(int64_t *uptime)

Get the date time UTC when the passing variable uptime was set. This function requires that k_uptime_get() has been called on the passing variable uptime prior to the function call.

Return

0 If the operation was successful. Otherwise, a (negative) error code is returned.

Parameters
  • [inout] uptime: Pointer to a previously set uptime.

int date_time_now(int64_t *unix_time_ms)

Get the current date time UTC.

Return

0 If the operation was successful. Otherwise, a (negative) error code is returned.

Parameters
  • [out] unix_time_ms: Pointer to a variable to store the current date time UTC.

void date_time_update(void)

Asynchronous update of internal date time UTC. This function initiates a date time update regardless of the internal update interval.