Getting time and date from ESP32

I would like to get current time from ESP32.
Can it works without rtc chips like DS3231, for example, via NTP?

char timestamp[10];

	if(mgos_strftime(timestamp, 10, "%I:%Mp", mg_time()) != -1)
		LOG(LL_INFO, ("Here at C it is now %s", timestamp));
function nowstuff() {
    let now = Timer.now();
    let text='Timer.now() returns ' + JSON.stringify(now) + ' (UTC); that corresponds to ' + Timer.fmt("%F %T", now) + ' in your configured timezone';
    Log.print(Log.INFO,text);
    return text;
};
config_schema:
  - ["sntp.server", "time.google.com"] # Server address, optional, defaults to Google
  - ["sntp.update_interval", 7200] # Update interval, optional. 0 => one-off sync
  - ["sys.tz_spec", "ART3"] # Timezone spec in tzset format
1 Like

Thanks for your reply.
All is ok, but I can’t set “sys.tz_spec” for my time zone.
My tz is Moscow (MSK). I set [“sys.tz_spec”, “MSK”] and [“sys.tz_spec”, “MSK3”] and your [“sys.tz_spec”, “ART3”] it shows me the same time 16.15 for every tz (my real time is 22.15)

Here I find codes, but the result is the same

Unfortunately I can’t help you with the tzset format. I remember I searched the net and found a library from where I got a clue, and somehow then I guess I barely understood what I needed to set the time.
The number after the three-letter identifier is the offset from GMT, I am at GMT-3 and so I’ve set it to 3. Daylight savings time is currently not used here, so that’s all I needed.
You seem to be at GMT+3 so I guess yours should be “MSK-3” but…

Bear in mind that you will only get local time when you apply a transformation function
Also, check the log at boot time to see which timezone the system is reading

Thanks!! “MSK-3” is exactly what is needed!.
But I found what was a problem…
I try to set “sys.tz_spec” parameter by mos utility, but the command mos call Config.Set '{"sys.tz_spec": MSK-3}' doesn’t work: it shows me {“saved”: true}, but the value “sys.tz_spec” is old. Had to change “sys.tz_spec” in mos.yml.

I guess tz_spec is only read at boot time (that’s why I told you to check the log at boot time), perhaps at time change too, but I don’t know for sure.