欢迎来到我的博客小站。  交流请加我微信好友: studyjava。  也欢迎关注同名公众号:Java学习之道

unity之lua计算某一时间段开始和结束时间戳

  |   0 评论   |   0 浏览

推荐阅读:

1.计算某一有效时间的开始时间戳和结束时间戳

starttime,endtime:某个活动的开始时间,结束时间
opentime :开服时间或者创角时间

--获取剩余时间:某时间戳距离当天23:59:59秒的时间差
local function GetReaminSecondsTo24(time)
    local toYear=os.date("*t",time).year
    local toMonth=os.date("*t",time).month
    local toDay=os.date("*t",time).day
    local toTime = os.time({year =toYear, month = toMonth, day =toDay, hour =23, min =59, sec = 59})
    return toTime-time   
end

local function GetStartAndEndTm(type,starttime,endtime,opentime)
	local start_tm,end_tm
	if type == 1 or type == 2 then
		--从某时间起,第X天到X天:例如开服1-7
		local leftTm = GetReaminSecondsTo24(opentime)
        local leftDay = endtime - starttime
        local now = GameDataManager.GetServerNowTime()
        if starttime > 1 then
            start_tm = opentime + (starttime-2)*60*60*24+leftTm+1
        else
            start_tm = opentime
        end
        end_tm = leftTm + leftDay*60*60*24+opentime
	elseif type == 3 then --例如:2021/10/23/00/00/00————-2021/10/25/00/00/00
		--
		local arr1 = string.split(starttime,"/")
        local tm_start = os.time({day=arr1[3], month=arr1[2], year=arr1[1], hour=arr1[4], minute=arr1[5], second=arr1[6]})
        local arr2 = string.split(endtime,"/")
        local tm_end = os.time({day=arr2[3], month=arr2[2], year=arr2[1], hour=arr2[4], minute=arr2[5], second=arr2[6]})
        start_tm = tm_start
        end_tm = tm_end
	end
	return start_tm,end_tm
end

2.获取剩余时间

某时间戳距离当天23:59:59秒的时间差

function GetReaminSecondsTo24(time)
    local toYear=os.date("*t",time).year
    local toMonth=os.date("*t",time).month
    local toDay=os.date("*t",time).day
    local toTime = os.time({year =toYear, month = toMonth, day =toDay, hour =23, min =59, sec = 59})
    return toTime-time   
end

3.判断某时间戳是否是当天

function CheckSameDay(timestamp)
    local today = os.date("*t")
    local secondOfToday = os.time({day=today.day, month=today.month,year=today.year, hour=0, minute=0, second=0})
    if timestamp >= secondOfToday and timestamp < secondOfToday + 24 * 60 * 60 then
        return true
    else
        return false
    end
end

标题:unity之lua计算某一时间段开始和结束时间戳
作者:mmzsblog
地址:https://www.mmzsblog.cn/articles/2022/05/04/1651648420990.html
-----------------------------
如未加特殊说明,此网站文章均为原创。
若本站转载文章遗漏了原文链接,请及时告知,我们将做删除处理!
网站转载须在文章起始位置标注作者及原文连接,否则保留追究法律责任的权利。
公众号转载请联系网站首页的微信号申请白名单!

个人微信公众号 ↓↓↓                 

微信搜一搜爱上游戏开发