local c = require "util.core" local os_time = os.time local os_date = os.date local math_floor = math.floor local logger = require "logger" local util = {} local oneday = 24*3600 ---------------------------- Maths ---------------------------- function util.max(a, b) return a > b and a or b end function util.min(a, b) return a < b and a or b end function util.clamp(a, low, up) assert(up >= low) if a < low then return low -- min elseif a > up then return up -- max else return a -- original value end end ---------------------------- Time ---------------------------- function util.timezone() return c.timezone() end function util.today(ti, sec) local tz = c.timezone() * 3600 return ((ti or os_time()) + tz) // 86400 * 86400 - tz + (sec or 0) end function util.gettime() return c.gettime() end function util.gettimeofday() return c.gettimeofday() end function util.nextday(ti) return util.today(ti) + 86400 end function util.week(ti) local w = tonumber(os_date("%w", ti)) if w == 0 then w = 7 end -- 星期转换结果为 0 return w end function util.hours(ti) return tonumber(os_date("%H", ti or os_time())) end function util.day(ti) return tonumber(os_date("%d", ti or os_time())) end function util.alarm(ti) -- 整点时间 -- 获取下次刷新排行榜时间, 活动刷新时间 local temp_tm = os.date("*t", ti or os_time()) temp_tm.min = 0 temp_tm.sec = 0 return os_time(temp_tm) -- 刷新战斗力时间 end -- time travel --[[ 获取以 hour点 minute 作为跨天点的 时间间隔天数 start_time : 开始的时间点 end_time : 结束的时间点 hour : 跨天的小时点 minute : 跨天的分钟点 not_start : true start_time 那一天的第一个跨点不计算 (例如:4点跨天 如果start_time 是0~4点,那么0~下一天4点是第一天) : false start_time 那一天的第一个跨点计算 (例如:4点跨天 如果start_time 是0~4点,那么4~下一天4点是第二天) ]] local function get_time_clock(give_time,hour,minute) -- 格式化时间为当天hour:minute -- local now_tm = os_date("*t", give_time) -- now_tm.hour = hour -- now_tm.min = minute -- now_tm.sec = 0 local sec = hour * 3600 + minute * 60 return util.today(give_time, sec) end function util.get_time_travel(start_time,end_time,hour,minute,not_start) assert(start_time) assert(end_time) assert(start_time <= end_time) hour = hour or 0 minute = minute or 0 not_start = not_start or false local start_tc = get_time_clock(start_time,hour,minute) local end_tc = get_time_clock(end_time,hour,minute) local start_zero = start_tc - (hour * 3600 + minute * 60) local end_zero =start_tc - (hour * 3600 + minute * 60) local travel_days = 0 if start_time < start_tc then if not_start then -- 第一个跨点不计算 不用修正 else -- 第一个跨点计算 修正到上一天 start_tc = start_tc - oneday end end if (end_time < end_tc) and not (not_start and start_zero == end_zero) then end_tc = end_tc - oneday end return math_floor((end_tc - start_tc)/86400 + 1),end_tc -- local start_tc = get_time_clock(start_time,hour,minute) -- local end_tc = get_time_clock(end_time,hour,minute) -- return math_floor((end_time - start_tc)/oneday) + 1,end_tc end ---------------------------- Random ---------------------------- local dice = {} local mt = { __index = dice } function util.newrandom(...) local self = { __cobj = assert(c.newrandom(...)) } return setmetatable(self, mt) end function dice:roll(...) return c.rand(self.__cobj, ...) end function dice:srand(seed) c.srand(self.__cobj, seed) end function dice:peekseed() return c.peekseed(self.__cobj) end local holdrand = 1 function util.peekseed() return holdrand end function util.rand(...) local r, s = c.random(...) holdrand = s return r end function util.srand(seed) holdrand = seed c.srandom(holdrand) end ---------------------------- UUID ---------------------------- function util.uuid() return c.uuid() end ---------------------------- Hash ---------------------------- function util.hashcode(str) return c.hashcode(str) end ---------------------------- Table ---------------------------- function util.length(t) local n = 0 for _, _ in pairs(t) do n = n + 1 end return n end -- table 序列化 function util.tablequeue(obj) local temp = {} if not obj then return temp end for _, v in pairs(obj) do table.insert(temp, v) end return temp end -- Initialize random seed for function of the util.rand util.srand(os_time()) util.rand() -- discard first value to avoid undesirable correlations function util.optimize_array(a) if a and next(a) then return a end end -- TODO: 计算指定时间的周一 function util.week_monday(time) time = util.today(time) local w = util.week(time) -- if w == 0 then w = 7 end -- 星期转换结果为 0 local rettm = time - (w - 1)*86400 logger.trace(" 周一的时间节点 %s, 开服时间节点 %s", os.date("%Y-%m-%d %H:%M:%S",tonumber(rettm)), os.date("%Y-%m-%d %H:%M:%S",tonumber(time)) ) return rettm end -- TODO: 计算指定时间月份的 1号 function util.month_one(time) time = util.today(time) local d = util.day(time) local rettm = time - (d - 1)*86400 logger.trace(" 1 号的时间节点 %s, 开服时间节点 %s", os.date("%Y-%m-%d %H:%M:%S",tonumber(rettm)), os.date("%Y-%m-%d %H:%M:%S",tonumber(time)) ) return rettm end -- TODO: 计算开服后指定多少个月的 1号 function util.next_month_one(time, many) many = (many or 1) - 1 local one = util.month_one(time) local onetable = os.date("*t", one) local onetable_month = onetable.month + many local add_year = 0 local set_month = onetable_month if onetable_month > 12 then add_year = math.modf(onetable_month/12) set_month = math.fmod( onetable_month, 12 ) end onetable.year = onetable.year+add_year onetable.month = set_month local newtime = os.time(onetable) logger.trace(" ### 指定时间的1号 %s, 计算出来的1号 %s, many:%s", os.date("%Y-%m-%d %H:%M:%S",tonumber(one)), os.date("%Y-%m-%d %H:%M:%S",tonumber(newtime)),many) return newtime end function util.split(str) local chunks = {str:match("(%d+)%/(%d+)%/(%d+)%/(%d+)%/(%d+)%/(%d+)")} assert(#chunks == 6) local year, month, day, hour, min, sec = table.unpack(chunks) return os.time({ year=year, month=month, day=day, hour=hour, min=min, sec=sec }) end -- function util.goods_merge(list) -- list = list or {} -- local ok, value = next(list) -- if not ok then -- return list -- end -- local t = {} -- if type(value) == "table" then -- for _, v in ipairs(list) do -- local id = v.id -- local num = v.num -- t[id] = t[id] or { -- id = id, -- num = 0 -- } -- t[id].num = t[id].num + num -- end -- else -- for i=1, #list, 2 do -- local id = list[i] -- local num = list[i+1] -- if num and num > 0 then -- t[id] = t[id] or { -- id = id, -- num = 0 -- } -- t[id].num = t[id].num + num -- end -- end -- end -- return util.tablequeue(t) -- end return util