Time_Tips.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ---@class Time
  2. Time = {}
  3. local this = Time
  4. local unity_time = CS.UnityEngine.Time
  5. local inited = false
  6. local timer = 0
  7. local openServerDay
  8. function Time.Update(time, unscaledTime)
  9. if not inited then
  10. rawset(this, "deltaTime", unity_time.deltaTime)
  11. rawset(this, "unscaledDeltaTime", unity_time.unscaledDeltaTime)
  12. rawset(this, "time", unity_time.time)
  13. rawset(this, "unscaledTime", unity_time.unscaledTime)
  14. --rawset(this, "timeSinceLevelLoad", unity_time.timeSinceLevelLoad)
  15. rawset(this, "frameCount", unity_time.frameCount)
  16. --rawset(this, "renderedFrameCount", unity_time.renderedFrameCount)
  17. inited = true
  18. else
  19. this.deltaTime = time - this.time
  20. this.unscaledDeltaTime = unscaledTime - this.unscaledTime
  21. this.time = time
  22. this.unscaledTime = unscaledTime
  23. --this.timeSinceLevelLoad = this.timeSinceLevelLoad + this.deltaTime
  24. this.frameCount = this.frameCount + 1
  25. --this.renderedFrameCount = this.renderedFrameCount + 1
  26. timer = timer + this.deltaTime
  27. if timer >= 15 then
  28. if LoginManager.EnterRoleRes and LoginManager.EnterRoleRes.openServerTime then
  29. local defTime = Time.GetServerTime() - LoginManager.EnterRoleRes.openServerTime + LoginManager.millisecond
  30. if defTime > 0 then
  31. local day = math.ceil(defTime / 86400000)
  32. if not openServerDay or (openServerDay ~= day) then
  33. openServerDay = day
  34. SL:onLUAEvent(LUA_EVENT_OPEN_SERVER_DAY_CHANGE,openServerDay)
  35. end
  36. end
  37. end
  38. timer = 0
  39. end
  40. end
  41. end
  42. local serverTime = 0
  43. local recvServerTime = 0
  44. -- 单位毫秒
  45. function Time.SetServerTime(time)
  46. serverTime = time
  47. recvServerTime = this.unscaledTime
  48. CS.KingML.DataManager.SetServerTime(time)
  49. end
  50. -- 单位毫秒
  51. function Time.GetServerTime()
  52. return serverTime + math.floor((this.unscaledTime - recvServerTime) * 1000)
  53. end
  54. --时分秒
  55. function Time.FormatTime(time)
  56. time = math.modf(time / 1000)
  57. local hour = math.modf(time / 3600)
  58. local minute = math.modf(time % 3600 / 60)
  59. local second = time % 60
  60. local formatTime = string.format("%02s:%02s:%02s", hour, minute, second)
  61. return formatTime
  62. end
  63. --时分
  64. function Time.FormatTimeHMString(time)
  65. local hour = math.modf(time / 3600)
  66. local minute = math.modf(time % 3600 / 60)
  67. local formatTime = string.format("%02s小时%02s分", hour, minute)
  68. return formatTime
  69. end
  70. --分秒
  71. function Time.FormatTimeMS(time)
  72. time = math.modf(time / 1000)
  73. local minute = math.modf(time / 60)
  74. local second = time % 60
  75. local fmtTime = string.format("%02s:%02s", minute, second)
  76. return fmtTime
  77. end
  78. --同上但艺术字
  79. function Time.FormatTimeMSForArt(time)
  80. time = math.modf(time / 1000)
  81. local minute = math.modf(time / 60)
  82. local second = time % 60
  83. local fmtTime = string.format("%02sa%02s", minute, second)
  84. return fmtTime
  85. end
  86. --分秒
  87. function Time.ShowFormatTimeMS(time)
  88. time = math.modf(time / 1000)
  89. local minute = math.modf(time / 60)
  90. local second = time % 60
  91. if minute > 0 then
  92. local s = "%2s分%2s秒"
  93. if minute <= 9 then
  94. s = "%02s分%2s秒"
  95. end
  96. return string.format(s, minute, second)
  97. end
  98. if minute == 0 then
  99. return string.format("%2s秒", second)
  100. end
  101. end
  102. --时分秒毫秒
  103. function Time.ShowFormatTimeHMSM(time)
  104. local time1 = math.modf(time / 1000)
  105. local hour = math.modf(time1 / 3600 )
  106. local minute = math.modf(time1 % 3600/60)
  107. local second = time1 % 60
  108. local millisecond = math.floor(time % 1000 / 10)
  109. return string.format("%02s:%02s:%02s:%02s", hour,minute, second, millisecond)
  110. end
  111. --分秒毫秒
  112. function Time.ShowFormatTimeMSM(time)
  113. local time1 = math.modf(time / 1000)
  114. local minute = math.modf(time1 / 60)
  115. local second = time1 % 60
  116. local millisecond = math.floor(time % 1000 / 10)
  117. return string.format("%02s:%02s:%02s", minute, second, millisecond)
  118. end
  119. --分钟
  120. function Time.FormatTimeMM(time, bCeil)
  121. time = math.modf(time / 1000)
  122. local m = (bCeil == true) and math.ceil(time / 60) or (time // 60)
  123. return string.format("%2s分钟", m);
  124. end
  125. --(分)秒
  126. function Time.FormatTimeAutoMS(time)
  127. time = math.modf(time / 1000)
  128. local minute = math.modf(time / 60)
  129. local second = time % 60
  130. local fmtTime = minute > 0 and string.format("%02s:%02s", minute, second)
  131. or string.format("%s", second)
  132. return fmtTime
  133. end
  134. --单位毫秒
  135. function Time.FormatTimeHMS(time)
  136. time = time // 1000
  137. local s = time % 60
  138. local m = time // 60 % 60
  139. local h = time // 3600 % 24
  140. return string.format("%02s:%02s:%02s", h, m, s);
  141. end
  142. --秒(剩余秒数倒计时)
  143. function Time.FormatTimeDHMS(time)
  144. local s = time % 60
  145. local m = time // 60 % 60
  146. local h = time // 3600 % 24
  147. local d = time // 86400
  148. return string.format("%02s天%02s小时%02s分钟%02s秒", d, h, m, s);
  149. end
  150. ---时分秒用冒号连接
  151. function Time.FormatTimeDHMSFormColon(time)
  152. local s = math.floor(time % 60)
  153. local m = math.floor(time // 60 % 60)
  154. local h = math.floor(time // 3600 % 24)
  155. local d = time // 86400
  156. if d <= 0 then
  157. return string.format("%02s:%02s:%02s", h, m, s);
  158. else
  159. return string.format("%d天%02s:%02s:%02s", d, h, m, s);
  160. end
  161. end
  162. --秒(剩余秒数倒计时)
  163. function Time.FormatTimeDHM(time)
  164. local s = time % 60
  165. local m = time // 60 % 60
  166. local h = time // 3600 % 24
  167. local d = time // 86400
  168. if h == 23 and m == 59 and s > 0 then
  169. d = d + 1
  170. m = 0
  171. h = 0
  172. elseif m == 59 and s > 0 then
  173. m = 0
  174. h = h + 1
  175. elseif s > 0 then
  176. m = m + 1
  177. end
  178. return string.format("%02s天%02s小时%02s分钟", d, h, m);
  179. end
  180. --秒(剩余秒数倒计时)
  181. function Time.FormatTimeDH(time)
  182. local h = math.ceil(time // 3600) % 24
  183. local d = math.ceil(time // 86400)
  184. return string.format("%02s天%02s小时", d, h);
  185. end
  186. ---@param time number @时间戳 单位秒
  187. function Time.FormatTimeYMDHMS(time)
  188. return os.date("%Y-%m-%d %H:%M:%S", time)
  189. end
  190. --分钟(剩余秒数倒计时)
  191. function Time.FormatTimeM(time, bCeil)
  192. local m = (bCeil == true) and math.ceil(time / 60) or (time // 60)
  193. return string.format("%02s分钟", m);
  194. end
  195. -- 获得当前时间戳
  196. function Time.GetTimeStamp(year, month, day, hour, minute, second)
  197. return os.time({ day = day, month = month, year = year, hour = hour, min = minute, sec = second })
  198. end
  199. function Time.GetMinute()
  200. return os.date("%M", math.floor(this.GetServerTime() / 1000))
  201. end
  202. function Time.GetSecond()
  203. return os.date("%S", math.floor(this.GetServerTime() / 1000))
  204. end
  205. function Time.GetHour()
  206. return os.date("%H", math.floor(this.GetServerTime() / 1000))
  207. end
  208. function Time.GetDay()
  209. return os.date("%d", math.floor(this.GetServerTime() / 1000))
  210. end
  211. function Time.GetWeekDay()
  212. return os.date("%w", math.floor(this.GetServerTime() / 1000))
  213. end
  214. function Time.GetYear()
  215. return os.date("%Y", math.floor(this.GetServerTime() / 1000))
  216. end
  217. function Time.GetMonth()
  218. return os.date("%m", math.floor(this.GetServerTime() / 1000))
  219. end
  220. -- 根据时间戳判断是否是同一天
  221. function Time.IsSameDay(stampOne, stampTwo)
  222. local year1, month1, day1 = os.date("%Y", math.floor(stampOne / 1000)), os.date("%m", math.floor(stampOne / 1000)), os.date("%d", math.floor(stampOne / 1000))
  223. local year2, month2, day2 = os.date("%Y", math.floor(stampTwo / 1000)), os.date("%m", math.floor(stampTwo / 1000)), os.date("%d", math.floor(stampTwo / 1000))
  224. return year1 == year2 and month1 == month2 and day1 == day2
  225. end
  226. local m = {
  227. __index = function(t, k)
  228. return unity_time[k]
  229. end,
  230. __newindex = function(t, k, v)
  231. unity_time[k] = v
  232. end,
  233. }
  234. setmetatable(Time, m)
  235. CS.UnityEngine.Time = Time