溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

lua中常用時(shí)間函數(shù)有哪些

發(fā)布時(shí)間:2021-10-21 14:05:26 來源:億速云 閱讀:176 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了lua中常用時(shí)間函數(shù)有哪些,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

-- 功能函數(shù): 獲取當(dāng)前服務(wù)器時(shí)間, num型

function Player:get_cur_sys_time()

    local last = self.time_span or 0

return os.time() - last

end

-- 獲取現(xiàn)在的日期

function CommonFunc_getNowDate()

local nd = os.date("*t", g_player:get_cur_sys_time())

return {

year = nd.year,

month = nd.month,

day = nd.day,

hour = nd.hour,

minute = nd.min,

second = nd.sec

}

end

--把服務(wù)端發(fā)過來的stme 轉(zhuǎn)換為秒為單位

function CommonFunc_ConvertDateToTime(stime)

    local timeDate = os.date("*t",os.time())

    -- Log(timeDate)

    timeDate.year = stime.year

    timeDate.month = stime.month

    timeDate.day = stime.day

    timeDate.hour = stime.hour or 0

    timeDate.min = stime.minute or 0

    timeDate.sec = stime.second or 0

    -- Log(timeDate)

    print("os.time",os.time(timeDate))

    return os.time(timeDate)

end

-- 獲取某年某月的天數(shù)

function UIShareToCircle:getMonthDays(year, month)

local time1 = CommonFunc_ConvertDateToTime({year=year, month=month, day=1})

-- local time1 = STime:dateToTime({year=year, month=month, day=1})

local nextYear, nextMonth = year, month + 1

if nextMonth > 12 then

nextYear = year + 1

nextMonth = 1

end

local time2 = CommonFunc_ConvertDateToTime({year=nextYear, month=nextMonth, day=1})

-- local time2 = STime:dateToTime({year=nextYear, month=nextMonth, day=1})

return math.floor((time2 - time1)/(24*3600))

end

--獲取同一年內(nèi),從幾月幾日 到幾月 總共的天數(shù)

function UIShareToCircle:calSpaceDaysByMonth( startDay, startMonth, startYear, endMonth, spaceDays)

for month = startMonth,endMonth,1 do

local monthDays = self:getMonthDays(startYear, month)

if month == startMonth then

spaceDays = monthDays - startDay

else

spaceDays = spaceDays  + monthDays

end

end

return spaceDays

end

--計(jì)算從上一次登錄到現(xiàn)在,已經(jīng)幾天了

function UIShareToCircle:calInternalDays(startDate)

   local spaceDays = 0-- 間隔的天數(shù)

local startDate = startDate--測(cè)試數(shù)據(jù)*******************

local nowDate = CommonFunc_getNowDate()--當(dāng)前的年月日

-- Log("62************",nowDate,startDate)

--先比較年

if nowDate.year > startDate.year then

for i = startDate.year,nowDate.year,1 do

if i == startDate.year then

spaceDays = self:calSpaceDaysByMonth( startDate.day, startDate.month, startDate.year, 12, spaceDays)

elseif i == nowDate.year then

spaceDays = self:calSpaceDaysByMonth( nowDate.day, 1, nowDate.year, nowDate.month, spaceDays)

else

for month =1,12,1 do

local monthDays =  self:getMonthDays(i, month)

spaceDays = spaceDays  + monthDays

end

end

end

else--比較月

if nowDate.month > startDate.month then

for i = startDate.month,nowDate.month,1 do

local monthDays =  self:getMonthDays(nowDate.year, i)

if i == startDate.month then

spaceDays = monthDays - startDate.day

elseif i == nowDate.month then

spaceDays = spaceDays + nowDate.day

else

spaceDays = spaceDays  + monthDays

end

end

else--比較日

spaceDays = nowDate.day - startDate.day

end

end

return spaceDays

end

-------------------------------------------------------------------------------

--獲取當(dāng)前時(shí)間

-------------------------------------------------------------------------------

function CommonFunc_getCurTime()

local tbCurTime = os.date("*t",g_Const_Time_Diff + os.time())

-- 將事件轉(zhuǎn)化為符合中國(guó)人習(xí)慣的

if tbCurTime.wday == 1 then

tbCurTime.wday = 7

else

tbCurTime.wday = tbCurTime.wday - 1

