nodemcu / nodemcu-firmware

Lua based interactive firmware for ESP8266, ESP8285 and ESP32
https://nodemcu.readthedocs.io
MIT License
7.61k stars 3.12k forks source link

ESP32: IDFv5.0 fix `time` module #3609

Closed serg3295 closed 5 months ago

serg3295 commented 11 months ago

The time.get() function returns an incorrect value of a second in the firmware [5.3-int32-singlefp] on IDF v5.0.2

test script:


// dbg was added in time.c

34  gettimeofday (&tv, NULL);
35  lua_pushnumber (L, tv.tv_sec);
36  lua_pushnumber (L, tv.tv_usec);
37  NODE_DBG("tv_sec:  %llu\n", tv.tv_sec);
38  NODE_DBG("tv_usec: %lu\n", tv.tv_usec);
39  return 2;

-- Lua script

getTime = function()
  ts, tms = time.get()
  print(string.format('%d %d', ts, tms))

  et = time.epoch2cal(time.get())
  print(string.format("%04d-%02d-%02d %02d:%02d:%02d DST:%d",
    et["year"], et["mon"], et["day"],
    et["hour"], et["min"], et["sec"], et["dst"]))

  lt = time.getlocal()
  print(string.format("%04d-%02d-%02d %02d:%02d:%02d DST:%d",
  lt["year"], lt["mon"], lt["day"],
  lt["hour"], lt["min"], lt["sec"], lt["dst"]))
end

tt = tmr.create()
tt:alarm(3000, tmr.ALARM_AUTO, getTime)

tt:stop()

test output:

tv_sec:  1690272183
tv_usec: 497059
1690272128 497059   <- wrong sec value
tv_sec:  1690272183
tv_usec: 498844
2023-07-25 08:02:08 DST:0  <- wrong time.epoch2cal
2023-07-25 08:03:03 DST:0  <- correct time.getlocal

This PR fixes the bug. I have tested the PR on firmware:

Lua 5.1-fp works correctly as well.

pjsg commented 5 months ago

Looks good to me.....