源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1124 lines
31 KiB

--[[
功能: 用于操作时间的类(比如取当前服务器时间, 取当前客户端时间等)
作者: deadline
]]
TimeUtil = TimeUtil or {
eng_faster_time = 0
}
local TimeUtil = TimeUtil
--[[
功能: 引擎从起动到现在的时间(每帧更新一次),单位(s)
其它:
作者: deadline
]]
function TimeUtil:getClientTime( )
return Status.NowUnscaledTime
end
--[[
功能: 获取当前服务器时间(单位:s)
参数:
其它:
作者: deadline
]]
function TimeUtil:getServerTime( )
return math.floor((Status.NowUnscaledTime*1000 + self.eng_faster_time)/1000 + 0.5)
end
--[[
功能: 引擎从起动到现在的时间(每帧更新一次),单位(ms)
其它:
作者: deadline
]]
function TimeUtil:getClientTimeMs( )
return Status.NowUnscaledTime*1000
end
--[[
功能: 获取当前服务器时间(单位:ms)
参数:
其它:
作者: deadline
]]
function TimeUtil:getServerTimeMs( )
return Status.NowUnscaledTime*1000 + self.eng_faster_time
end
--[[
功能: 提示客户端当前服务器的时间戳
参数:
server_time 服务器时间(ms) int
其它:
作者: deadline
]]
function TimeUtil:notifyServerTime(server_time)
self.eng_faster_time = server_time - Status.NowUnscaledTime*1000
end
function TimeUtil:convertTime(times)
local timeStr = ""
local second = math.modf(times % 60)
local minute = math.modf(times/60)
if minute >= 60 then
times = math.modf(minute / 60);
timeStr=timeStr..((times > 9 and times) or "0"..times)
timeStr=timeStr..(":")
minute= math.modf(minute % 60)
timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
timeStr=timeStr..(":")
timeStr=timeStr..((second > 9 and second) or "0"..second)
else
timeStr=timeStr..("00:")
timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
timeStr=timeStr..(":")
timeStr=timeStr..((second > 9 and second) or "0"..second)
end
return timeStr
end
--[[
功能:分钟和秒钟的显示,不显示出小时数
作者:zhongdong
]]--
function TimeUtil:convertTimeWithoutHour(times)
local timeStr = ""
local second = math.modf(times % 60)
local minute = math.modf(times/60)
if minute >= 60 then
times = math.modf(minute / 60);
timeStr=timeStr..((times > 9 and times) or "0"..times)
timeStr=timeStr..(":")
minute= math.modf(minute % 60)
timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
timeStr=timeStr..(":")
timeStr=timeStr..((second > 9 and second) or "0"..second)
else
-- timeStr=timeStr..("00:")
timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
timeStr=timeStr..(":")
timeStr=timeStr..((second > 9 and second) or "0"..second)
end
return timeStr
end
-- 获得当前时间 m:秒
function TimeUtil:timeSec(times)
local time = os.date("%X", times)
local datearr = Language.split(time,":")
local result=datearr[1]*3600+datearr[2]*60+datearr[3]
return result
end
--解析"21:30"这样的字符串,返回{hour:21, min:30}
function TimeUtil:ParseHourMinute(time_str)
-- local p = string.find(time_str, ":")
local ss = Language.split(time_str, ":")
local ret = {}
ret.hour = tonumber(ss[1])
ret.min = tonumber(ss[2])
return ret
end
--[[
* 秒转换时间
* @param timeStr 秒数
* @return 格式化时间
]]
function TimeUtil:timeConversion(timeStr, stype)
if timeStr == nil then
return ""
end
timeStr = tonumber(timeStr)
if timeStr < 0 then timeStr = 0 end
local cur_type = stype or "date"
local date = os.date("*t", timeStr)
local monthStr = date.month < 10 and "0"..date.month or date.month
local dateStr = date.day < 10 and "0"..date.day or date.day
local hoursStr = date.hour < 10 and "0"..date.hour or date.hour
local minutesStr = date.min < 10 and "0"..date.min or date.min
local secondStr = date.sec < 10 and "0"..date.sec or date.sec
if cur_type == "date" then
return date.year .. "-" .. monthStr .. "-" .. dateStr
elseif cur_type =="mm-dd" then
return monthStr .. "-" .. dateStr
elseif cur_type == "mm-dd hh-MM" then
return monthStr .. "-" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
elseif cur_type == "yyyy-mm-dd hh-MM" then
return date.year .. "-" .. monthStr .. "-" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
elseif cur_type == "yyyy年mm月dd日 hh-MM" then
return date.year .. "" .. monthStr .. "" .. dateStr .. "" .. hoursStr .. ":" .. minutesStr
elseif cur_type == "yyyy年mm月dd日 hh" then
return date.year .. "" .. monthStr .. "" .. dateStr .. "" .. hoursStr..""
elseif cur_type == "yyyy-mm-dd" then
return date.year.."-"..monthStr.."-"..dateStr
elseif cur_type =="yyyy-mm-dd hh:mm:ss" then
return date.year .. "-" .. monthStr .. "-" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr
elseif cur_type == "yyyy年mm月dd日" then
return date.year .. "" .. monthStr .. "" .. dateStr .. ""
elseif cur_type == "yyyy年mm月dd日 hh:MM:ss" then
return date.year .. "" .. monthStr .. "" .. dateStr .. "" .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr
elseif cur_type == "hh-MM" then
return hoursStr .. ":" .. minutesStr
elseif cur_type == "MM-ss" then
return minutesStr .. ":" .. secondStr
elseif cur_type == "hh-MM-ss" then
return hoursStr..":"..minutesStr .. ":" .. secondStr
elseif cur_type =="mm-dd hh:mm" then
return monthStr.."-"..dateStr.." "..hoursStr..":"..minutesStr
elseif cur_type =="mm月dd日 hh:mm" then
return monthStr..""..dateStr..""..hoursStr..":"..minutesStr
elseif cur_type =="mm/dd hh:mm" then
return monthStr.."/"..dateStr.." "..hoursStr..":"..minutesStr
elseif cur_type =="yy/mm/dd hh:mm" then
return date.year .. "/" .. monthStr .. "/" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
elseif cur_type =="yy/mm/dd" then
return date.year .. "/" .. monthStr .. "/" .. dateStr
elseif cur_type =="yy/mm/dd hh:mm:ss" then
return date.year .. "/" .. monthStr .. "/" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr
elseif cur_type =="yymmddhhmmss" then
return date.year ..monthStr .. dateStr .. hoursStr .. minutesStr .. secondStr
elseif cur_type =="mm/dd" then
return monthStr .. "/" .. dateStr
elseif cur_type =="mm月dd日" then
return monthStr..""..dateStr..""
elseif cur_type == "mm.dd" then
return monthStr.."."..dateStr
elseif cur_type == "mm-dd hh:MM:ss" then
return monthStr .. "-" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr
elseif cur_type =="hh:mm:ss" then
return hoursStr .. ":" .. minutesStr .. ":" .. secondStr
elseif cur_type =="exfont_hh:mm:ss" then--主题活动副本双倍活动“00时00分00秒”艺术字
return hoursStr .. "h" .. minutesStr .. "m" .. secondStr.."s"
elseif cur_type =="hh:mm" then
return hoursStr .. ":" .. minutesStr
elseif cur_type =="act_hh:mm" then
return hoursStr .. "h" .. minutesStr.."m"
elseif cur_type =="yyyy.mm.dd hh:mm" then
return date.year .. "." .. monthStr .. "." .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
elseif cur_type =="mm.dd hh:mm" then
return monthStr .. "." .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
elseif cur_type == "yyyy.mm.dd" then
return date.year .. "." .. monthStr .. "." .. dateStr
else
return monthStr.."//"..date.day.." "..hoursStr..":"..minutesStr
end
end
--[[
* 秒转换时间
* @param timeStr 精确到毫秒级的时间
* @return 格式化时间
]]
function TimeUtil:timeConversionWithMillisecond(timeStr, stype)
if timeStr == nil then
return ""
end
timeStr = tonumber(timeStr)
if timeStr < 0 then timeStr = 0 end
local cur_type = stype or "date"
local timeStr, milli = math.modf(timeStr / 1000)
local date = os.date("*t", timeStr)
local monthStr = date.month < 10 and "0"..date.month or date.month
local dateStr = date.day < 10 and "0"..date.day or date.day
local hoursStr = date.hour < 10 and "0"..date.hour or date.hour
local minutesStr = date.min < 10 and "0"..date.min or date.min
local secondStr = date.sec < 10 and "0"..date.sec or date.sec
local str = timeStr % 86400000
local len = string.len(str)
local millisecond = string.sub(str, len - 2, len)
if cur_type =="yy/mm/dd hh:mm:ss.ms" then
return date.year .. "/" .. monthStr .. "/" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr .. "." .. millisecond
elseif "hh:mm:ss.ms" then
return hoursStr .. ":" .. minutesStr .. ":" .. secondStr .. "." .. millisecond
else
return monthStr.."//"..date.day.." "..hoursStr..":"..minutesStr
end
end
--[[
* second 秒
* formatStr 格式化字符串,hh替换为小时, mm替换为分钟, ss替换为秒
* addZero 是否补0
]]
function TimeUtil:timeConvert(second, dateType, addZero)
local function appendZero(time)
return string.format("%02d", time)
end
dateType = dateType or "hh小时mm分ss秒"
if addZero==nil then
addZero=true
else
addZero = addZero
end
local h = math.floor(second / 3600)
local m = math.floor((second % 3600) / 60)
local s = second % 60;
if addZero == true then
h = appendZero(h)
m = appendZero(m)
s = appendZero(s)
end
local str = string.gsub(dateType, "hh", h)
str = string.gsub(str, "mm", m)
str = string.gsub(str, "ss", s)
return str
end
--[[
* second 秒
* formatStr 格式化字符串, mm替换为分钟, ss替换为秒
* addZero 是否补0
]]
function TimeUtil:timeConvert2(second, dateType, addZero)
local function appendZero(time)
return string.format("%02d", time)
end
dateType = dateType or "mm分ss秒"
if addZero==nil then
addZero=true
else
addZero = addZero
end
local m = math.floor((second % 3600) / 60)
local s = second % 60;
if addZero == true then
m = appendZero(m)
s = appendZero(s)
end
local str = ""
if tonumber(m) > 0 then
str = string.gsub(dateType, "mm", m)
else
str = "ss秒"
end
str = string.gsub(str, "ss", s)
return str
end
--[[
@param:second
@retuen *天*时*分
]]
function TimeUtil:qianggouTimeLeft2(second, hide_second)
local s=second%60
local m = math.floor(second/60);
local min = m % 60
if hide_second then
min = 0
end
local str = min == 0 and "" or min .. "";
local hour
local d
if m >= 60 then
local h = math.floor(m/60);
hour = h % 24
str = (hour == 0 and "" or hour .. "") .. str;
if h >= 24 then
d = math.floor(h/24);
str = d .. "" .. str;
end
elseif m > 0 and m < 60 then
str = m .. "" .. s .. ""
else
str = s .. ""
end
return str,(d or 0),(hour or 0),(min or 0),(s or 0)
end
--[[
@param:second
@retuen *时*分
]]
function TimeUtil:timeConvert6(second, show_second, not_show_day)
if second == nil then return "" end
local s = second % 60
local m = math.floor(second / 60);
local min = m % 60
local str = min == 0 and "" or min .. ""
local hour
local d
if m >= 60 then
local h = math.floor(m / 60);
hour = h % 24
str = hour == 0 and "" or hour .. "" .. str
if show_second then
str = str .. s .. ""
end
if h >= 24 and not not_show_day then
d = math.floor(h/24);
str = d .. "" .. str;
end
elseif m > 0 and m < 60 then
str = m .. ""
if show_second then
str = str .. s .. ""
end
else
str = str .. s .. ""
end
if second <= 0 then
str = 0
end
return str
end
--[[
@param:second
@retuen *时*分(带颜色)
]]
function TimeUtil:timeConvertColor6(second, show_second, not_show_day, color, mask_empty)
if second == nil then return "" end
local s = second % 60
local m = math.floor(second / 60);
local min = m % 60
local str = min == 0 and "" or " <color="..color..">"..min.."</color> 分"
local hour
local d
if m >= 60 then
local h = math.floor(m / 60);
hour = h % 24
str = hour == 0 and "" or " <color="..color..">"..hour.."</color> 时" .. str
if show_second then
str = str .. " <color="..color..">"..s.."</color> 秒"
end
if h >= 24 and not not_show_day then
d = math.floor(h/24);
str = " <color="..color..">"..d.."</color> 天" .. str;
end
elseif m > 0 and m < 60 then
str = " <color="..color..">"..min.."</color> 分"
if show_second then
str = str .. " <color="..color..">"..s.."</color> 秒"
end
else
str = str .. " <color="..color..">"..s.."</color> 秒"
end
if second <= 0 then
str = 0
end
if mask_empty then
str = string.gsub( str, " ", "" )
end
return str
end
--[[
@param:second
@retuen *天*时
]]
function TimeUtil:timeConvert7(second)
if second == nil then return "" end
local s = second % 60
local m = math.floor(second / 60);
local min = m % 60
local str = min == 0 and "" or min .. ""
local hour
local d
if m >= 60 then
local h = math.floor(m / 60);
hour = h % 24
str = hour == 0 and "" or hour .. "小时" .. str
if h >= 24 then
d = math.floor(h/24);
str = d .. "" .. str;
end
elseif m > 0 and m < 60 then
str = m .. ""
end
if second <= 0 then
str = 0
end
return str
end
--[[
@param:second
@有天数则显示*天00:00,没有则显示00:00:00
@retuen *天00:00:00
@day_show_second显示*天00:00:00
]]
function TimeUtil:timeConvert3(second,day_show_second)
local s=second%60
local min = math.floor(second/60)%60
local hour = math.floor(second/3600)%24
local day = math.floor(second/86400)
local str = ""
if day > 0 then--显示*天00:00
if not day_show_second then
str = string.format("%s天%02d:%02d",day,hour,min)
else
str = string.format("%s天%02d:%02d:%02d",day,hour,min,s)
end
else--显示00:00:00
str = string.format("%02d:%02d:%02d",hour,min,s)
end
return str
end
--[[
@param:second
@有天数则显示*天00:00:00,没有则显示00:00:00
@retuen *天00:00:00
]]
function TimeUtil:timeConvert4(second)
local s=second%60
local s_str = s == 0 and ":00" or string.format(":%02d",s);
local m = math.floor(second/60);
local min = m % 60
local str = min == 0 and ":00" or string.format(":%02d",min);
local hour
local d
if m >= 60 then
local h = math.floor(m/60);
hour = h % 24
str = (hour == 0 and ":00" or string.format("%02d",hour))..str;
if h >= 24 then
d = math.floor(h/24);
str = d .. "" .. str..s_str;
if d >= 7 then
str = "超时"
end
else
str = str..s_str
end
else
str = "00"..str..s_str
end
return str
end
--[[
@des 将时间字符串转换为时间戳(用到的格式可以自己加上)
@author
]]
function TimeUtil:ConvertFmStringToTime(str,ipattern)
--local pattern1 = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)([%+%-])(%d+)%:(%d+)"
local pattern = {}
pattern[1] = "(%d+)/(%d+)/(%d+)%s+(%d+):(%d+)" --对应2013/6/8 23:59
pattern[2] = "(%d+)@(%d+)@(%d+)@(%d+)@(%d+)" --对应2013@6@8@23@59
local xyear, xmonth, xday, xhour, xminute,xsec = string.match(str,pattern[ipattern])
local convertedTimestamp = os.time({year = xyear, month = xmonth,
day = xday, hour = xhour, min = xminute,sec = xsec})
return convertedTimestamp;
end
--[[
@describtion:获得什么时候前
@param:服务器传来的时间戳time
@return:什么前
@author:hjp
]]
function TimeUtil:GetTimeStrByServerTime( time )
local dec_time = self:getServerTime() - time
if dec_time <= 0 then
return "刚刚"
end
local tem = 0
local month = math.floor(dec_time / (3600*24*30))
tem = dec_time % (3600*24*30)
local day = math.floor(tem / (3600 * 24))
tem = tem % (3600*24)
local h = math.floor(tem / 3600)
tem = tem % 3600
local m = math.floor(tem / 60)
tem = tem % 60
local s = tem
if month ~= 0 then
return month .. "月前"
elseif day ~= 0 then
return day .. "天前"
elseif h ~= 0 then
return h .. "小时前"
elseif m ~= 0 then
return m .. "分钟前"
elseif s ~= 0 then
return s .. "秒前"
else
return ""
end
end
--[[
@describtion:获得什么时候前
@param:服务器传来的时间戳time
@return:什么前
@author:hjp
]]
function TimeUtil:GetTimeAgoStr( dec_time )
if dec_time <= 0 then
return "刚刚"
end
local tem = 0
local month = math.floor(dec_time / (3600*24*30))
tem = dec_time % (3600*24*30)
local day = math.floor(tem / (3600 * 24))
tem = tem % (3600*24)
local h = math.floor(tem / 3600)
tem = tem % 3600
local m = math.floor(tem / 60)
tem = tem % 60
local s = tem
if month ~= 0 then
return month .. "月前"
elseif day ~= 0 then
return day .. "天前"
elseif h ~= 0 then
return h .. "小时前"
elseif m ~= 0 then
return m .. "分钟前"
elseif s ~= 0 then
return s .. "秒前"
else
return ""
end
end
--获取今日三点的时间戳
function TimeUtil:GetToDayThreeHourStr( ... )
local now=self:getServerTime()
local date = os.date("*t", now)
local date_three={year=date.year,month=date.month,day=date.day,hour=3,min=0,sec=0}
return os.time(date_three)
end
--N(0-24)--获取今日N点的时间戳
function TimeUtil:GetToDayNHourStr( hour_num )
local now=self:getServerTime()
local date = os.date("*t", now)
local date_n = {year=date.year,month=date.month,day=date.day,hour=hour_num,min=0,sec=0}
return os.time(date_n)
end
--N(0-24)--获取当前的小时数
function TimeUtil:GetHourNow( )
local now=self:getServerTime()
local date = os.date("*t", now)
return date.hour
end
--转化时间,得到天或是分钟
function TimeUtil:timeConvert5(second)
local s = second % 60
local m = math.floor(second / 60);
local min = m % 60
local str = ""
local hour
local d
if m >= 60 then
local h = math.floor(m/60);
hour = h % 24
str = hour == 0 and "" or hour .. "小时"
if h >= 24 then
d = math.floor(h/24);
str = d .. ""
end
else
-- str = "小于1小时"
str = m .. "分钟"
end
return str
end
-- --转化时间,得到(XX天XX小时 或XX小时XX分 或XX分)
function TimeUtil:timeConvert7(second)
local s = second % 60
local m = math.floor(second / 60);
local min = m % 60
local str = ""
local hour
local d
if m >= 60 then
local h = math.floor(m/60);
hour = h % 24
str = hour == 0 and "" or hour .. "小时" .. min .. ""
if h >= 24 then
d = math.floor(h/24);
str = d .. "" .. hour.. "小时"
end
else
str = min .. ""
if min == 0 then
str = "1分"
end
end
return str
end
function TimeUtil:timeConvert12(time)
local dateStr = math.floor(time / 60 / 60 / 24)
local hoursStr = math.floor(time / 60 / 60) % 24
local minutesStr = math.floor(time / 60) % 60
local secondStr = time % 60
if dateStr > 0 then
return string.format("%s天%s时", dateStr, hoursStr)
else
if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
return hoursStr ..":" .. minutesStr .. ":" ..secondStr
end
end
function TimeUtil:timeConvert13(time)
local dateStr = math.floor(time / 60 / 60 / 24)
local hoursStr = math.floor(time / 60 / 60) % 24
local minutesStr = math.floor(time / 60) % 60
local secondStr = time % 60
if dateStr > 0 then
return string.format("%s天%s时%s分", dateStr, hoursStr,minutesStr)
else
if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
return hoursStr ..":" .. minutesStr .. ":" ..secondStr
end
end
function TimeUtil:timeConvert14(time)
local dateStr = math.floor(time / 60 / 60 / 24)
local hoursStr = math.floor(time / 60 / 60) % 24
local minutesStr = math.floor(time / 60) % 60
local secondStr = time % 60
if dateStr > 0 then
return string.format("%s天%s时", dateStr, hoursStr)
elseif hoursStr > 0 then
return string.format("%s时%s分", hoursStr,minutesStr)
else
-- if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
return minutesStr .. ":" ..secondStr
-- return hoursStr ..":" .. minutesStr .. ":" ..secondStr
end
end
function TimeUtil:timeConvert15(time)
local dateStr = math.floor(time / 60 / 60 / 24)
local hoursStr = math.floor(time / 60 / 60) % 24
local minutesStr = math.floor(time / 60) % 60
local secondStr = time % 60
local str = ""
if dateStr > 0 then
str = dateStr .. ""
str = hoursStr > 0 and str .. hoursStr .. "" or str
str = minutesStr > 0 and str .. minutesStr .. "" or str
else
str = hoursStr > 0 and str .. hoursStr .. "" or str
str = minutesStr > 0 and str .. minutesStr .. "" or str
str = secondStr > 0 and str .. secondStr .. "" or str
end
return str
end
function TimeUtil:timeConvert16(time)
local dateStr = math.floor(time / 60 / 60 / 24)
local hoursStr = math.floor(time / 60 / 60) % 24
local minutesStr = math.floor(time / 60) % 60
local secondStr = time % 60
if dateStr > 0 then
return string.format("%s天%s时%s分", dateStr, hoursStr,minutesStr)
else
-- if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
-- if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
-- if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
return hoursStr .."" .. minutesStr .. "" ..secondStr..""
end
end
function TimeUtil:timeConvert17(time, color)
local dateStr = math.floor(time / 60 / 60 / 24)
local hoursStr = math.floor(time / 60 / 60) % 24
local minutesStr = math.floor(time / 60) % 60
local secondStr = time % 60
local str = ""
if color then
if dateStr > 0 then
str = HtmlColorTxt(dateStr , color) .. ""
str = hoursStr > 0 and str .. HtmlColorTxt(hoursStr , color) .. "" or str
elseif hoursStr > 0 then
str = hoursStr > 0 and str .. HtmlColorTxt(hoursStr , color) .. "" or str
str = minutesStr > 0 and str .. HtmlColorTxt(minutesStr , color) .. "" or str
else
str = minutesStr > 0 and str .. HtmlColorTxt(minutesStr , color) .. "" or str
str = secondStr > 0 and str .. HtmlColorTxt(secondStr , color) .. "" or str
end
else
if dateStr > 0 then
str = dateStr .. ""
str = hoursStr > 0 and str .. hoursStr .. "" or str
str = minutesStr > 0 and str .. minutesStr .. "" or str
elseif hoursStr > 0 then
str = hoursStr > 0 and str .. hoursStr .. "" or str
str = minutesStr > 0 and str .. minutesStr .. "" or str
else
str = minutesStr > 0 and str .. minutesStr .. "" or str
str = secondStr > 0 and str .. secondStr .. "" or str
end
end
return str
end
-- 目标<=现在->过去 false
function TimeUtil:ConfigTimeCompare(timeStr, now_time)
local dec_time = now_time or self:getServerTime()
dec_time = self:timeConversion(dec_time,"yymmddhhmmss")
timeStr = string.gsub(timeStr,"%-","")
timeStr = string.gsub(timeStr,"%:","")
-- timeStr = Trim(timeStr)
return tonumber(timeStr) > tonumber(dec_time)
end
function TimeUtil:GetWeekStrNum(week)
local week_list = {"","","","","","",""}
return week_list[week]
end
--获取未来某一天0点时间戳
function TimeUtil:GetFutureZeroStamp(future_days)
local cur_timestamp = os.time()
local one_hour_timestamp = 24*60*60
local temp_time = cur_timestamp + one_hour_timestamp * future_days
local temp_date = os.date("*t", temp_time)
temp_date.hour = 0
temp_date.min = 0
temp_date.sec = 0
return os.time(temp_date)
end
--获取未来某一天0点时间戳(使用服务器时间戳去计算)
function TimeUtil:GetFutureZeroStampServer(future_days)
local cur_timestamp =self:getServerTime()
local one_hour_timestamp = 24*60*60
local temp_time = cur_timestamp + one_hour_timestamp * future_days
local temp_date = os.date("*t", temp_time)
temp_date.hour = 0
temp_date.min = 0
temp_date.sec = 0
return os.time(temp_date)
end
--获取未来某一天4点时间戳(使用服务器时间戳去计算)
function TimeUtil:GetFutureFourStampServer(future_days)
local cur_timestamp =self:getServerTime()
local one_hour_timestamp = 24*60*60
local temp_time = cur_timestamp + one_hour_timestamp * future_days
local temp_date = os.date("*t", temp_time)
temp_date.hour = 4
temp_date.min = 0
temp_date.sec = 0
return os.time(temp_date)
end
--[[
@param:second
@return 00:00
]]
function TimeUtil:timeConvert8(second)
local s=second%60
local s_str = s == 0 and ":00" or string.format(":%02d",s);
local m = math.floor(second/60);
local min = m % 60
local str = min == 0 and "00" or string.format("%02d",min);
str = str..s_str
return str
end
--[[
@param:second
@retuen *时*分
]]
function TimeUtil:timeConvert9(second)
if second == nil then return "" end
local s = second % 60
local m = math.floor(second / 60);
local min = m % 60
local str = min == 0 and "" or min .. ""
local hour
local d
if m >= 60 then
local h = math.floor(m / 60);
hour = h % 24
str = hour == 0 and "" or hour .. "小时" .. str
if h >= 24 then
d = math.floor(h/24);
str = d .. ""
end
elseif m > 0 and m < 60 then
if s > 0 then
str = m .. "" .. s .. ""
else
str = m .. ""
end
else
if s > 0 then
str = str .. s .. ""
end
end
if second <= 0 then
str = 0
end
return str
end
--[[
@param:second
@retuen *时*分
]]
function TimeUtil:timeConvert10(second, show_second)
if second == nil then return "" end
local s = second % 60
local m = math.floor(second / 60);
local min = m % 60
local str = min == 0 and "" or min .. ""
local hour
local d
if m >= 60 then
local h = math.floor(m / 60);
hour = h % 24
str = hour == 0 and "" or hour .. "" .. str
if show_second then
str = str .. s .. ""
end
elseif m > 0 and m < 60 then
str = m .. ""
if show_second then
str = str .. s .. ""
end
else
if show_second then
str = str .. s .. ""
else
str = "0" .. ""
end
end
if second <= 0 then
str = 0
end
return str
end
--[[
@param:second
@retuen 00时00分
]]
function TimeUtil:timeConvert11(second, show_second)
if second == nil then return "" end
local s = second % 60
local m = math.floor(second / 60);
local min = m % 60
local str = min == 0 and "" or string.format("%02d",min) .. ""
local hour
local d
local h = math.floor(m / 60);
if m >= 60 then
hour = h % 24
str = hour == 0 and "" or string.format("%02d",h) .. "" .. string.format("%02d",min)..""
if show_second then
str = str .. s .. ""
end
elseif m > 0 and m < 60 then
str = string.format("%02d",h) .. "" .. string.format("%02d",m)..""
if show_second then
str = str .. s .. ""
end
else
if show_second then
str = str .. s .. ""
else
str = "00时01分"
end
end
if second <= 0 then
str = "00时00分"
end
return str
end
--[[
@param:second
@retuen 00天00时00分00秒
]]
function TimeUtil:qianggouTimeLeft3(second, hide_second)
local s=second%60
local m = math.floor(second/60);
local min = m % 60
if hide_second then
min = 0
end
local str = min == 0 and "" or string.format("%02d",min) .. ""..string.format("%02d",s) .. "";
local hour
local d
if m >= 60 then
local h = math.floor(m/60);
hour = h % 24
str = (hour == 0 and "" or string.format("%02d",hour) .. "") .. str;
if h >= 24 then
d = math.floor(h/24);
str = string.format("%02d",d) .. "" .. str;
end
elseif m > 0 and m < 60 then
str = "00天00时"..string.format("%02d",m) .. "" .. string.format("%02d",s) .. ""
else
str = "00天00时00分"..string.format("%02d",s) .. ""
end
return str,(d or 0),(hour or 0),(min or 0),(s or 0)
end
--根据开服第几天获取周几
function TimeUtil:GetWeekStrByOpenDay( day )
day = tonumber(day)
local openDay = ServerTimeModel:getInstance():GetOpenServerDay()
local curTime = TimeUtil:getServerTime()
local targetTime = curTime+(day-openDay)*24*60*60
local week = os.date("%w", targetTime)
return week
end
--根据合服第几天获取周几
function TimeUtil:GetWeekStrByMergeDay( day )
day = tonumber(day)
local openDay = ServerTimeModel:getInstance():GetMergeServerDay()
local curTime = TimeUtil:getServerTime()
local targetTime = curTime+(day-openDay)*24*60*60
local week = os.date("%w", targetTime)
return week
end
--今天是周几
function TimeUtil:GetCurrentWeekDay()
local curTime = TimeUtil:getServerTime()
local targetTime = curTime
local week = os.date("%w", targetTime)
return week
end
--获取本地的时区偏移,缓存一下,减少os.date的内存分配
function TimeUtil:GetLocalOffZone( )
if not self.local_off_zone then
local now_time = os.time()
local now_time_table = os.date("*t", now_time)
local local_zone = os.difftime(now_time, os.time(os.date("!*t", now_time)))/3600
local off_dst = now_time_table.isdst and 1 or 0 --夏令时
self.local_off_zone = self:GetSereverZone() - (local_zone + off_dst)
end
return self.local_off_zone
end
--获取服务器时间转化为本地时区的时间
function TimeUtil:GetZoneTime( )
local off_zone = self:GetLocalOffZone()
local cur_date = self:getServerTime() + off_zone*3600
-- print("tanar - [ TimeUtil ] [518] ==> local_zone: ",local_zone, off_zone, now_time_table.isdst)
return cur_date
end
--获取服务器所在时区,国内版本东八区,其他语言版本再改
function TimeUtil:GetSereverZone( )
return 8
end
--转换毫秒倒计时(00:00.00)
function TimeUtil:convertMillisecondTime( times,no_show_dot_num )
local timeStr = ""
local second = math.modf(times % 60)
local minute = math.modf(times/60)
timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
timeStr=timeStr..(":")
timeStr=timeStr..((second > 9 and second) or "0"..second)
if not no_show_dot_num then
local int_times = math.floor(times)
local dot_num = times - int_times
if dot_num > 0.1 then
timeStr = timeStr..(".")..math.floor(dot_num*100)
elseif dot_num > 0 and dot_num < 0.01 then
timeStr = timeStr..(".0")..math.floor(dot_num*100)
else
timeStr = timeStr..(".00")
end
end
return timeStr
end
--[[
@param:second
@retuen *时*分
]]
function TimeUtil:timeConvertAct(second)
local s=second%60
local m = math.floor(second/60);
local min = m % 60
if hide_second then
min = 0
end
local str = string.format("%02d",min) .. "m"..string.format("%02d",s) .. "s";
local hour
local d
if m >= 60 then
local h = math.floor(m/60);
str = string.format("%02d",h) .. "h" .. str;
elseif m > 0 and m < 60 then
str = "00h"..string.format("%02d",m) .. "m" .. string.format("%02d",s) .. "s"
else
str = "00h00m"..string.format("%02d",s) .. "s"
end
return str
end
--N(0-24)--获取今日N点的时间戳
function TimeUtil:IsToday( time )
local now=self:getServerTime()
local now_date = os.date("*t", now)
local date = os.date("*t", time)
return now_date.year == date.year and now_date.month == date.month and now_date.day == date.day
end
--[[
@param:second
@显示00:00:00
@retuen 00:00:00
]]
function TimeUtil:timeConvert33(second)
local s=second%60
local min = math.floor(second/60)%60
local hour = math.floor(second/3600)
local str = string.format("%02d:%02d:%02d",hour,min,s)
return str
end
--获取当前的时,分,秒
function TimeUtil:getCurHourMinSce()
local time = self:getServerTime( )
local date = os.date("*t", time)
return date.hour,date.min,date.s
end