LuaTimerMgr.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. local LuaTimerMgr = class("LuaTimerMgr", function()
  2. return TimerManager.Instance
  3. end)
  4. local frameCount = 0
  5. local curServerTimeStamp
  6. local curServerTimeSecond --通过心跳包获取的时间,单位是秒
  7. function LuaTimerMgr:ctor()
  8. self.OneMinuteSeconds = 60
  9. self.OneDaySeconds = 24*3600
  10. end
  11. function LuaTimerMgr:GetOneDaySeconds()
  12. return self.OneDaySeconds
  13. end
  14. function LuaTimerMgr:AddLuaTimer(inter, times, cb, params)
  15. return self:AddTimer(inter, times, false, cb, params)
  16. end
  17. function LuaTimerMgr:SyncServerTime(serverTime)
  18. curServerTimeStamp = serverTime
  19. curServerTimeSecond = serverTime / 1000
  20. curServerTimeSecond = type(curServerTimeSecond) == "number" and curServerTimeSecond or #curServerTimeSecond
  21. if serverTime ~= nil then
  22. self:SetServerTime(serverTime)
  23. end
  24. end
  25. function LuaTimerMgr:Update()
  26. if curServerTimeStamp ~= nil then
  27. curServerTimeStamp = curServerTimeStamp + Time.unscaledDeltaTime*1000
  28. curServerTimeSecond = curServerTimeSecond + Time.unscaledDeltaTime
  29. end
  30. end
  31. function LuaTimerMgr:CurLuaServerTime()
  32. return curServerTimeStamp
  33. end
  34. function LuaTimerMgr:GetTimeSecond()
  35. return math.floor(curServerTimeSecond or 0)
  36. end
  37. function LuaTimerMgr:CurServerTime()
  38. return self:LuaGetServerTime()
  39. end
  40. --秒时间戳 转为小时
  41. function LuaTimerMgr:Getstamp2TimeH(Format,Seconds)
  42. local LocalSeconds = type(Seconds) == "number" and Seconds or #Seconds
  43. return os.date(Format,LocalSeconds)
  44. end
  45. --日期转 秒时间戳
  46. function LuaTimerMgr:GetTime2StampS(Date)
  47. return os.time({day=Date.day, month=Date.month, year=Date.year, hour=Date.hour, minute=Date.minute, second=Date.second})
  48. end
  49. function LuaTimerMgr:ParseSeconds2Time(seconds)
  50. return string.format("%02d:%02d:%02d", seconds/(60*60), seconds/60%60, seconds%60)
  51. end
  52. function LuaTimerMgr:ParseTimeStamp2HS(time)
  53. if type(time) == "userdata" then
  54. time = time/1000
  55. time = #time
  56. end
  57. return self:ParseTimeStamp2Format(time, "%H:%M")
  58. end
  59. function LuaTimerMgr:ParseTimeStamp2Format(time, format)
  60. if type(time) == "userdata" then
  61. time = time/1000
  62. time = #time
  63. end
  64. return os.date(format,time);
  65. end
  66. function LuaTimerMgr:ParseEndTimeStamp2Time(endTime)
  67. local s = self:GetRemainSeconds(endTime)
  68. return self:ParseSeconds2Time(s)
  69. end
  70. function LuaTimerMgr:ParseEndTimeStamp2TimeWithUInt64(endTime)
  71. local endSeconds = endTime*0.001
  72. return self:ParseEndTimeStamp2Time(endSeconds)
  73. end
  74. function LuaTimerMgr:GetRemainSecondsWithUInt64(endTime, needAbs)
  75. local endSeconds = endTime/1000
  76. return self:GetRemainSeconds(endSeconds, needAbs)
  77. end
  78. function LuaTimerMgr:GetRemainSeconds(endTime, needAbs)
  79. local currentTime = self:CurLuaServerTime()/1000
  80. local CoolingTime = endTime - currentTime
  81. CoolingTime = type(CoolingTime) == "number" and CoolingTime or #CoolingTime
  82. if needAbs then
  83. CoolingTime = math.abs(CoolingTime)
  84. end
  85. CoolingTime = math.max(0, CoolingTime)
  86. return CoolingTime
  87. end
  88. function LuaTimerMgr:Get2TimeStampDeltaSeconds(time1, time2)
  89. local CoolingTime = (time2 - time1)/1000
  90. CoolingTime = type(CoolingTime) == "number" and CoolingTime or #CoolingTime
  91. CoolingTime = math.abs(CoolingTime)
  92. CoolingTime = math.max(0, CoolingTime)
  93. return CoolingTime
  94. end
  95. function LuaTimerMgr:TransSeconds2Minutes(seconds)
  96. return seconds/self.OneMinuteSeconds
  97. end
  98. function LuaTimerMgr:PrintTimeStamp2DayStr(time)
  99. local time1 = time/1000
  100. LogError(os.date("%Y-%m-%d %H:%M:%S", #time1))
  101. end
  102. function LuaTimerMgr:TwoTimeStampBetweenOneDay(time, time1)
  103. if type(time) == "userdata" then
  104. time = time/1000
  105. time = #time
  106. end
  107. if type(time1) == "userdata" then
  108. time1 = time1/1000
  109. time1 = #time1
  110. end
  111. return self:TwoTimeSecondBetweenOneDay(time, time1)
  112. end
  113. function LuaTimerMgr:TwoTimeSecondBetweenOneDay(time, time1)
  114. if type(time) == "userdata" then
  115. time = time/1000
  116. time = #time
  117. end
  118. if type(time1) == "userdata" then
  119. time1 = time1/1000
  120. time1 = #time1
  121. end
  122. local str = os.date("%Y-%m-%d", time)
  123. local str1 = os.date("%Y-%m-%d", time1)
  124. return str == str1
  125. end
  126. function LuaTimerMgr:IsSomeFrameCount()
  127. if frameCount >= Time.frameCount then
  128. return true
  129. end
  130. frameCount = Time.frameCount
  131. return false
  132. end
  133. function LuaTimerMgr:Destroy()
  134. if tolua.getpeer(self) ~= nil then
  135. tolua.setpeer(self, nil)
  136. end
  137. end
  138. return LuaTimerMgr