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