Sntp.sync()忽略服务器 [英] Sntp.sync() ignores server
问题描述
我一直在尝试与ntp服务器同步时间,但是,nodemcu似乎忽略了服务器参数.
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)
执行..
> dofile('sync.lua')
> 2017/05/22 21:38:39
时间响应是Unix纪元时间( https://www.epochconverter.com/) .是否应该是服务器参数时间(在这种情况下是法国)?我尝试了几种不同的服务器(即 http://www.pool.ntp.org/zone/europe ),但响应仍然相同.
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.
有什么建议吗?谢谢!
推荐答案
行为正确.如果您想使用时区,则需要使用tz数据库中的区域文件" .每个tz文件都包含(除其他信息外)诸如夏时制的过渡,并且还记录了leap秒.
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.
NodeMCU存储库中有一个如何处理时区的示例.
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)
因此,您需要 tz.lua
a>加上您感兴趣的时区的区域文件(在示例中为东部").
So, you need tz.lua
plus the zone file(s) for the timezone(s) you're interested in ('eastern' in the example).
这篇关于Sntp.sync()忽略服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!