TimeUtil.lua 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by zhangkai.
  4. --- DateTime: 2024/8/22 下午8:19
  5. ---
  6. TimeUtil = {}
  7. --- 将秒时间戳转换为日期表
  8. function TimeUtil.timeToDate(timestamp)
  9. return os.date("*t", timestamp)
  10. end
  11. --- 比较两个日期是否相同
  12. function TimeUtil.datesIsSame(date1, date2)
  13. return date1.year == date2.year and
  14. date1.month == date2.month and
  15. date1.day == date2.day
  16. end
  17. --- 将秒时间戳转换为%Y-%m-%d %H:%M:%S
  18. function TimeUtil.timeFormat(sec, format)
  19. format = format or "%Y-%m-%d %H:%M:%S"
  20. return os.date(format, sec)
  21. end
  22. --- 判断两个秒时间戳是否跨天
  23. function TimeUtil.isSameDay(timestampA, timestampB)
  24. local dateA = TimeUtil.timeToDate(timestampA)
  25. local dateB = TimeUtil.timeToDate(timestampB)
  26. return TimeUtil.datesIsSame(dateA, dateB)
  27. end
  28. --- 判断两个 毫秒 时间戳是否跨天
  29. function TimeUtil.isSameDay4Millis(timestampA, timestampB)
  30. return TimeUtil.isSameDay(
  31. math.floor(timestampA / 1000),
  32. math.floor(timestampB / 1000)
  33. )
  34. end
  35. --- 比较两个日期是否是同一个周
  36. ---@param stampA number 时间戳毫秒
  37. ---@param stampB number 时间戳毫秒
  38. function TimeUtil.isSameWeek4Millis(stampA, stampB)
  39. return TimeUtil.isSameWeek(
  40. math.floor(stampA / 1000),
  41. math.floor(stampB / 1000)
  42. )
  43. end
  44. --- 比较两个日期是否是同一个周
  45. ---@param stampA number 时间戳秒
  46. ---@param stampB number 时间戳秒
  47. function TimeUtil.isSameWeek(stampA, stampB)
  48. -- 检查时间戳是否为空
  49. if not stampA or not stampB then
  50. return false
  51. end
  52. -- 转换时间戳为日期表
  53. local dateA = os.date("*t", stampA)
  54. local dateB = os.date("*t", stampB)
  55. -- 获取每个时间戳对应的一年的第几周
  56. -- 注意:os.date("*t") 返回的表中没有直接提供周数,需要手动计算
  57. local weekA = os.date("%W", stampA) + 1 -- %W 返回一年中的第几周,从0开始计数
  58. local weekB = os.date("%W", stampB) + 1
  59. -- 获取每个时间戳对应的年份
  60. local yearA = dateA.year
  61. local yearB = dateB.year
  62. -- 比较年份和周数
  63. return yearA == yearB and weekA == weekB
  64. end
  65. --- 比较两个日期是否是同一个月
  66. ---@param stampA number 时间戳毫秒
  67. ---@param stampB number 时间戳毫秒
  68. function TimeUtil.isSameMonth4Millis(stampA, stampB)
  69. return TimeUtil.isSameMonth(
  70. math.floor(stampA / 1000),
  71. math.floor(stampB / 1000)
  72. )
  73. end
  74. --- 比较两个日期是否是同一个月
  75. ---@param stampA number 时间戳秒
  76. ---@param stampB number 时间戳秒
  77. function TimeUtil.isSameMonth(stampA, stampB)
  78. -- 检查时间戳是否为空
  79. if not stampA or not stampB then
  80. return false
  81. end
  82. -- 转换时间戳为日期表
  83. local dateA = os.date("*t", stampA)
  84. local dateB = os.date("*t", stampB)
  85. -- 获取每个时间戳对应的年份和月份
  86. local yearA = dateA.year
  87. local monthA = dateA.month
  88. local yearB = dateB.year
  89. local monthB = dateB.month
  90. -- 比较年份和月份
  91. return yearA == yearB and monthA == monthB
  92. end
  93. --- 判断两个时间戳是否在24小时之内
  94. function TimeUtil.within24hours(timestamp1, timestamp2)
  95. local diff = os.difftime(timestamp2, timestamp1)
  96. return diff <= 24 * 60 * 60
  97. end
  98. --判断当前时间是否是每天的凌晨 主要用于每天凌晨的服务器数据刷新
  99. function TimeUtil.isDayZero(seconds)
  100. local now = TimeUtil.timeToDate(seconds)
  101. return now.hour == 0 and now.min == 0 and now.sec >= 0
  102. end
  103. --判断当前时间是否是周一的凌晨 主要用于每周凌晨的服务器数据刷新
  104. function TimeUtil.isMondayZero(seconds)
  105. local now = TimeUtil.timeToDate(seconds)
  106. return now.wday == 1 and now.hour == 0 and now.min == 0 and now.sec >= 0
  107. end
  108. --判断当前时间是否是每月1号的凌晨 主要用于每月凌晨的服务器数据刷新
  109. function TimeUtil.isMonthZero(seconds)
  110. local now = TimeUtil.timeToDate(seconds)
  111. return now.day == 1 and now.hour == 0 and now.min == 0 and now.sec >= 0
  112. end
  113. --- 判断两个秒时间戳是否同一小时
  114. function TimeUtil.isSameHour4Sec(timestampA, timestampB)
  115. local dateA = TimeUtil.timeToDate(timestampA)
  116. local dateB = TimeUtil.timeToDate(timestampB)
  117. return TimeUtil.isSameHour(dateA, dateB)
  118. end
  119. --- 比较两个日期是否同一小时
  120. function TimeUtil.isSameHour(dateA, dateB)
  121. return dateA.year == dateB.year and
  122. dateA.month == dateB.month and
  123. dateA.day == dateB.day and
  124. dateA.hour == dateB.hour
  125. end
  126. --- 根据秒时间戳获取年月日 00:00:00
  127. function TimeUtil.creteTimeDayStart4Sec(sec)
  128. local date = TimeUtil.timeToDate(sec)
  129. return TimeUtil.creteTimeDayStart(date.year, date.month, date.day)
  130. end
  131. --- 获取年月日 00:00:00 时间戳
  132. function TimeUtil.creteTimeDayStart(year, month, day)
  133. return os.time({ year = year, month = month, day = day, hour = 0, min = 0, sec = 0 })
  134. end
  135. --- 结束时间,就是23:59:59 时间戳
  136. function TimeUtil.creteTimeDayEnd4Sec(sec)
  137. --把指定的时间,添加指定天数的秒后转行成那一天结束时间
  138. local date = TimeUtil.timeToDate(sec)
  139. return TimeUtil.creteTimeDayEnd(date.year, date.month, date.day)
  140. end
  141. --- 获取年月日结束时间,就是23:59:59 时间戳
  142. function TimeUtil.creteTimeDayEnd(year, month, day)
  143. return os.time({ year = year, month = month, day = day, hour = 23, min = 59, sec = 59 })
  144. end
  145. --- 计算两个日期之间的天数差,如果返回0表示购买当天
  146. function TimeUtil.diffDays(sec1, sec2)
  147. local timestamp1 = TimeUtil.creteTimeDayStart4Sec(sec1)
  148. local timestamp2 = TimeUtil.creteTimeDayStart4Sec(sec2)
  149. -- 计算两个时间戳之间的差值
  150. local difference = math.abs(timestamp2 - timestamp1)
  151. -- 将差值转换成天数
  152. local days = difference / (24 * 60 * 60)
  153. return math.floor(days)
  154. end
  155. --- 每天按照某个时间刷新
  156. ---timestampA 当前时间
  157. ---timestampB 上次时间
  158. ---hour 几点刷新
  159. function TimeUtil.dayFlush(timestampA, timestampB, hour)
  160. local timeDiff = timestampA - timestampB
  161. local oneDay = 24 * 60 * 60
  162. if timeDiff >= oneDay then
  163. return true
  164. end
  165. local dateA = TimeUtil.timeToDate(timestampA)
  166. local dateB = TimeUtil.timeToDate(timestampB)
  167. if dateA.day ~= dateB.day then
  168. if dateB.hour >= hour and dateA.hour < hour then
  169. return false
  170. end
  171. return true
  172. end
  173. if dateA.hour >= hour and dateB.hour < hour then
  174. return true
  175. end
  176. return false
  177. end
  178. --- 是否为同一天(以指定的小时为刷新节点)
  179. ---t1 当前时间(秒)
  180. ---t2 上次时间(秒)
  181. ---n 刷新节点(24小时制,不填默认0点)
  182. function TimeUtil.isSameDayWithNum(t1, t2, n)
  183. local diff = string.tonumber(n) * 60 * 60
  184. return TimeUtil.isSameDay(tonumber(t1) - diff, tonumber(t2) - diff)
  185. end
  186. ---获取本周周一的凌晨时间戳
  187. ---@param sec number 时间戳秒
  188. function TimeUtil.monday(sec)
  189. -- 获取当前时间
  190. local date = TimeUtil.timeToDate(sec)
  191. --print("date", sec, date.wday)
  192. -- 计算从周日到今天的天数差,注意 Lua 的 week 中 Sunday 是 1,Monday 是 2
  193. local daysToMonday = date.wday > 1 and date.wday - 2 or 6
  194. -- 设置时间为本周一的零点
  195. date.hour = 0
  196. date.min = 0
  197. date.sec = 0
  198. date.day = date.day - daysToMonday
  199. -- 计算本周一的时间戳
  200. return os.time(date)
  201. end
  202. ---当前时间追加多少天
  203. ---@param sec number 时间戳秒
  204. ---@param change number 增加的天数
  205. function TimeUtil.addDay(sec, change)
  206. -- 获取当前时间
  207. local date = TimeUtil.timeToDate(sec)
  208. date.day = date.day + change
  209. -- 计算本周一的时间戳
  210. return os.time(date)
  211. end
  212. ---当前时间追加多少天 的开始时间 00:00:00
  213. ---@param sec number 时间戳秒
  214. ---@param change number 增加的天数
  215. function TimeUtil.addDayStart(sec, change)
  216. -- 获取当前时间
  217. local date = TimeUtil.timeToDate(sec)
  218. date.day = date.day + change
  219. date.sec = 0
  220. date.min = 0
  221. date.hour = 0
  222. -- 计算本周一的时间戳
  223. return os.time(date)
  224. end
  225. ---当前时间追加多少天 的结束时间 23:59:59
  226. ---@param sec number 时间戳秒
  227. ---@param change number 增加的天数
  228. function TimeUtil.addDayEnd(sec, change)
  229. -- 获取当前时间
  230. local date = TimeUtil.timeToDate(sec)
  231. date.day = date.day + change
  232. date.sec = 59
  233. date.min = 59
  234. date.hour = 23
  235. -- 计算本周一的时间戳
  236. return os.time(date)
  237. end
  238. --- 获取某一天凌晨
  239. function TimeUtil.earlyOneMorning(year,month,day)
  240. return os.time({ year = tonumber(year),
  241. month = tonumber(month),
  242. day = tonumber(day),
  243. hour = 0,
  244. min = 0,
  245. sec = 0
  246. })
  247. end
  248. --- 获取某一天午夜
  249. function TimeUtil.midnightOneDay(year,month,day)
  250. return os.time({ year = tonumber(year),
  251. month = tonumber(month),
  252. day = tonumber(day),
  253. hour = 23,
  254. min = 59,
  255. sec = 59
  256. })
  257. end
  258. --- 根据字符串事件获取对应的秒时间戳
  259. --- yyyy-MM-dd HH:mm:ss
  260. function TimeUtil.getSec(time_str)
  261. local year, month, day, hour, min, sec = time_str:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
  262. local time_table = {
  263. year = tonumber(year),
  264. month = tonumber(month),
  265. day = tonumber(day),
  266. hour = tonumber(hour),
  267. min = tonumber(min),
  268. sec = tonumber(sec),
  269. }
  270. return os.time(time_table)
  271. end
  272. --- 根据字符串事件获取对应时间凌晨时间戳
  273. --- yyyy-MM-dd HH:mm:ss
  274. function TimeUtil.getDawnSec(time_str)
  275. local year, month, day, hour, min, sec = time_str:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
  276. local time_table = {
  277. year = tonumber(year),
  278. month = tonumber(month),
  279. day = tonumber(day),
  280. hour = 0,
  281. min = 0,
  282. sec = 0,
  283. }
  284. return os.time(time_table)
  285. end