1

I noticed a weird behavior of my application on an ESP32. After some "debugging" I think the issue is due to this function:

bool getLocalTime(struct tm * info, uint32_t ms)
{
    uint32_t start = millis();
    time_t now;
    while((millis()-start) <= ms) {
        time(&now);
        localtime_r(&now, info);
        if(info->tm_year > (2016 - 1900)){
            return true;
        }
        delay(10);
    }
    return false;
}

By the way, ms defaults to 5000. In my code I use getLocalTime in a finite-state-machine in order to execute actions at specific time, i.e.:

void Irrigation::fsm()
{
    time_t now = timing.getTimestamp();

    switch (state)
    {
    case Running:
        if (now - lastExecution)
        {
            // do something
        }
        break;
    }
}

where:

time_t Timing::getTimestamp()
{
    struct tm tm;
    getLocalTime(&tm);
    return mktime(&tm);
}

It seemed to me the application hangs (the fsm is called every second). Actually, looking at the getLocalTime implementation I don't understand what it does. Why it needs a while cycle and a delay of 10 ms every cycle, just to retrieve the current time?

I'm looking for the epoch time in second. Is my approach wrong?

Mark
  • 4,338
  • 7
  • 58
  • 120
  • It'll return true if the year more than 2017, so I suppose it's in case other thread changes current time?? I assume there is some RTOS and then the delay will be for suspending thread instead of busy wait.. – KIIV Jul 11 '22 at 14:36
  • after boot the year is 1970 until the time is retrieved from NTP service, which can take some seconds – Juraj Jul 11 '22 at 14:41
  • @Juraj not sure to understand. The NTP is handled manually (i.e. `configTime()`). Even if the year is 1970, it's ok for me. I handle this situation. But I cannot wait just to know the epoch time. Is there a more convenient function? – Mark Jul 11 '22 at 14:44
  • 1
    @Mark - you have the code right there. Just rename it and write your own. – romkey Jul 11 '22 at 20:12
  • standard C function to get 'epoch' is `time_t t = time(nullptr);` – Juraj Jul 12 '22 at 04:40
  • @romkey thank you for your kindness. I'm trying to understand *why* they wrote that code. Of course is there a reason for that, they are senior developer not like me :-) – Mark Jul 12 '22 at 06:01

2 Answers2

1

Thank you for raising this. I just found the same issue when using the ESP32Time by fbiego. Which as you have identified (Thanks) is that delay when fetching the time. Looking the code, it is always having that 5000ms delay while the time is set before 2016. I fixed mine by setting the rtc to the start of 2022. This gets rid of the delay for me until the GPS I am using gets a lock and gives me the correct time.

John_nz
  • 11
  • 4
1

How to fix that:
Just pass the 2nd parameter to the function. It will limit the delay:

getLocalTime(&timeinfo, 5);
It will finish in 10ms max (if current year is < 2016).

Why it is implemented that way:

Just sharing my guess:
Function returns immediately if the evaluated year is > 2016. And in this case it returns true, i.e. success.

In case if the evaluated year is < 2016, then such result is considered as failed result. So, function waits 10ms and does another attempt to calculate the time. It runs in that loops for 5 sec (5000 ms).

I think, the assumption here is that the correct date/time might be retrieved from NTP during this delay.
(surely it won't happen automatically. But technically there might be started request to the NTP server, running on the other core)

Alexander Stepaniuk
  • 6,217
  • 4
  • 31
  • 48