问题
I've been trying to synchronize time with ntp servers, however, nodemcu seems to ignore the server parameter.
-- sync.lua
sntp.sync("fr.pool.ntp.org", function()
tm = rtctime.epoch2cal(rtctime.get())
print(string.format("%04d/%02d/%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]))
end)
Execution..
> dofile('sync.lua')
> 2017/05/22 21:38:39
The time response is the unix epoch time (https://www.epochconverter.com/). Is it supposed to be the server parameter time (in this case, France)? I tried several different servers (i.e http://www.pool.ntp.org/zone/europe) but the response stills the same.
Any suggestion? Thanks!
回答1:
The behavior is correct. If you want to work with timezones you need so called "zone files" from the tz database. Each tz file contains (amongst other info) transitions such as daylight saving time, and it also records leap seconds.
There's an example of how to deal with timezones in the NodeMCU repository.
tz = require('tz')
tz.setzone('eastern')
sntp.sync(nil, function(now)
local tm = rtctime.epoch2cal(now + tz.getoffset(now))
print(string.format("%04d/%02d/%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]))
end)
So, you need tz.lua plus the zone file(s) for the timezone(s) you're interested in ('eastern' in the example).
来源:https://stackoverflow.com/questions/44122622/sntp-sync-ignores-server