util.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. local c = require "util.core"
  2. local os_time = os.time
  3. local os_date = os.date
  4. local math_floor = math.floor
  5. local logger = require "logger"
  6. local util = {}
  7. local oneday = 24*3600
  8. ---------------------------- Maths ----------------------------
  9. function util.max(a, b)
  10. return a > b and a or b
  11. end
  12. function util.min(a, b)
  13. return a < b and a or b
  14. end
  15. function util.clamp(a, low, up)
  16. assert(up >= low)
  17. if a < low then
  18. return low -- min
  19. elseif a > up then
  20. return up -- max
  21. else
  22. return a -- original value
  23. end
  24. end
  25. ---------------------------- Time ----------------------------
  26. function util.timezone()
  27. return c.timezone()
  28. end
  29. function util.today(ti, sec)
  30. local tz = c.timezone() * 3600
  31. return ((ti or os_time()) + tz) // 86400 * 86400 - tz + (sec or 0)
  32. end
  33. function util.gettime()
  34. return c.gettime()
  35. end
  36. function util.gettimeofday()
  37. return c.gettimeofday()
  38. end
  39. function util.nextday(ti)
  40. return util.today(ti) + 86400
  41. end
  42. function util.week(ti)
  43. local w = tonumber(os_date("%w", ti))
  44. if w == 0 then w = 7 end -- 星期转换结果为 0
  45. return w
  46. end
  47. function util.hours(ti)
  48. return tonumber(os_date("%H", ti or os_time()))
  49. end
  50. function util.day(ti)
  51. return tonumber(os_date("%d", ti or os_time()))
  52. end
  53. function util.alarm(ti) -- 整点时间
  54. -- 获取下次刷新排行榜时间, 活动刷新时间
  55. local temp_tm = os.date("*t", ti or os_time())
  56. temp_tm.min = 0
  57. temp_tm.sec = 0
  58. return os_time(temp_tm) -- 刷新战斗力时间
  59. end
  60. -- time travel
  61. --[[
  62. 获取以 hour点 minute 作为跨天点的 时间间隔天数
  63. start_time : 开始的时间点
  64. end_time : 结束的时间点
  65. hour : 跨天的小时点
  66. minute : 跨天的分钟点
  67. not_start : true start_time 那一天的第一个跨点不计算 (例如:4点跨天 如果start_time 是0~4点,那么0~下一天4点是第一天)
  68. : false start_time 那一天的第一个跨点计算 (例如:4点跨天 如果start_time 是0~4点,那么4~下一天4点是第二天)
  69. ]]
  70. local function get_time_clock(give_time,hour,minute)
  71. -- 格式化时间为当天hour:minute
  72. -- local now_tm = os_date("*t", give_time)
  73. -- now_tm.hour = hour
  74. -- now_tm.min = minute
  75. -- now_tm.sec = 0
  76. local sec = hour * 3600 + minute * 60
  77. return util.today(give_time, sec)
  78. end
  79. function util.get_time_travel(start_time,end_time,hour,minute,not_start)
  80. assert(start_time)
  81. assert(end_time)
  82. assert(start_time <= end_time)
  83. hour = hour or 0
  84. minute = minute or 0
  85. not_start = not_start or false
  86. local start_tc = get_time_clock(start_time,hour,minute)
  87. local end_tc = get_time_clock(end_time,hour,minute)
  88. local start_zero = start_tc - (hour * 3600 + minute * 60)
  89. local end_zero =start_tc - (hour * 3600 + minute * 60)
  90. local travel_days = 0
  91. if start_time < start_tc then
  92. if not_start then
  93. -- 第一个跨点不计算 不用修正
  94. else
  95. -- 第一个跨点计算 修正到上一天
  96. start_tc = start_tc - oneday
  97. end
  98. end
  99. if (end_time < end_tc) and not (not_start and start_zero == end_zero) then
  100. end_tc = end_tc - oneday
  101. end
  102. return math_floor((end_tc - start_tc)/86400 + 1),end_tc
  103. -- local start_tc = get_time_clock(start_time,hour,minute)
  104. -- local end_tc = get_time_clock(end_time,hour,minute)
  105. -- return math_floor((end_time - start_tc)/oneday) + 1,end_tc
  106. end
  107. ---------------------------- Random ----------------------------
  108. local dice = {}
  109. local mt = { __index = dice }
  110. function util.newrandom(...)
  111. local self = { __cobj = assert(c.newrandom(...)) }
  112. return setmetatable(self, mt)
  113. end
  114. function dice:roll(...)
  115. return c.rand(self.__cobj, ...)
  116. end
  117. function dice:srand(seed)
  118. c.srand(self.__cobj, seed)
  119. end
  120. function dice:peekseed()
  121. return c.peekseed(self.__cobj)
  122. end
  123. local holdrand = 1
  124. function util.peekseed()
  125. return holdrand
  126. end
  127. function util.rand(...)
  128. local r, s = c.random(...)
  129. holdrand = s
  130. return r
  131. end
  132. function util.srand(seed)
  133. holdrand = seed
  134. c.srandom(holdrand)
  135. end
  136. ---------------------------- UUID ----------------------------
  137. function util.uuid()
  138. return c.uuid()
  139. end
  140. ---------------------------- Hash ----------------------------
  141. function util.hashcode(str)
  142. return c.hashcode(str)
  143. end
  144. ---------------------------- Table ----------------------------
  145. function util.length(t)
  146. local n = 0
  147. for _, _ in pairs(t) do
  148. n = n + 1
  149. end
  150. return n
  151. end
  152. -- table 序列化
  153. function util.tablequeue(obj)
  154. local temp = {}
  155. if not obj then
  156. return temp
  157. end
  158. for _, v in pairs(obj) do
  159. table.insert(temp, v)
  160. end
  161. return temp
  162. end
  163. -- Initialize random seed for function of the util.rand
  164. util.srand(os_time())
  165. util.rand() -- discard first value to avoid undesirable correlations
  166. function util.optimize_array(a)
  167. if a and next(a) then
  168. return a
  169. end
  170. end
  171. -- TODO: 计算指定时间的周一
  172. function util.week_monday(time)
  173. time = util.today(time)
  174. local w = util.week(time)
  175. -- if w == 0 then w = 7 end -- 星期转换结果为 0
  176. local rettm = time - (w - 1)*86400
  177. logger.trace(" 周一的时间节点 %s, 开服时间节点 %s",
  178. os.date("%Y-%m-%d %H:%M:%S",tonumber(rettm)),
  179. os.date("%Y-%m-%d %H:%M:%S",tonumber(time))
  180. )
  181. return rettm
  182. end
  183. -- TODO: 计算指定时间月份的 1号
  184. function util.month_one(time)
  185. time = util.today(time)
  186. local d = util.day(time)
  187. local rettm = time - (d - 1)*86400
  188. logger.trace(" 1 号的时间节点 %s, 开服时间节点 %s",
  189. os.date("%Y-%m-%d %H:%M:%S",tonumber(rettm)),
  190. os.date("%Y-%m-%d %H:%M:%S",tonumber(time))
  191. )
  192. return rettm
  193. end
  194. -- TODO: 计算开服后指定多少个月的 1号
  195. function util.next_month_one(time, many)
  196. many = (many or 1) - 1
  197. local one = util.month_one(time)
  198. local onetable = os.date("*t", one)
  199. local onetable_month = onetable.month + many
  200. local add_year = 0
  201. local set_month = onetable_month
  202. if onetable_month > 12 then
  203. add_year = math.modf(onetable_month/12)
  204. set_month = math.fmod( onetable_month, 12 )
  205. end
  206. onetable.year = onetable.year+add_year
  207. onetable.month = set_month
  208. local newtime = os.time(onetable)
  209. logger.trace(" ### 指定时间的1号 %s, 计算出来的1号 %s, many:%s",
  210. os.date("%Y-%m-%d %H:%M:%S",tonumber(one)), os.date("%Y-%m-%d %H:%M:%S",tonumber(newtime)),many)
  211. return newtime
  212. end
  213. function util.split(str)
  214. local chunks = {str:match("(%d+)%/(%d+)%/(%d+)%/(%d+)%/(%d+)%/(%d+)")}
  215. assert(#chunks == 6)
  216. local year, month, day, hour, min, sec = table.unpack(chunks)
  217. return os.time({ year=year, month=month, day=day, hour=hour, min=min, sec=sec })
  218. end
  219. -- function util.goods_merge(list)
  220. -- list = list or {}
  221. -- local ok, value = next(list)
  222. -- if not ok then
  223. -- return list
  224. -- end
  225. -- local t = {}
  226. -- if type(value) == "table" then
  227. -- for _, v in ipairs(list) do
  228. -- local id = v.id
  229. -- local num = v.num
  230. -- t[id] = t[id] or {
  231. -- id = id,
  232. -- num = 0
  233. -- }
  234. -- t[id].num = t[id].num + num
  235. -- end
  236. -- else
  237. -- for i=1, #list, 2 do
  238. -- local id = list[i]
  239. -- local num = list[i+1]
  240. -- if num and num > 0 then
  241. -- t[id] = t[id] or {
  242. -- id = id,
  243. -- num = 0
  244. -- }
  245. -- t[id].num = t[id].num + num
  246. -- end
  247. -- end
  248. -- end
  249. -- return util.tablequeue(t)
  250. -- end
  251. return util