Can't get timezone to work (ESP32) [SOLVED]

#1

My goal is: to fetch current local time .
My actions are: I have included SNTP library in mos.yml, configured sys.tz_spec, and invoked Timer.now() function.
The result I see is: Time as UTC, not local time, no matter what I set in sys.tz_spec
My expectation & question is:
I am expecting to get the current local time (in my timezone). As that is what was answered here
However, I always get UTC.
What am I missing ?
Logs follow

[Jun  3 15:51:30.447] mgos_core.c:104         Setting TZ to '<-03>3'
[Jun  3 15:51:35.723] mgos_sntp.c:95          SNTP query to time.google.com
[Jun  3 15:51:35.909] mgos_sntp.c:59          SNTP reply from 216.239.35.12: time 1591210296.611131, local 5.702667, delta 1591210290.908464
[Jun  3 15:51:35.922] init.js:10              Timer.now() returns 1591210296.612774
[Jun  3 15:51:49.024] mgos_http_server.c:180  0x3ffcb0a8 HTTP connection from 192.168.69.11:40338
[Jun  3 15:51:49.031] mg_rpc.c:293            Do.now via HTTP 192.168.69.11:40338
[Jun  3 15:51:49.045] init.js:10              Timer.now() returns 1591210309.734524

1591210309 Is equivalent to: 06/03/2020 @ 6:51pm (UTC); ART is as mos shows (-3)

[Jun  3 15:53:31.438] mgos_core.c:104         Setting TZ to 'EST5EDT4,M4.1.0/02:00:00,M10.5.0/02:00:00'
[Jun  3 15:53:37.010] mgos_sntp.c:95          SNTP query to time.google.com
[Jun  3 15:53:37.199] mgos_sntp.c:59          SNTP reply from 216.239.35.8: time 1591210418.197341, local 6.007640, delta 1591210412.189701
[Jun  3 15:53:37.212] init.js:10              Timer.now() returns 1591210418.198974
[Jun  3 15:53:49.718] mgos_http_server.c:180  0x3ffcb118 HTTP connection from 192.168.69.11:40352
[Jun  3 15:53:49.725] mg_rpc.c:293            Do.now via HTTP 192.168.69.11:40352
[Jun  3 15:53:49.738] init.js:10              Timer.now() returns 1591210430.724517

1591210418 Is equivalent to: 06/03/2020 @ 6:53pm (UTC); EST is expected

#2

NTP is intended to synchronize all participating computers to within a few milliseconds of “Coordinated Universal Time” (UTC).

It is your resposibility to display the localtime in the format you need.

  time_t t = time(0);
  struct tm timeinfo;
  localtime_r(&t, &timeinfo);
  char timestamp[24];
  strftime(timestamp, sizeof(timestamp), "%F %T%Z", &timeinfo);
  const char *tz = mgos_sys_config_get_sys_tz_spec();
  LOG(LL_INFO, ("tz: %s, epoch: %ld, localtime: %s", tz, t, timestamp));

Results:

tz: EET-2EEST,M3.5.0/3,M10.5.0/4, epoch: 1591246377, localtime: 2020-06-04 07:52:57EEST
tz: EST5EDT4,M4.1.0/02:00:00,M10.5.0/02:00:00, epoch: 1591246523, localtime: 2020-06-04 00:55:23EDT
tz: EST5EDT4,M4.1.0/02,M10.5.0/02, epoch: 1591246663, localtime: 2020-06-04 00:57:43EDT
1 Like
#3

SOLVED
For anyone else arriving here:

Timer.now() is now, but not here.
The conversion is actually done by the formatting function, if you call Timer.fmt() you get the local time for your timezone:

    let now = Timer.now();
    let text='Timer.now() returns ' + JSON.stringify(now) + ' and that corresponds to ' + Timer.fmt("%I:%M%p.", now);

Timer.now() returns 1591638090.184137 and that corresponds to 02:41PM.
1591638090 Is equivalent to: 06/08/2020 @ 5:41pm (UTC)

PS:
Timer.now() calls mg_time() that calls cs_time() that calls gettimeofday()
Timer.fmt() calls mgos_strftime() that calls localtime() and strftime()

Unix time reset using Timer.now()