码迷,mamicode.com
首页 > 其他好文 > 详细

lua获取网络当前时间戳

时间:2015-12-30 09:21:49      阅读:1178      评论:0      收藏:0      [点我收藏+]

标签:

使用luasocket

local socket = require "socket.core"
local server_ip = {
        "time-nw.nist.gov",
        "time-a.nist.gov",
        "time-b.nist.gov",
        "time.nist.gov",
        "time-c.nist.gov",
        "time-d.nist.gov",
    }

function nstol(str)
    assert(str and #str == 4)
    local t = {str:byte(1,-1)}
    local n = 0
    for k = 1, #t do
        n= n*256 + t[k]
    end
    return n
end

-- get time from a ip address, use tcp protocl
function gettime(ip)
    print(connect , ip)
    local tcp = socket.tcp()
    tcp:settimeout(10)
    tcp:connect(ip, 37)
    success, time = pcall(nstol, tcp:receive(4))
    tcp:close()
    return success and time or nil
end

function nettime()
    for _, ip in pairs(server_ip) do
        time = gettime(ip)
        if time then 
            --print(os.date("%c", time-2208988800))
            print(time-2208988800)
            --return time-2208988800
        end
    end
end

nettime()

 

lua获取网络当前时间戳

标签:

原文地址:http://www.cnblogs.com/em-x/p/5087699.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!