8266获取网络时间
生活随笔
收集整理的這篇文章主要介紹了
8266获取网络时间
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
8266獲取網(wǎng)絡(luò)時間
今天第一次用阿里的博客寫點(diǎn)東西感受一下....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)現(xiàn)在說一下問題
國際時間和北京時間相差8小時,所以會發(fā)現(xiàn)所獲得的時間會在北京時間8點(diǎn)的時候變化
會問怎么會有31小時,這就是問題點(diǎn)1
國際時間小時是從0-23變化,加了8小時所以變化為8-31
所以獲得時間后直接加8小時是不對的
而且還會發(fā)現(xiàn)日子總是在每天的8點(diǎn)變化,肯定不對
最簡單的方法
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)
直接設(shè)置系統(tǒng)的時間加8小時,這樣的話系統(tǒng)就能讓日期在零點(diǎn)的時候變化,而且不會向上面似得出現(xiàn)31這種不對的時間
現(xiàn)在的數(shù)據(jù)
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總結(jié)
以上是生活随笔為你收集整理的8266获取网络时间的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: EMC VMAX的磁盘构成,fast p
- 下一篇: java.util.Date和java.