cocos2dx-lua 一些公共方法

function getStringTimeForInt(timeInt)

if(tonumber(timeInt) <= 0)then

return "00:00:00"

elseif(timeInt/60 >= 60)then

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

elseif(timeInt >= 60)then

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

else

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

end

end

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

function getTimeString(timeInt)

if(tonumber(timeInt) <= 0)then

return "00:00:00"

else

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

end

end

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

function getTimeStringFont(timeInt)

if(tonumber(timeInt) <= 0)then

return "00时00分00秒"

else

return string.format("%02d时%02d分%02d秒", math.floor(timeInt/(60*60)), math.floor((timeInt/60)%60), timeInt%60)

end

end

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

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

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

function expireTimeString( nGenTime, nDuration )

    local nNow = BTUtil:getSvrTimeInterval()

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

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

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

end

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

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

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

function getDifferDay(timeInt, offset)

timeInt = tonumber(timeInt or 0)

offset = tonumber(offset or 0)

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

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

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

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

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

    end

end

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

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

function getIntervalByTimeString( sTime )

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

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

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

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

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

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

return east8

end

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

function getIntervalByTime( time )

local curTime = BTUtil:getSvrTimeInterval()

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

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

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

    local timeInt = TimeUtil.getIntervalByTimeString(timeString)

    return timeInt

end

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值