end

return tbCurTime

end

-------------------------------------------------------------------------------

-- 時(shí)間的一些操作****

-- 判斷一個(gè)時(shí)間是不是在兩個(gè)時(shí)間之內(nèi)

-- GuildBossOpenTime = {

-- ["start_timer"] = { {2014,1,1} },

-- ["end_timer"]= { {2099,12,31} },

-- ["day"]= {1,2,3,4,5},

-- ["hour"]= {0,24}

-- }

-------------------------------------------------------------------------------

function CommmonFunc_compTimer(tbData, tbCurTime)

tbCurTime = tbCurTime or CommonFunc_getCurTime()

local yFlag = CommonFunc_compDataBetData(tbData["start_timer"][1], tbData["end_timer"][1], 

{tbCurTime["year"], tbCurTime["month"], tbCurTime["day"]} )

-- 如果周時(shí)間滿足

local wFlag = tbData["day"]:has(tbCurTime["wday"])

-- 如果小時(shí)時(shí)間滿足

local hFlag = CommonFunc_compDataBetHS(tbData["hour"][1], tbData["hour"][2], tbCurTime["hour"])

return yFlag and wFlag and hFlag

end

function CommonFunc_compDataBetData(tbData1,tbData2,detData)

if detData[1] < tbData1[1] or detData[1] > tbData2[1] then

return false

end

if detData[1] == tbData1[1] or detData[1] == tbData2[1] then

if detData[2] < tbData1[2] or detData[2] > tbData2[2] then

return false

end

if detData[2] == tbData2[2] or detData[2] == tbData2[2] then

if detData[3] < tbData1[3] or detData[3] > tbData2[3] then

return false 

end

end

end

return true

end

----------------------------------------------------------------------------

-- 判斷一個(gè)時(shí)間是不是在兩個(gè)時(shí)間之內(nèi)

function CommonFunc_compDataBetHS(tbData1,tbData2,detData)

if detData >= tbData1 and detData < tbData2 then

return true

end

return false

end

-- 獲取活動(dòng)剩余的天數(shù)

function UIActivity:getLeftTime(activity_tplt)

local leftDays = 0-- 剩余的天數(shù)

local nowTb = CommonFunc_getCurTime()--當(dāng)前的年月日

local startTb = activity_tplt.start_time--活動(dòng)開始的時(shí)間

local endTb = activity_tplt.end_time--活動(dòng)結(jié)束的時(shí)間

--先判斷當(dāng)前的時(shí)間在不在活動(dòng)時(shí)間內(nèi)

local tbDate = List({["start_timer"] = List({ {startTb.year,startTb.month,startTb.day,startTb.hour} }),

["end_timer"]= List({ {endTb.year,endTb.month,endTb.day,endTb.hour} }),

["day"]= List({1,2,3,4,5,6,7}),

["hour"]= List({0,24})

})

local inActivityFlag = CommmonFunc_compTimer(tbDate)

if inActivityFlag then

--先比較年

if endTb.year > nowTb.year then

for i = nowTb.year,endTb.year,1 do

if i == nowTb.year then

for month = nowTb.month,12,1 do

local monthDays =  self:getMonthDays(nowTb.year, month)

if month == nowTb.month then

leftDays = monthDays - nowTb.day

else

leftDays = leftDays  + monthDays

end

end

elseif i == endTb.year then

for month = 1,endTb.month,1 do

local monthDays = self:getMonthDays(i, month)

if month == endTb.month then

leftDays = leftDays + endTb.day

else

leftDays = leftDays  + monthDays

end

end

else

for month =1,12,1 do

local monthDays =  self:getMonthDays(i, month)

leftDays = leftDays  + monthDays

end

end

end

else--比較月

if endTb.month > nowTb.month then

for i = nowTb.month,endTb.month,1 do

local monthDays =  self:getMonthDays(endTb.year, i)

if i == nowTb.month then

leftDays = monthDays - nowTb.day

elseif i == endTb.month then

leftDays = leftDays + endTb.day

else

leftDays = leftDays  + monthDays

end

end

else--比較日

leftDays = endTb.day - nowTb.day

end

end

end

return string.format(StringRes["activity_copy_1"],leftDays)

end

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“l(fā)ua中常用時(shí)間函數(shù)有哪些”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI