[觸動精靈] 時間戳和日期時間相互轉化


轉載 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM