您好,登錄后才能下訂單哦!
這篇文章主要介紹了lua中常用時間函數有哪些,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
-- 功能函數: 獲取當前服務器時間, num型
function Player:get_cur_sys_time()
local last = self.time_span or 0
return os.time() - last
end
-- 獲取現在的日期
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
--把服務端發過來的stme 轉換為秒為單位
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
-- 獲取某年某月的天數
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
--獲取同一年內,從幾月幾日 到幾月 總共的天數
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
--計算從上一次登錄到現在,已經幾天了
function UIShareToCircle:calInternalDays(startDate)
local spaceDays = 0-- 間隔的天數
local startDate = startDate--測試數據*******************
local nowDate = CommonFunc_getNowDate()--當前的年月日
-- 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
-------------------------------------------------------------------------------
--獲取當前時間
-------------------------------------------------------------------------------
function CommonFunc_getCurTime()
local tbCurTime = os.date("*t",g_Const_Time_Diff + os.time())
-- 將事件轉化為符合中國人習慣的
if tbCurTime.wday == 1 then
tbCurTime.wday = 7
else
tbCurTime.wday = tbCurTime.wday - 1
end
return tbCurTime
end
-------------------------------------------------------------------------------
-- 時間的一些操作****
-- 判斷一個時間是不是在兩個時間之內
-- 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"]} )
-- 如果周時間滿足
local wFlag = tbData["day"]:has(tbCurTime["wday"])
-- 如果小時時間滿足
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
----------------------------------------------------------------------------
-- 判斷一個時間是不是在兩個時間之內
function CommonFunc_compDataBetHS(tbData1,tbData2,detData)
if detData >= tbData1 and detData < tbData2 then
return true
end
return false
end
-- 獲取活動剩余的天數
function UIActivity:getLeftTime(activity_tplt)
local leftDays = 0-- 剩余的天數
local nowTb = CommonFunc_getCurTime()--當前的年月日
local startTb = activity_tplt.start_time--活動開始的時間
local endTb = activity_tplt.end_time--活動結束的時間
--先判斷當前的時間在不在活動時間內
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
感謝你能夠認真閱讀完這篇文章,希望小編分享的“lua中常用時間函數有哪些”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。