温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

lua中常用时间函数有哪些

发布时间:2021-10-21 14:05:26 来源:亿速云 阅读:181 作者:小新 栏目:开发技术

这篇文章主要介绍了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中常用时间函数有哪些”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI