我一直在尝试与 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/ https://www.epochconverter.com/)。它应该是服务器参数时间(在本例中为法国)吗?我尝试了几个不同的服务器(即http://www.pool.ntp.org/zone/europe http://www.pool.ntp.org/zone/europe)但响应仍然相同。
有什么建议吗?谢谢!
行为是正确的。如果你想使用时区,你需要所谓的tz 数据库中的“区域文件” https://en.wikipedia.org/wiki/Tz_database。每个 tz 文件包含(以及其他信息)转换,例如夏令时,并且还记录闰秒。
有一个如何处理的例子NodeMCU 存储库中的时区 https://github.com/nodemcu/nodemcu-firmware/tree/master/lua_examples/timezone.
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 https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/timezone/tz.lua加上您感兴趣的时区的区域文件(示例中为“东部”)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)