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







你可能感兴趣的:(Lua)