cocos2dx-lua 一些公共方法

前端之家收集整理的这篇文章主要介绍了cocos2dx-lua 一些公共方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@function getStringTimeForInt(timeInt)

@H_502_0@if(tonumber(timeInt) <= 0)then

@H_502_0@return "00:00:00"

@H_502_0@elseif(timeInt/60 >= 60)then

@H_502_0@return string.format("%.2d:%.2d:%.2d",timeInt/3600,(timeInt/60)%60,timeInt%60)

@H_502_0@elseif(timeInt >= 60)then

@H_502_0@return string.format("00:%.2d:%.2d",timeInt%60)

@H_502_0@else

@H_502_0@return string.format("00:00:%.2d",timeInt%60)

@H_502_0@end

@H_502_0@end

@H_502_0@-- 将一个时间数转换成"00:00:00"格式

@H_502_0@function getTimeString(timeInt)

@H_502_0@if(tonumber(timeInt) <= 0)then

@H_502_0@return "00:00:00"

@H_502_0@else

@H_502_0@return string.format("%02d:%02d:%02d",math.floor(timeInt/(60*60)),math.floor((timeInt/60)%60),timeInt%60)

@H_502_0@end

@H_502_0@end

@H_502_0@-- 将一个时间数转换成"00时00分00秒"格式

@H_502_0@function getTimeStringFont(timeInt)

@H_502_0@if(tonumber(timeInt) <= 0)then

@H_502_0@return "00时00分00秒"

@H_502_0@else

@H_502_0@return string.format("%02d时%02d分%02d秒",timeInt%60)

@H_502_0@end

@H_502_0@end

@H_502_0@-- nGenTime: 产生时间戳(也可以是一个未来的时间,比如CD时间戳)

@H_502_0@-- nDuration: 固定的有效期间,单位秒,计算某个未来时间的剩余时间时不需要指定

@H_502_0@-- 返回3个结果,第一个是剩余到期时间的字符串,"HH:MM:SS",不足2位自动补零;第二个是bool,标识nGenTime是否到期;第三个是剩余秒数

@H_502_0@function expireTimeString( nGenTime,nDuration )

@H_502_0@ local nNow = BTUtil:getSvrTimeInterval()

@H_502_0@ --CCLuaLog("nGenTime = " .. nGenTime .. " nNow = " .. nNow)

@H_502_0@ local nViewSec = (nDuration or 0) - (nNow - nGenTime)

@H_502_0@ return getTimeString(nViewSec),nViewSec <= 0,nViewSec

@H_502_0@end

@H_502_0@--得到一个时间戳timeInt与当前时间的相隔天数

@H_502_0@--offset是偏移量,例如凌晨4点:4*60*60

@H_502_0@--return type is integer,0--当天,n--不在同一天,相差n天

@H_502_0@function getDifferDay(timeInt,offset)

@H_502_0@timeInt = tonumber(timeInt or 0)

@H_502_0@offset = tonumber(offset or 0)

@H_502_0@ local curTime = tonumber(BTUtil:getSvrTimeInterval()) - offset

@H_502_0@ if(os.date("%j",curTime) == 1 and os.date("%j",timeInt - offset) ~= 1)then

@H_502_0@return os.date("%j",curTime) - (os.date("%j",timeInt - offset) - os.date("%j",curTime-24*60*60))

@H_502_0@ else--if(os.date("%j",curTime) ~= os.date("%j",timeInt - offset))then

@H_502_0@return os.date("%j",curTime) - os.date("%j",timeInt - offset)

@H_502_0@ end

@H_502_0@end

@H_502_0@-- 指定一个日期时间字符串,返回与之对应的东八区(服务器时区)时间戳

@H_502_0@-- sTime: 格式 "2013-07-02 20:00:00"

@H_502_0@function getIntervalByTimeString( sTime )

@H_502_0@local t = string.split(sTime," ")

@H_502_0@local tDate = string.split(t[1],"-")

@H_502_0@local tTime = string.split(t[2],":")

@H_502_0@local tt = os.time({year = tDate[1],month = tDate[2],day = tDate[3],hour = tTime[1],min = tTime[2],sec = tTime[3]})

@H_502_0@local ut = os.date("!*t",tt)

@H_502_0@local east8 = os.time(ut) + 8*60*60 -- UTC时间+8小时转为东八区北京时间

@H_502_0@return east8

@H_502_0@end

@H_502_0@--给一个时间如:153000,得到今天15:30:00的时间戳

@H_502_0@function getIntervalByTime( time )

@H_502_0@local curTime = BTUtil:getSvrTimeInterval()

@H_502_0@local temp = os.date("*t",curTime)

@H_502_0@local h,m,s = string.match(time,"(%d%d)(%d%d)(%d%d)" )

@H_502_0@local timeString = temp.year .."-".. temp.month .."-".. temp.day .." ".. h ..":".. m ..":".. s

@H_502_0@ local timeInt = TimeUtil.getIntervalByTimeString(timeString)

@H_502_0@ return timeInt

@H_502_0@end

猜你在找的Cocos2d-x相关文章