8266获取网络时间
今天第一次用阿里的博客写点东西感受一下....
sntp.sync("ntp1.aliyun.com",function()print("sync succeeded")end,function(index)print("failed : "..index)end)
用的SNTP
然后打印时间
time = rtctime.epoch2cal(rtctime.get()
timer =     string.format("%04d;%02d;%02d;%02d;%02d;%02d;%01d", time["year"], time["mon"], time["day"], time["hour"]+8, time["min"], time["sec"],time["wday"])
--print(timer)                    
local DataList = split(timer, ';');    
year = tonumber(DataList[1]);
month = tonumber(DataList[2]);
day = tonumber(DataList[3]);
hour = tonumber(DataList[4]);
minute = tonumber(DataList[5]);
second = tonumber(DataList[6]);
weekday = tonumber(DataList[7]); print(year,month,day,hour,minute,second,weekday)
现在说一下问题
国际时间和北京时间相差8小时,所以会发现所获得的时间会在北京时间8点的时候变化
      年     月   日    时    分   秒  星期2018    3    3    31    0    0    7
2018    3    3    31    0    1    7
2018    3    3    31    0    2    7
2018    3    3    31    0    3    7
2018    3    3    31    0    4    7
2018    3    3    31    0    5    7
2018    3    3    31    0    6    7
2018    3    3    31    59    54    7
2018    3    3    31    59    55    7
2018    3    3    31    59    56    7
2018    3    3    31    59    57    7
2018    3    3    31    59    58    7
2018    3    3    31    59    59    7
2018    3    4    8    0    0    1
2018    3    4    8    0    1    1
2018    3    4    8    0    2    1
2018    3    4    8    0    3    1
2018    3    4    8    59    51    1
2018    3    4    8    59    52    1
2018    3    4    8    59    53    1
会问怎么会有31小时,这就是问题点1
国际时间小时是从0-23变化,加了8小时所以变化为8-31
所以获得时间后直接加8小时是不对的
而且还会发现日子总是在每天的8点变化,肯定不对
最简单的方法
time = rtctime.epoch2cal(rtctime.get()+28800)
timer = string.format("%04d;%02d;%02d;%02d;%02d;%02d;%01d", time["year"], time["mon"], time["day"], time["hour"], time["min"], time["sec"],time["wday"])
--print(timer)                    
local DataList = split(timer, ';');    
year = tonumber(DataList[1]);
month = tonumber(DataList[2]);
day = tonumber(DataList[3]);
hour = tonumber(DataList[4]);
minute = tonumber(DataList[5]);
second = tonumber(DataList[6]);
weekday = tonumber(DataList[7]); print(year,month,day,hour,minute,second,weekday)
time = rtctime.epoch2cal(rtctime.get()+28800)
直接设置系统的时间加8小时,这样的话系统就能让日期在零点的时候变化,而且不会向上面似得出现31这种不对的时间
现在的数据
2018    3    5    23    59    50    2
2018    3    5    23    59    51    2
2018    3    5    23    59    52    2
2018    3    5    23    59    53    2
2018    3    5    23    59    54    2
2018    3    5    23    59    55    2
2018    3    5    23    59    56    2
2018    3    5    23    59    57    2
2018    3    5    23    59    58    2
2018    3    5    23    59    59    2
2018    3    6    0    0    0    3
2018    3    6    0    0    1    3
2018    3    6    0    0    2    3
2018    3    6    0    0    3    3
2018    3    6    0    0    4    3
2018    3    6    0    0    5    3
2018    3    6    0    0    6    3
2018    3    6    0    0    7    3
2018    3    6    0    0    8    3
2018    3    6    0    0    9    3
2018    3    6    0    0    10    3