关于时间的几个函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于时间的几个函数相关的知识,希望对你有一定的参考价值。

-- 功能函数: 获取当前服务器时间, 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


以上是关于关于时间的几个函数的主要内容,如果未能解决你的问题,请参考以下文章

关于C语言的字符串的几个函数

[JSONP]关于JSONP的几个点

关于 ip 操作的几个实用函数

关于 ip 操作的几个实用函数

关于go语言的几个陷阱

关于c语言的几个输入输出函数的问题