首页 > 技术文章 > [触动精灵] 时间戳和日期时间相互转化

zjl8455482 2019-08-20 21:27 原文

转载 https://www.cnblogs.com/Denny_Yang/p/6197435.html 

想不到lua下的时间戳和日期时间转化 有现成函数可供使用

1、时间戳转换成时间

local t = 1412753621000

function getTimeStamp(t)

return os.date("%Y%m%d%H",t/1000)

end

print(getTimeStamp(t))

2、得时间戳

os.time() -- 当前时间戳

os.time({day=17, month=5, year=2012, hour=0, min=0, sec=0}) -- 指定时间的时间戳

3、时间格式 yyyyMMddHHmmss

print(os.date("%Y%m%d%H%M%S", os.time()))



从上面的资料写出2个函数 日期时间到时间戳 时间戳到日期时间

--时间戳转日期时间 注意输出格式是xxxx-02-12 09:30:12
--参数可以是10位的时间戳 也可以是13位的 是数值也可以是字符串
function timeStampToData(tempTimeStamp)
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常即可    
            local result=""
            local temp=-1
            tempTimeStamp=tonumber(tempTimeStamp)
            if tempTimeStamp==nil then
                error("传递进来的时间戳不合法")
            end
            temp=string.len(tostring(tempTimeStamp))
            if temp==10 then
                result=os.date("%Y-%m-%d %H:%M:%S",tempTimeStamp)
            elseif temp==13 then
                result=os.date("%Y-%m-%d %H:%M:%S",tempTimeStamp/1000)
            else
                error("传进的时间戳长度不合法")
            end
            return result
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("timeStampToData") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }
end
--日期时间转时间戳 注意输出格式是xxxx-02-12 09:30:12
--参数可以是  “xxxx-02-12 09:30:12” 或者 表{2019,2,12,9,30,12}
function dataToTimeStamp(dataStr)
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常即可    
            local result=-1
            local tempTable={}
            traceprint(dataStr)
            if dataStr==nil then
                error("传递进来的日期时间参数不合法")
            elseif type(dataStr)=="string" then        
                dataStr=trim(dataStr)
                for v in string.gmatch(dataStr, "%d+") do--正则匹配出字符串里面的全部数字
                    tempTable[#tempTable+1]=v     
                end
            elseif type(dataStr)=="table" then
                tempTable=dataStr
            else
                error("传递进来的日期时间参数不合法")
            end
            traceprint(tonumber(tempTable[5]))
            traceprint(tonumber(tempTable[6]))
            result=os.time({day=tonumber(tempTable[3]), month=tonumber(tempTable[2]), year=tonumber(tempTable[1]), hour=tonumber(tempTable[4]), min=tonumber(tempTable[5]), sec=tonumber(tempTable[6])}) -- 指定时间的时间戳

            return result
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("dataToTimeStamp") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }
end

由此我们可以写个检测脚本到期时间的函数

config={}
--到期时间
config["isExpireTime"]=true--是否判断过期
config["expireTime"]="2019-08-02 12:30:12" --过期时间

-脚本是否过期判断 设置过期时间在配置表的config["expireTime"]里面设置 格式是"2019-10-02 12:30:12"或者表{2019,10,2,12,30,12} 开启判断是 config["isExpireTime"] =true
--返回值没有 过期了就弹出窗口然后停止脚本
function isExpireTime()
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常即可            
            local result=-1
            local nowTime
            local expireTime
            
            if config["isExpireTime"] then
                nowTime=getNetTime()
                expireTime=dataToTimeStamp(config["expireTime"])
                if nowTime>expireTime then
                    --过期了                    
                    result=1
                    traceprint("脚本过期了 请检查")
                    dialog("脚本过期了 请检查", 0)
                    lua_exit()
                else
                    --在试用期内
                    result=-1
                    traceprint("在试用期内 到期时间[".. tostring(config["expireTime"])  .. "]")
                end        
            else
                
            end
            --return result
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("isExpireTime") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }    
end

if config["isExpireTime"] then--判断下脚本是否到期
    isExpireTime()
end


123

推荐阅读