Lua学习笔记--简单的时间操作

暂时抛弃所学的客户端知识,从零开始做服务器,一个月加油吧


--其中.......["TimeBegin"]={10,1},["TimeEnd"]={10,8}
function actJudgeActivityVisible(tReq)
	
	--local infotable = stringToTable(tReq.data)
	--local timebegin = infotable["TimeBegin"]
	--local timeend = infotable["TimeEnd"]
	
	local nowmouth = os.date("%m",os.time());
	local nowday = os.date("%d",os.time());
	
	for i = 1, #ActivityPlaza1Info do
		if	tonumber(nowmouth) >= ActivityPlazaList[i]["TimeBegin"][1] and
			tonumber(nowday) >= ActivityPlazaList[i]["TimeBegin"][2] and
			tonumber(nowmouth) <= ActivityPlazaList[i]["TimeEnd"][1] and
			tonumber(nowday) <= ActivityPlazaList[i]["TimeEnd"][2] then
			return 'T'
		else
			return 'F'
		end
	end
end

这是刚开始写的一个判断在规定时间内才开启活动的功能,但是可能方法比较影响效率,需要重写

我的想法是表中储存着秒数,然后提取出来再判断,首先我需要知道某天的秒数,比如2015-10-8 0:00:00


如下为代码

date = {
    year = 2015,
    month = 10,
    day = 8,
    hour = 0,
    min = 0,
    sec = 0
}
local t = os.time(date)
这样 t 就是秒数了,它为1443628800


然后代码修改为

function actJudgeActivityVisible(tReq)

	local nowday = tonumber(os.date("%m%d",os.time()));

	for i = 1, #ActivityPlaza1Info do
		--如果开始时间为零
		if ActivityPlazaList[i]["TimeBegin"] == 0 then
			return 'F'
		--将秒数转换成时间数字
		local timeBegin = tonumber(os.date("%m%d",ActivityPlazaList[i]["TimeBegin"]))
		local timeEnd = tonumber(os.date("%m%d",ActivityPlazaList[i]["TimeEnd"]))
		--判断
		if	nowday >= timeBegin and
			nowday <= timeEnd then
			return 'T'
		else
			return 'F'
		end
	end
end







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值