Date time on label refresh problem

Hello,

I have function that updates time on label:

void updateDate()
{
	char text[100];

	time_t now = time(NULL);
	struct tm *t = localtime(&now);


	strftime(text, sizeof(text) - 1, "%d %m %Y %H:%M", t);
	lv_label_set_text(time_label, "text");

}

I call it from function custom_tick_get defined below. In this case application crashes. Why and what is the best way to update time label ?

uint32_t custom_tick_get(void)
{
    static uint64_t start_ms = 0;
    if (start_ms == 0) {
        struct timeval tv_start;
        gettimeofday(&tv_start, NULL);
        start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
    }

    struct timeval tv_now;
    gettimeofday(&tv_now, NULL);
    uint64_t now_ms;
    now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;

    uint32_t time_ms = now_ms - start_ms;
	updateDate();
	return time_ms;
	
}
1 Like

You could use a timer (v8) or task (v7) and poll for the date once per second.

Putting extra logic in your custom_tick_get function is a bad idea. LVGL calls it from lots of places and you could end up with recursion.