千家信息网

lua中常用时间函数有哪些

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章主要介绍了lua中常用时间函数有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。-- 功能函数: 获取当前服务器时间, nu
千家信息网最后更新 2026年10月31日lua中常用时间函数有哪些

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

时间 天数 篇文章 活动 函数 中常 两个 年月 年月日 剩余 服务 中国人 事件 价值 兴趣 功能 单位 同时 小时 数据 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 保险科技 互联网保险 南京欣网互联网络科技有限公司 互联网科技深圳 唐信互联网科技 猫创互联网科技 衡水科技产业互联网平台 太原新华互联网科技学校怎么样 农村开互联网科技讲堂 互联网科技寓意 互联网科技创新服务 国大互联网教育科技有限公司工作 济南有互联网科技公司吗 有人说阿里系是互联网科技企业 互联网科技机房 中国科技和美国互联网 北京互联网科技类企业分布 玉溪新华互联网科技怎么选 北京品多多互联网科技有限公司 互联网企业和网络科技有限公司 互联网科技快速发展优美句子 云南咖啡 互联网科技企业 云之行互联网科技 互联网科技产业 深圳互联网络科技有限公司 壕鑫互联网络科技 米聚互联网科技南京有限公司排面 银川新华互联网科技学校联系电话 亚光科技低轨卫星互联网 广州全民伙伴互联网科技有限公司
0