Date_Time gives the wrong unix time

Hi! I am trying to sync the time on my nrf9160dk with date_time_now() or date_time_uptime_to_unix_time_ms() but it seems that they return a very low unix_time_ms (only 1089262423ms). This is my code:

static K_SEM_DEFINE(write_done_sem, 0, 1);  // Starts at 0; max count is 1
static K_SEM_DEFINE(read_done_sem, 0, 1);
static K_SEM_DEFINE(lte_connected, 0, 1);
static K_SEM_DEFINE(date_time_obtained, 0, 1);



static void date_time_evt_handler(const struct date_time_evt *evt)
{
	switch (evt->type) {
	case DATE_TIME_OBTAINED_NTP:
		printk("\r\nDate and time obtained from NTP\n");
		k_sem_give(&date_time_obtained);
		break;
	
	case DATE_TIME_OBTAINED_MODEM:
		printk("\r\nDate and time obtained from modem\n");
		k_sem_give(&date_time_obtained);
		break;

	case DATE_TIME_OBTAINED_EXT:
		printk("\r\nDate and time obtained from external source\n");
		k_sem_give(&date_time_obtained);
		break;

	
	case DATE_TIME_NOT_OBTAINED:
		printk("\r\nDate and time not obtained\n");
		k_sem_give(&date_time_obtained);
		break;
	default:
		break;
	}
}

static void lte_handler(const struct lte_lc_evt *const evt)
{
	switch (evt->type) {
	
	case LTE_LC_EVT_NW_REG_STATUS:
		if ((evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_HOME) &&
			(evt->nw_reg_status != LTE_LC_NW_REG_REGISTERED_ROAMING)) {
			break;
		}
		printk("\r\nNetwork registration status: %s",
				evt->nw_reg_status == LTE_LC_NW_REG_REGISTERED_HOME ?
				"Connected - home network" : "Connected - roaming");
		k_sem_give(&lte_connected);
		break;
	
	case LTE_LC_EVT_RRC_UPDATE:
		printk("\r\nRRC mode: %s", evt->rrc_mode == LTE_LC_RRC_MODE_CONNECTED ?
				"Connected" : "Idle");
		break;

	
	default:
		break;
	}
}

static int modem_configure(void)
{
	int err;

	err = nrf_modem_lib_init();
	if (err) {
		printk("Failed to initialize the modem library, error: %d", err);
		return err;
	}

	/* lte_lc_init deprecated in >= v2.6.0 */
	#if NCS_VERSION_NUMBER < 0x20600
	err = lte_lc_init();
	if (err) {
		printk("Failed to initialize LTE link control library, error: %d", err);
		return err;
	}
	#endif

	date_time_register_handler(date_time_evt_handler);


	
	printk("\r\nConnecting to LTE network");
	err = lte_lc_connect_async(lte_handler);
	if (err) {
		printk("Error in lte_lc_connect_async, error: %d", err);
		return err;
	}
	k_sem_take(&lte_connected, K_FOREVER);
	printk("\r\nWaiting for date and time\n");
	k_sem_take(&date_time_obtained, K_SECONDS(30));
	printk("\r\nDate and time obtained\n");
	if (!date_time_is_valid()) {
		printk("\r\nDate and time is not valid\n");
		return -1;
	}
	else{
		printk("\r\nDate and time is valid\n");
	}

	return 0;
}



void main(void){
ret = modem_configure();
	if (ret) {
		printk("Failed to configure the modem");
		return 0;
	}
	current_time = 0;
	date_time_now(&current_time);
	printk("\r\nCurrent time: %lld\n", current_time);
}

and my prj.conf:

CONFIG_LTE_AUTO_INIT_AND_CONNECT=n 
CONFIG_LTE_LINK_CONTROL=y 
CONFIG_NRF_MODEM_LIB=y
CONFIG_DATE_TIME_NTP=y   #
CONFIG_DATE_TIME_MODEM=n  #I've tried all combination of these two and it gives me the same result
CONFIG_DATE_TIME=y
CONFIG_DATE_TIME_NTP_QUERY_TIME_SECONDS=25
CONFIG_AT_HOST_LIBRARY=y

# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_LOG=y



I am a bit lost since it feels like i have done everything correctly since it connects and goes into RRC mode before instantly updating the time but the final time is just wrong.

Best,
Ray

Parents
  • Also this is a typical console printout:

    Connecting to LTE network+CEREG: 2,"859E","0510CB16",7
    +CSCON: 1
    
    RRC mode: Connected+CEREG: 5,"859E","0510CB16",7,,,"11100000","11100000"
    
    Network registration status: Connected - roaming
    Waiting for date and time
    %XTIME: "40","52107031041140","00"
    
    Date and time obtained from modem
    
    Date and time obtained
    
    Date and time is valid
    
    Current time: 1090423425

Reply
  • Also this is a typical console printout:

    Connecting to LTE network+CEREG: 2,"859E","0510CB16",7
    +CSCON: 1
    
    RRC mode: Connected+CEREG: 5,"859E","0510CB16",7,,,"11100000","11100000"
    
    Network registration status: Connected - roaming
    Waiting for date and time
    %XTIME: "40","52107031041140","00"
    
    Date and time obtained from modem
    
    Date and time obtained
    
    Date and time is valid
    
    Current time: 1090423425

Children
No Data
Related