Sntp.sync() 忽略服务器 [英] Sntp.sync() ignores server

查看:14
本文介绍了Sntp.sync() 忽略服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试与 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 文件都包含(以及其他信息)转换,例如夏令时,并且还记录闰秒.

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 加上您感兴趣的时区的区域文件(示例中的东方").

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