Sntp.sync() 忽略服务器

Sntp.sync() ignores server

我一直在尝试与ntp服务器同步时间,然而,nodemcu似乎忽略了服务器参数。

-- 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)

执行..

> dofile('sync.lua')
> 2017/05/22 21:38:39

时间响应是 unix 纪元时间 (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) 但响应仍然相同。

有什么建议吗?谢谢!

行为正确。如果你想使用时区,你需要所谓的 "zone files" from the tz database。每个 tz 文件包含(除其他信息外)转换,例如夏令时,它还记录闰秒。

有一个如何处理 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)

因此,您需要 tz.lua 加上您感兴趣的时区的区域文件(示例中为 'eastern')。