源战役客户端
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1123 linhas
31 KiB

  1. --[[
  2. : (, )
  3. : deadline
  4. ]]
  5. TimeUtil = TimeUtil or {
  6. eng_faster_time = 0
  7. }
  8. local TimeUtil = TimeUtil
  9. --[[
  10. : (),(s)
  11. :
  12. : deadline
  13. ]]
  14. function TimeUtil:getClientTime( )
  15. return Status.NowUnscaledTime
  16. end
  17. --[[
  18. : (:s)
  19. :
  20. :
  21. : deadline
  22. ]]
  23. function TimeUtil:getServerTime( )
  24. return math.floor((Status.NowUnscaledTime*1000 + self.eng_faster_time)/1000 + 0.5)
  25. end
  26. --[[
  27. : (),(ms)
  28. :
  29. : deadline
  30. ]]
  31. function TimeUtil:getClientTimeMs( )
  32. return Status.NowUnscaledTime*1000
  33. end
  34. --[[
  35. : (:ms)
  36. :
  37. :
  38. : deadline
  39. ]]
  40. function TimeUtil:getServerTimeMs( )
  41. return Status.NowUnscaledTime*1000 + self.eng_faster_time
  42. end
  43. --[[
  44. :
  45. :
  46. server_time (ms) int
  47. :
  48. : deadline
  49. ]]
  50. function TimeUtil:notifyServerTime(server_time)
  51. self.eng_faster_time = server_time - Status.NowUnscaledTime*1000
  52. end
  53. function TimeUtil:convertTime(times)
  54. local timeStr = ""
  55. local second = math.modf(times % 60)
  56. local minute = math.modf(times/60)
  57. if minute >= 60 then
  58. times = math.modf(minute / 60);
  59. timeStr=timeStr..((times > 9 and times) or "0"..times)
  60. timeStr=timeStr..(":")
  61. minute= math.modf(minute % 60)
  62. timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
  63. timeStr=timeStr..(":")
  64. timeStr=timeStr..((second > 9 and second) or "0"..second)
  65. else
  66. timeStr=timeStr..("00:")
  67. timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
  68. timeStr=timeStr..(":")
  69. timeStr=timeStr..((second > 9 and second) or "0"..second)
  70. end
  71. return timeStr
  72. end
  73. --[[
  74. zhongdong
  75. ]]--
  76. function TimeUtil:convertTimeWithoutHour(times)
  77. local timeStr = ""
  78. local second = math.modf(times % 60)
  79. local minute = math.modf(times/60)
  80. if minute >= 60 then
  81. times = math.modf(minute / 60);
  82. timeStr=timeStr..((times > 9 and times) or "0"..times)
  83. timeStr=timeStr..(":")
  84. minute= math.modf(minute % 60)
  85. timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
  86. timeStr=timeStr..(":")
  87. timeStr=timeStr..((second > 9 and second) or "0"..second)
  88. else
  89. -- timeStr=timeStr..("00:")
  90. timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
  91. timeStr=timeStr..(":")
  92. timeStr=timeStr..((second > 9 and second) or "0"..second)
  93. end
  94. return timeStr
  95. end
  96. -- 获得当前时间 m:秒
  97. function TimeUtil:timeSec(times)
  98. local time = os.date("%X", times)
  99. local datearr = Language.split(time,":")
  100. local result=datearr[1]*3600+datearr[2]*60+datearr[3]
  101. return result
  102. end
  103. --解析"21:30"这样的字符串,返回{hour:21, min:30}
  104. function TimeUtil:ParseHourMinute(time_str)
  105. -- local p = string.find(time_str, ":")
  106. local ss = Language.split(time_str, ":")
  107. local ret = {}
  108. ret.hour = tonumber(ss[1])
  109. ret.min = tonumber(ss[2])
  110. return ret
  111. end
  112. --[[
  113. *
  114. * @param timeStr
  115. * @return
  116. ]]
  117. function TimeUtil:timeConversion(timeStr, stype)
  118. if timeStr == nil then
  119. return ""
  120. end
  121. timeStr = tonumber(timeStr)
  122. if timeStr < 0 then timeStr = 0 end
  123. local cur_type = stype or "date"
  124. local date = os.date("*t", timeStr)
  125. local monthStr = date.month < 10 and "0"..date.month or date.month
  126. local dateStr = date.day < 10 and "0"..date.day or date.day
  127. local hoursStr = date.hour < 10 and "0"..date.hour or date.hour
  128. local minutesStr = date.min < 10 and "0"..date.min or date.min
  129. local secondStr = date.sec < 10 and "0"..date.sec or date.sec
  130. if cur_type == "date" then
  131. return date.year .. "-" .. monthStr .. "-" .. dateStr
  132. elseif cur_type =="mm-dd" then
  133. return monthStr .. "-" .. dateStr
  134. elseif cur_type == "mm-dd hh-MM" then
  135. return monthStr .. "-" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
  136. elseif cur_type == "yyyy-mm-dd hh-MM" then
  137. return date.year .. "-" .. monthStr .. "-" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
  138. elseif cur_type == "yyyy年mm月dd日 hh-MM" then
  139. return date.year .. "" .. monthStr .. "" .. dateStr .. "" .. hoursStr .. ":" .. minutesStr
  140. elseif cur_type == "yyyy年mm月dd日 hh" then
  141. return date.year .. "" .. monthStr .. "" .. dateStr .. "" .. hoursStr..""
  142. elseif cur_type == "yyyy-mm-dd" then
  143. return date.year.."-"..monthStr.."-"..dateStr
  144. elseif cur_type =="yyyy-mm-dd hh:mm:ss" then
  145. return date.year .. "-" .. monthStr .. "-" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr
  146. elseif cur_type == "yyyy年mm月dd日" then
  147. return date.year .. "" .. monthStr .. "" .. dateStr .. ""
  148. elseif cur_type == "yyyy年mm月dd日 hh:MM:ss" then
  149. return date.year .. "" .. monthStr .. "" .. dateStr .. "" .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr
  150. elseif cur_type == "hh-MM" then
  151. return hoursStr .. ":" .. minutesStr
  152. elseif cur_type == "MM-ss" then
  153. return minutesStr .. ":" .. secondStr
  154. elseif cur_type == "hh-MM-ss" then
  155. return hoursStr..":"..minutesStr .. ":" .. secondStr
  156. elseif cur_type =="mm-dd hh:mm" then
  157. return monthStr.."-"..dateStr.." "..hoursStr..":"..minutesStr
  158. elseif cur_type =="mm月dd日 hh:mm" then
  159. return monthStr..""..dateStr..""..hoursStr..":"..minutesStr
  160. elseif cur_type =="mm/dd hh:mm" then
  161. return monthStr.."/"..dateStr.." "..hoursStr..":"..minutesStr
  162. elseif cur_type =="yy/mm/dd hh:mm" then
  163. return date.year .. "/" .. monthStr .. "/" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
  164. elseif cur_type =="yy/mm/dd" then
  165. return date.year .. "/" .. monthStr .. "/" .. dateStr
  166. elseif cur_type =="yy/mm/dd hh:mm:ss" then
  167. return date.year .. "/" .. monthStr .. "/" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr
  168. elseif cur_type =="yymmddhhmmss" then
  169. return date.year ..monthStr .. dateStr .. hoursStr .. minutesStr .. secondStr
  170. elseif cur_type =="mm/dd" then
  171. return monthStr .. "/" .. dateStr
  172. elseif cur_type =="mm月dd日" then
  173. return monthStr..""..dateStr..""
  174. elseif cur_type == "mm.dd" then
  175. return monthStr.."."..dateStr
  176. elseif cur_type == "mm-dd hh:MM:ss" then
  177. return monthStr .. "-" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr
  178. elseif cur_type =="hh:mm:ss" then
  179. return hoursStr .. ":" .. minutesStr .. ":" .. secondStr
  180. elseif cur_type =="exfont_hh:mm:ss" then--主题活动副本双倍活动“00时00分00秒”艺术字
  181. return hoursStr .. "h" .. minutesStr .. "m" .. secondStr.."s"
  182. elseif cur_type =="hh:mm" then
  183. return hoursStr .. ":" .. minutesStr
  184. elseif cur_type =="act_hh:mm" then
  185. return hoursStr .. "h" .. minutesStr.."m"
  186. elseif cur_type =="yyyy.mm.dd hh:mm" then
  187. return date.year .. "." .. monthStr .. "." .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
  188. elseif cur_type =="mm.dd hh:mm" then
  189. return monthStr .. "." .. dateStr .. " " .. hoursStr .. ":" .. minutesStr
  190. elseif cur_type == "yyyy.mm.dd" then
  191. return date.year .. "." .. monthStr .. "." .. dateStr
  192. else
  193. return monthStr.."//"..date.day.." "..hoursStr..":"..minutesStr
  194. end
  195. end
  196. --[[
  197. *
  198. * @param timeStr
  199. * @return
  200. ]]
  201. function TimeUtil:timeConversionWithMillisecond(timeStr, stype)
  202. if timeStr == nil then
  203. return ""
  204. end
  205. timeStr = tonumber(timeStr)
  206. if timeStr < 0 then timeStr = 0 end
  207. local cur_type = stype or "date"
  208. local timeStr, milli = math.modf(timeStr / 1000)
  209. local date = os.date("*t", timeStr)
  210. local monthStr = date.month < 10 and "0"..date.month or date.month
  211. local dateStr = date.day < 10 and "0"..date.day or date.day
  212. local hoursStr = date.hour < 10 and "0"..date.hour or date.hour
  213. local minutesStr = date.min < 10 and "0"..date.min or date.min
  214. local secondStr = date.sec < 10 and "0"..date.sec or date.sec
  215. local str = timeStr % 86400000
  216. local len = string.len(str)
  217. local millisecond = string.sub(str, len - 2, len)
  218. if cur_type =="yy/mm/dd hh:mm:ss.ms" then
  219. return date.year .. "/" .. monthStr .. "/" .. dateStr .. " " .. hoursStr .. ":" .. minutesStr .. ":" .. secondStr .. "." .. millisecond
  220. elseif "hh:mm:ss.ms" then
  221. return hoursStr .. ":" .. minutesStr .. ":" .. secondStr .. "." .. millisecond
  222. else
  223. return monthStr.."//"..date.day.." "..hoursStr..":"..minutesStr
  224. end
  225. end
  226. --[[
  227. * second
  228. * formatStr hh替换为小时 mm替换为分钟 ss替换为秒
  229. * addZero 0
  230. ]]
  231. function TimeUtil:timeConvert(second, dateType, addZero)
  232. local function appendZero(time)
  233. return string.format("%02d", time)
  234. end
  235. dateType = dateType or "hh小时mm分ss秒"
  236. if addZero==nil then
  237. addZero=true
  238. else
  239. addZero = addZero
  240. end
  241. local h = math.floor(second / 3600)
  242. local m = math.floor((second % 3600) / 60)
  243. local s = second % 60;
  244. if addZero == true then
  245. h = appendZero(h)
  246. m = appendZero(m)
  247. s = appendZero(s)
  248. end
  249. local str = string.gsub(dateType, "hh", h)
  250. str = string.gsub(str, "mm", m)
  251. str = string.gsub(str, "ss", s)
  252. return str
  253. end
  254. --[[
  255. * second
  256. * formatStr mm替换为分钟 ss替换为秒
  257. * addZero 0
  258. ]]
  259. function TimeUtil:timeConvert2(second, dateType, addZero)
  260. local function appendZero(time)
  261. return string.format("%02d", time)
  262. end
  263. dateType = dateType or "mm分ss秒"
  264. if addZero==nil then
  265. addZero=true
  266. else
  267. addZero = addZero
  268. end
  269. local m = math.floor((second % 3600) / 60)
  270. local s = second % 60;
  271. if addZero == true then
  272. m = appendZero(m)
  273. s = appendZero(s)
  274. end
  275. local str = ""
  276. if tonumber(m) > 0 then
  277. str = string.gsub(dateType, "mm", m)
  278. else
  279. str = "ss秒"
  280. end
  281. str = string.gsub(str, "ss", s)
  282. return str
  283. end
  284. --[[
  285. @param:second
  286. @retuen ***
  287. ]]
  288. function TimeUtil:qianggouTimeLeft2(second, hide_second)
  289. local s=second%60
  290. local m = math.floor(second/60);
  291. local min = m % 60
  292. if hide_second then
  293. min = 0
  294. end
  295. local str = min == 0 and "" or min .. "";
  296. local hour
  297. local d
  298. if m >= 60 then
  299. local h = math.floor(m/60);
  300. hour = h % 24
  301. str = (hour == 0 and "" or hour .. "") .. str;
  302. if h >= 24 then
  303. d = math.floor(h/24);
  304. str = d .. "" .. str;
  305. end
  306. elseif m > 0 and m < 60 then
  307. str = m .. "" .. s .. ""
  308. else
  309. str = s .. ""
  310. end
  311. return str,(d or 0),(hour or 0),(min or 0),(s or 0)
  312. end
  313. --[[
  314. @param:second
  315. @retuen **
  316. ]]
  317. function TimeUtil:timeConvert6(second, show_second, not_show_day)
  318. if second == nil then return "" end
  319. local s = second % 60
  320. local m = math.floor(second / 60);
  321. local min = m % 60
  322. local str = min == 0 and "" or min .. ""
  323. local hour
  324. local d
  325. if m >= 60 then
  326. local h = math.floor(m / 60);
  327. hour = h % 24
  328. str = hour == 0 and "" or hour .. "" .. str
  329. if show_second then
  330. str = str .. s .. ""
  331. end
  332. if h >= 24 and not not_show_day then
  333. d = math.floor(h/24);
  334. str = d .. "" .. str;
  335. end
  336. elseif m > 0 and m < 60 then
  337. str = m .. ""
  338. if show_second then
  339. str = str .. s .. ""
  340. end
  341. else
  342. str = str .. s .. ""
  343. end
  344. if second <= 0 then
  345. str = 0
  346. end
  347. return str
  348. end
  349. --[[
  350. @param:second
  351. @retuen **()
  352. ]]
  353. function TimeUtil:timeConvertColor6(second, show_second, not_show_day, color, mask_empty)
  354. if second == nil then return "" end
  355. local s = second % 60
  356. local m = math.floor(second / 60);
  357. local min = m % 60
  358. local str = min == 0 and "" or " <color="..color..">"..min.."</color> 分"
  359. local hour
  360. local d
  361. if m >= 60 then
  362. local h = math.floor(m / 60);
  363. hour = h % 24
  364. str = hour == 0 and "" or " <color="..color..">"..hour.."</color> 时" .. str
  365. if show_second then
  366. str = str .. " <color="..color..">"..s.."</color> 秒"
  367. end
  368. if h >= 24 and not not_show_day then
  369. d = math.floor(h/24);
  370. str = " <color="..color..">"..d.."</color> 天" .. str;
  371. end
  372. elseif m > 0 and m < 60 then
  373. str = " <color="..color..">"..min.."</color> 分"
  374. if show_second then
  375. str = str .. " <color="..color..">"..s.."</color> 秒"
  376. end
  377. else
  378. str = str .. " <color="..color..">"..s.."</color> 秒"
  379. end
  380. if second <= 0 then
  381. str = 0
  382. end
  383. if mask_empty then
  384. str = string.gsub( str, " ", "" )
  385. end
  386. return str
  387. end
  388. --[[
  389. @param:second
  390. @retuen **
  391. ]]
  392. function TimeUtil:timeConvert7(second)
  393. if second == nil then return "" end
  394. local s = second % 60
  395. local m = math.floor(second / 60);
  396. local min = m % 60
  397. local str = min == 0 and "" or min .. ""
  398. local hour
  399. local d
  400. if m >= 60 then
  401. local h = math.floor(m / 60);
  402. hour = h % 24
  403. str = hour == 0 and "" or hour .. "小时" .. str
  404. if h >= 24 then
  405. d = math.floor(h/24);
  406. str = d .. "" .. str;
  407. end
  408. elseif m > 0 and m < 60 then
  409. str = m .. ""
  410. end
  411. if second <= 0 then
  412. str = 0
  413. end
  414. return str
  415. end
  416. --[[
  417. @param:second
  418. @*00:0000:00:00
  419. @retuen *00:00:00
  420. @day_show_second显示*00:00:00
  421. ]]
  422. function TimeUtil:timeConvert3(second,day_show_second)
  423. local s=second%60
  424. local min = math.floor(second/60)%60
  425. local hour = math.floor(second/3600)%24
  426. local day = math.floor(second/86400)
  427. local str = ""
  428. if day > 0 then--显示*天00:00
  429. if not day_show_second then
  430. str = string.format("%s天%02d:%02d",day,hour,min)
  431. else
  432. str = string.format("%s天%02d:%02d:%02d",day,hour,min,s)
  433. end
  434. else--显示00:00:00
  435. str = string.format("%02d:%02d:%02d",hour,min,s)
  436. end
  437. return str
  438. end
  439. --[[
  440. @param:second
  441. @*00:00:0000:00:00
  442. @retuen *00:00:00
  443. ]]
  444. function TimeUtil:timeConvert4(second)
  445. local s=second%60
  446. local s_str = s == 0 and ":00" or string.format(":%02d",s);
  447. local m = math.floor(second/60);
  448. local min = m % 60
  449. local str = min == 0 and ":00" or string.format(":%02d",min);
  450. local hour
  451. local d
  452. if m >= 60 then
  453. local h = math.floor(m/60);
  454. hour = h % 24
  455. str = (hour == 0 and ":00" or string.format("%02d",hour))..str;
  456. if h >= 24 then
  457. d = math.floor(h/24);
  458. str = d .. "" .. str..s_str;
  459. if d >= 7 then
  460. str = "超时"
  461. end
  462. else
  463. str = str..s_str
  464. end
  465. else
  466. str = "00"..str..s_str
  467. end
  468. return str
  469. end
  470. --[[
  471. @des ()
  472. @author
  473. ]]
  474. function TimeUtil:ConvertFmStringToTime(str,ipattern)
  475. --local pattern1 = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)([%+%-])(%d+)%:(%d+)"
  476. local pattern = {}
  477. pattern[1] = "(%d+)/(%d+)/(%d+)%s+(%d+):(%d+)" --对应2013/6/8 23:59
  478. pattern[2] = "(%d+)@(%d+)@(%d+)@(%d+)@(%d+)" --对应2013@6@8@23@59
  479. local xyear, xmonth, xday, xhour, xminute,xsec = string.match(str,pattern[ipattern])
  480. local convertedTimestamp = os.time({year = xyear, month = xmonth,
  481. day = xday, hour = xhour, min = xminute,sec = xsec})
  482. return convertedTimestamp;
  483. end
  484. --[[
  485. @describtion:
  486. @param:time
  487. @return:
  488. @author:hjp
  489. ]]
  490. function TimeUtil:GetTimeStrByServerTime( time )
  491. local dec_time = self:getServerTime() - time
  492. if dec_time <= 0 then
  493. return "刚刚"
  494. end
  495. local tem = 0
  496. local month = math.floor(dec_time / (3600*24*30))
  497. tem = dec_time % (3600*24*30)
  498. local day = math.floor(tem / (3600 * 24))
  499. tem = tem % (3600*24)
  500. local h = math.floor(tem / 3600)
  501. tem = tem % 3600
  502. local m = math.floor(tem / 60)
  503. tem = tem % 60
  504. local s = tem
  505. if month ~= 0 then
  506. return month .. "月前"
  507. elseif day ~= 0 then
  508. return day .. "天前"
  509. elseif h ~= 0 then
  510. return h .. "小时前"
  511. elseif m ~= 0 then
  512. return m .. "分钟前"
  513. elseif s ~= 0 then
  514. return s .. "秒前"
  515. else
  516. return ""
  517. end
  518. end
  519. --[[
  520. @describtion:
  521. @param:time
  522. @return:
  523. @author:hjp
  524. ]]
  525. function TimeUtil:GetTimeAgoStr( dec_time )
  526. if dec_time <= 0 then
  527. return "刚刚"
  528. end
  529. local tem = 0
  530. local month = math.floor(dec_time / (3600*24*30))
  531. tem = dec_time % (3600*24*30)
  532. local day = math.floor(tem / (3600 * 24))
  533. tem = tem % (3600*24)
  534. local h = math.floor(tem / 3600)
  535. tem = tem % 3600
  536. local m = math.floor(tem / 60)
  537. tem = tem % 60
  538. local s = tem
  539. if month ~= 0 then
  540. return month .. "月前"
  541. elseif day ~= 0 then
  542. return day .. "天前"
  543. elseif h ~= 0 then
  544. return h .. "小时前"
  545. elseif m ~= 0 then
  546. return m .. "分钟前"
  547. elseif s ~= 0 then
  548. return s .. "秒前"
  549. else
  550. return ""
  551. end
  552. end
  553. --获取今日三点的时间戳
  554. function TimeUtil:GetToDayThreeHourStr( ... )
  555. local now=self:getServerTime()
  556. local date = os.date("*t", now)
  557. local date_three={year=date.year,month=date.month,day=date.day,hour=3,min=0,sec=0}
  558. return os.time(date_three)
  559. end
  560. --N(0-24)--获取今日N点的时间戳
  561. function TimeUtil:GetToDayNHourStr( hour_num )
  562. local now=self:getServerTime()
  563. local date = os.date("*t", now)
  564. local date_n = {year=date.year,month=date.month,day=date.day,hour=hour_num,min=0,sec=0}
  565. return os.time(date_n)
  566. end
  567. --N(0-24)--获取当前的小时数
  568. function TimeUtil:GetHourNow( )
  569. local now=self:getServerTime()
  570. local date = os.date("*t", now)
  571. return date.hour
  572. end
  573. --转化时间,得到天或是分钟
  574. function TimeUtil:timeConvert5(second)
  575. local s = second % 60
  576. local m = math.floor(second / 60);
  577. local min = m % 60
  578. local str = ""
  579. local hour
  580. local d
  581. if m >= 60 then
  582. local h = math.floor(m/60);
  583. hour = h % 24
  584. str = hour == 0 and "" or hour .. "小时"
  585. if h >= 24 then
  586. d = math.floor(h/24);
  587. str = d .. ""
  588. end
  589. else
  590. -- str = "小于1小时"
  591. str = m .. "分钟"
  592. end
  593. return str
  594. end
  595. -- --转化时间,得到(XX天XX小时 或XX小时XX分 或XX分)
  596. function TimeUtil:timeConvert7(second)
  597. local s = second % 60
  598. local m = math.floor(second / 60);
  599. local min = m % 60
  600. local str = ""
  601. local hour
  602. local d
  603. if m >= 60 then
  604. local h = math.floor(m/60);
  605. hour = h % 24
  606. str = hour == 0 and "" or hour .. "小时" .. min .. ""
  607. if h >= 24 then
  608. d = math.floor(h/24);
  609. str = d .. "" .. hour.. "小时"
  610. end
  611. else
  612. str = min .. ""
  613. if min == 0 then
  614. str = "1分"
  615. end
  616. end
  617. return str
  618. end
  619. function TimeUtil:timeConvert12(time)
  620. local dateStr = math.floor(time / 60 / 60 / 24)
  621. local hoursStr = math.floor(time / 60 / 60) % 24
  622. local minutesStr = math.floor(time / 60) % 60
  623. local secondStr = time % 60
  624. if dateStr > 0 then
  625. return string.format("%s天%s时", dateStr, hoursStr)
  626. else
  627. if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
  628. if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
  629. if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
  630. return hoursStr ..":" .. minutesStr .. ":" ..secondStr
  631. end
  632. end
  633. function TimeUtil:timeConvert13(time)
  634. local dateStr = math.floor(time / 60 / 60 / 24)
  635. local hoursStr = math.floor(time / 60 / 60) % 24
  636. local minutesStr = math.floor(time / 60) % 60
  637. local secondStr = time % 60
  638. if dateStr > 0 then
  639. return string.format("%s天%s时%s分", dateStr, hoursStr,minutesStr)
  640. else
  641. if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
  642. if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
  643. if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
  644. return hoursStr ..":" .. minutesStr .. ":" ..secondStr
  645. end
  646. end
  647. function TimeUtil:timeConvert14(time)
  648. local dateStr = math.floor(time / 60 / 60 / 24)
  649. local hoursStr = math.floor(time / 60 / 60) % 24
  650. local minutesStr = math.floor(time / 60) % 60
  651. local secondStr = time % 60
  652. if dateStr > 0 then
  653. return string.format("%s天%s时", dateStr, hoursStr)
  654. elseif hoursStr > 0 then
  655. return string.format("%s时%s分", hoursStr,minutesStr)
  656. else
  657. -- if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
  658. if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
  659. if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
  660. return minutesStr .. ":" ..secondStr
  661. -- return hoursStr ..":" .. minutesStr .. ":" ..secondStr
  662. end
  663. end
  664. function TimeUtil:timeConvert15(time)
  665. local dateStr = math.floor(time / 60 / 60 / 24)
  666. local hoursStr = math.floor(time / 60 / 60) % 24
  667. local minutesStr = math.floor(time / 60) % 60
  668. local secondStr = time % 60
  669. local str = ""
  670. if dateStr > 0 then
  671. str = dateStr .. ""
  672. str = hoursStr > 0 and str .. hoursStr .. "" or str
  673. str = minutesStr > 0 and str .. minutesStr .. "" or str
  674. else
  675. str = hoursStr > 0 and str .. hoursStr .. "" or str
  676. str = minutesStr > 0 and str .. minutesStr .. "" or str
  677. str = secondStr > 0 and str .. secondStr .. "" or str
  678. end
  679. return str
  680. end
  681. function TimeUtil:timeConvert16(time)
  682. local dateStr = math.floor(time / 60 / 60 / 24)
  683. local hoursStr = math.floor(time / 60 / 60) % 24
  684. local minutesStr = math.floor(time / 60) % 60
  685. local secondStr = time % 60
  686. if dateStr > 0 then
  687. return string.format("%s天%s时%s分", dateStr, hoursStr,minutesStr)
  688. else
  689. -- if tonumber(hoursStr) < 10 then hoursStr = 0 .. hoursStr end
  690. -- if tonumber(minutesStr) < 10 then minutesStr = 0 .. minutesStr end
  691. -- if tonumber(secondStr) < 10 then secondStr = 0 .. secondStr end
  692. return hoursStr .."" .. minutesStr .. "" ..secondStr..""
  693. end
  694. end
  695. function TimeUtil:timeConvert17(time, color)
  696. local dateStr = math.floor(time / 60 / 60 / 24)
  697. local hoursStr = math.floor(time / 60 / 60) % 24
  698. local minutesStr = math.floor(time / 60) % 60
  699. local secondStr = time % 60
  700. local str = ""
  701. if color then
  702. if dateStr > 0 then
  703. str = HtmlColorTxt(dateStr , color) .. ""
  704. str = hoursStr > 0 and str .. HtmlColorTxt(hoursStr , color) .. "" or str
  705. elseif hoursStr > 0 then
  706. str = hoursStr > 0 and str .. HtmlColorTxt(hoursStr , color) .. "" or str
  707. str = minutesStr > 0 and str .. HtmlColorTxt(minutesStr , color) .. "" or str
  708. else
  709. str = minutesStr > 0 and str .. HtmlColorTxt(minutesStr , color) .. "" or str
  710. str = secondStr > 0 and str .. HtmlColorTxt(secondStr , color) .. "" or str
  711. end
  712. else
  713. if dateStr > 0 then
  714. str = dateStr .. ""
  715. str = hoursStr > 0 and str .. hoursStr .. "" or str
  716. str = minutesStr > 0 and str .. minutesStr .. "" or str
  717. elseif hoursStr > 0 then
  718. str = hoursStr > 0 and str .. hoursStr .. "" or str
  719. str = minutesStr > 0 and str .. minutesStr .. "" or str
  720. else
  721. str = minutesStr > 0 and str .. minutesStr .. "" or str
  722. str = secondStr > 0 and str .. secondStr .. "" or str
  723. end
  724. end
  725. return str
  726. end
  727. -- 目标<=现在->过去 false
  728. function TimeUtil:ConfigTimeCompare(timeStr, now_time)
  729. local dec_time = now_time or self:getServerTime()
  730. dec_time = self:timeConversion(dec_time,"yymmddhhmmss")
  731. timeStr = string.gsub(timeStr,"%-","")
  732. timeStr = string.gsub(timeStr,"%:","")
  733. -- timeStr = Trim(timeStr)
  734. return tonumber(timeStr) > tonumber(dec_time)
  735. end
  736. function TimeUtil:GetWeekStrNum(week)
  737. local week_list = {"","","","","","",""}
  738. return week_list[week]
  739. end
  740. --获取未来某一天0点时间戳
  741. function TimeUtil:GetFutureZeroStamp(future_days)
  742. local cur_timestamp = os.time()
  743. local one_hour_timestamp = 24*60*60
  744. local temp_time = cur_timestamp + one_hour_timestamp * future_days
  745. local temp_date = os.date("*t", temp_time)
  746. temp_date.hour = 0
  747. temp_date.min = 0
  748. temp_date.sec = 0
  749. return os.time(temp_date)
  750. end
  751. --获取未来某一天0点时间戳(使用服务器时间戳去计算)
  752. function TimeUtil:GetFutureZeroStampServer(future_days)
  753. local cur_timestamp =self:getServerTime()
  754. local one_hour_timestamp = 24*60*60
  755. local temp_time = cur_timestamp + one_hour_timestamp * future_days
  756. local temp_date = os.date("*t", temp_time)
  757. temp_date.hour = 0
  758. temp_date.min = 0
  759. temp_date.sec = 0
  760. return os.time(temp_date)
  761. end
  762. --获取未来某一天4点时间戳(使用服务器时间戳去计算)
  763. function TimeUtil:GetFutureFourStampServer(future_days)
  764. local cur_timestamp =self:getServerTime()
  765. local one_hour_timestamp = 24*60*60
  766. local temp_time = cur_timestamp + one_hour_timestamp * future_days
  767. local temp_date = os.date("*t", temp_time)
  768. temp_date.hour = 4
  769. temp_date.min = 0
  770. temp_date.sec = 0
  771. return os.time(temp_date)
  772. end
  773. --[[
  774. @param:second
  775. @return 00:00
  776. ]]
  777. function TimeUtil:timeConvert8(second)
  778. local s=second%60
  779. local s_str = s == 0 and ":00" or string.format(":%02d",s);
  780. local m = math.floor(second/60);
  781. local min = m % 60
  782. local str = min == 0 and "00" or string.format("%02d",min);
  783. str = str..s_str
  784. return str
  785. end
  786. --[[
  787. @param:second
  788. @retuen **
  789. ]]
  790. function TimeUtil:timeConvert9(second)
  791. if second == nil then return "" end
  792. local s = second % 60
  793. local m = math.floor(second / 60);
  794. local min = m % 60
  795. local str = min == 0 and "" or min .. ""
  796. local hour
  797. local d
  798. if m >= 60 then
  799. local h = math.floor(m / 60);
  800. hour = h % 24
  801. str = hour == 0 and "" or hour .. "小时" .. str
  802. if h >= 24 then
  803. d = math.floor(h/24);
  804. str = d .. ""
  805. end
  806. elseif m > 0 and m < 60 then
  807. if s > 0 then
  808. str = m .. "" .. s .. ""
  809. else
  810. str = m .. ""
  811. end
  812. else
  813. if s > 0 then
  814. str = str .. s .. ""
  815. end
  816. end
  817. if second <= 0 then
  818. str = 0
  819. end
  820. return str
  821. end
  822. --[[
  823. @param:second
  824. @retuen **
  825. ]]
  826. function TimeUtil:timeConvert10(second, show_second)
  827. if second == nil then return "" end
  828. local s = second % 60
  829. local m = math.floor(second / 60);
  830. local min = m % 60
  831. local str = min == 0 and "" or min .. ""
  832. local hour
  833. local d
  834. if m >= 60 then
  835. local h = math.floor(m / 60);
  836. hour = h % 24
  837. str = hour == 0 and "" or hour .. "" .. str
  838. if show_second then
  839. str = str .. s .. ""
  840. end
  841. elseif m > 0 and m < 60 then
  842. str = m .. ""
  843. if show_second then
  844. str = str .. s .. ""
  845. end
  846. else
  847. if show_second then
  848. str = str .. s .. ""
  849. else
  850. str = "0" .. ""
  851. end
  852. end
  853. if second <= 0 then
  854. str = 0
  855. end
  856. return str
  857. end
  858. --[[
  859. @param:second
  860. @retuen 0000
  861. ]]
  862. function TimeUtil:timeConvert11(second, show_second)
  863. if second == nil then return "" end
  864. local s = second % 60
  865. local m = math.floor(second / 60);
  866. local min = m % 60
  867. local str = min == 0 and "" or string.format("%02d",min) .. ""
  868. local hour
  869. local d
  870. local h = math.floor(m / 60);
  871. if m >= 60 then
  872. hour = h % 24
  873. str = hour == 0 and "" or string.format("%02d",h) .. "" .. string.format("%02d",min)..""
  874. if show_second then
  875. str = str .. s .. ""
  876. end
  877. elseif m > 0 and m < 60 then
  878. str = string.format("%02d",h) .. "" .. string.format("%02d",m)..""
  879. if show_second then
  880. str = str .. s .. ""
  881. end
  882. else
  883. if show_second then
  884. str = str .. s .. ""
  885. else
  886. str = "00时01分"
  887. end
  888. end
  889. if second <= 0 then
  890. str = "00时00分"
  891. end
  892. return str
  893. end
  894. --[[
  895. @param:second
  896. @retuen 00000000
  897. ]]
  898. function TimeUtil:qianggouTimeLeft3(second, hide_second)
  899. local s=second%60
  900. local m = math.floor(second/60);
  901. local min = m % 60
  902. if hide_second then
  903. min = 0
  904. end
  905. local str = min == 0 and "" or string.format("%02d",min) .. ""..string.format("%02d",s) .. "";
  906. local hour
  907. local d
  908. if m >= 60 then
  909. local h = math.floor(m/60);
  910. hour = h % 24
  911. str = (hour == 0 and "" or string.format("%02d",hour) .. "") .. str;
  912. if h >= 24 then
  913. d = math.floor(h/24);
  914. str = string.format("%02d",d) .. "" .. str;
  915. end
  916. elseif m > 0 and m < 60 then
  917. str = "00天00时"..string.format("%02d",m) .. "" .. string.format("%02d",s) .. ""
  918. else
  919. str = "00天00时00分"..string.format("%02d",s) .. ""
  920. end
  921. return str,(d or 0),(hour or 0),(min or 0),(s or 0)
  922. end
  923. --根据开服第几天获取周几
  924. function TimeUtil:GetWeekStrByOpenDay( day )
  925. day = tonumber(day)
  926. local openDay = ServerTimeModel:getInstance():GetOpenServerDay()
  927. local curTime = TimeUtil:getServerTime()
  928. local targetTime = curTime+(day-openDay)*24*60*60
  929. local week = os.date("%w", targetTime)
  930. return week
  931. end
  932. --根据合服第几天获取周几
  933. function TimeUtil:GetWeekStrByMergeDay( day )
  934. day = tonumber(day)
  935. local openDay = ServerTimeModel:getInstance():GetMergeServerDay()
  936. local curTime = TimeUtil:getServerTime()
  937. local targetTime = curTime+(day-openDay)*24*60*60
  938. local week = os.date("%w", targetTime)
  939. return week
  940. end
  941. --今天是周几
  942. function TimeUtil:GetCurrentWeekDay()
  943. local curTime = TimeUtil:getServerTime()
  944. local targetTime = curTime
  945. local week = os.date("%w", targetTime)
  946. return week
  947. end
  948. --获取本地的时区偏移,缓存一下,减少os.date的内存分配
  949. function TimeUtil:GetLocalOffZone( )
  950. if not self.local_off_zone then
  951. local now_time = os.time()
  952. local now_time_table = os.date("*t", now_time)
  953. local local_zone = os.difftime(now_time, os.time(os.date("!*t", now_time)))/3600
  954. local off_dst = now_time_table.isdst and 1 or 0 --夏令时
  955. self.local_off_zone = self:GetSereverZone() - (local_zone + off_dst)
  956. end
  957. return self.local_off_zone
  958. end
  959. --获取服务器时间转化为本地时区的时间
  960. function TimeUtil:GetZoneTime( )
  961. local off_zone = self:GetLocalOffZone()
  962. local cur_date = self:getServerTime() + off_zone*3600
  963. -- print("tanar - [ TimeUtil ] [518] ==> local_zone: ",local_zone, off_zone, now_time_table.isdst)
  964. return cur_date
  965. end
  966. --获取服务器所在时区,国内版本东八区,其他语言版本再改
  967. function TimeUtil:GetSereverZone( )
  968. return 8
  969. end
  970. --转换毫秒倒计时(00:00.00)
  971. function TimeUtil:convertMillisecondTime( times,no_show_dot_num )
  972. local timeStr = ""
  973. local second = math.modf(times % 60)
  974. local minute = math.modf(times/60)
  975. timeStr=timeStr..((minute > 9 and minute) or "0"..minute)
  976. timeStr=timeStr..(":")
  977. timeStr=timeStr..((second > 9 and second) or "0"..second)
  978. if not no_show_dot_num then
  979. local int_times = math.floor(times)
  980. local dot_num = times - int_times
  981. if dot_num > 0.1 then
  982. timeStr = timeStr..(".")..math.floor(dot_num*100)
  983. elseif dot_num > 0 and dot_num < 0.01 then
  984. timeStr = timeStr..(".0")..math.floor(dot_num*100)
  985. else
  986. timeStr = timeStr..(".00")
  987. end
  988. end
  989. return timeStr
  990. end
  991. --[[
  992. @param:second
  993. @retuen **
  994. ]]
  995. function TimeUtil:timeConvertAct(second)
  996. local s=second%60
  997. local m = math.floor(second/60);
  998. local min = m % 60
  999. if hide_second then
  1000. min = 0
  1001. end
  1002. local str = string.format("%02d",min) .. "m"..string.format("%02d",s) .. "s";
  1003. local hour
  1004. local d
  1005. if m >= 60 then
  1006. local h = math.floor(m/60);
  1007. str = string.format("%02d",h) .. "h" .. str;
  1008. elseif m > 0 and m < 60 then
  1009. str = "00h"..string.format("%02d",m) .. "m" .. string.format("%02d",s) .. "s"
  1010. else
  1011. str = "00h00m"..string.format("%02d",s) .. "s"
  1012. end
  1013. return str
  1014. end
  1015. --N(0-24)--获取今日N点的时间戳
  1016. function TimeUtil:IsToday( time )
  1017. local now=self:getServerTime()
  1018. local now_date = os.date("*t", now)
  1019. local date = os.date("*t", time)
  1020. return now_date.year == date.year and now_date.month == date.month and now_date.day == date.day
  1021. end
  1022. --[[
  1023. @param:second
  1024. @00:00:00
  1025. @retuen 00:00:00
  1026. ]]
  1027. function TimeUtil:timeConvert33(second)
  1028. local s=second%60
  1029. local min = math.floor(second/60)%60
  1030. local hour = math.floor(second/3600)
  1031. local str = string.format("%02d:%02d:%02d",hour,min,s)
  1032. return str
  1033. end
  1034. --获取当前的时,分,秒
  1035. function TimeUtil:getCurHourMinSce()
  1036. local time = self:getServerTime( )
  1037. local date = os.date("*t", time)
  1038. return date.hour,date.min,date.s
  1039. end