8266获取网络时间

简介: 8266获取网络时间 今天第一次用阿里的博客写点东西感受一下....可以参考一下这篇文章,不过有点问题,我后面会说https://www.jianshu.com/p/2d71cc18cfa7 sntp.

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
目录
相关文章
|
XML 数据格式 移动开发
多方法获取网络时间
       在做YH维护的时候,偶尔会碰到这样的问题:电脑的非正常关机导致系统时间出错(变为了2002-1-1),从而影响到项目系统的使用。尤其是设计到money的系统,如果时间错误,可能会导致无法想象的后果。
881 0
|
C# 网络协议
c#获取网络时间并同步本地时间
原文:c#获取网络时间并同步本地时间 通过TCP形式来获取NTP时间。主要代码如下: [DllImport("kernel32.dll")] private static extern bool SetLocalTime(ref Systemtime time); [StructLayout(LayoutKind.
3036 0
|
算法 C++ Windows
C++ 取得系统当前时间
#include //* 方法一 time_t tt = time(NULL);//这句返回的只是一个时间cuo tm* t= localtime(&tt); printf("%d-%02d-%02d %02d:%02d:%02d\n",   t->tm_year + 1900,  t->...
801 0
|
数据挖掘 计算机视觉
R-C3D:用于时间活动检测的区域3D网络
论文原称:R-C3D: Region Convolutional 3D Network for Temporal Activity Detection(2017)
R-C3D:用于时间活动检测的区域3D网络
你的时间有限,不要为别人而活 ! 2018-01-01!
你的时间有限,不要为别人而活 ! 2018-01-01!
975 0
|
定位技术 C#
C#编程练习(03):北斗时间系统、GPS时间系统及其与UTC时间系统之间的转换
C#编程练习(03):北斗时间系统、GPS时间系统及其与UTC时间系统之间的转换
C#编程练习(03):北斗时间系统、GPS时间系统及其与UTC时间系统之间的转换