Files
test-all/lua-test/lua/base/TimeUtil.lua
2024-12-10 16:52:17 +08:00

277 lines
8.9 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by zhangkai.
--- DateTime: 2024/8/22 下午8:19
---
TimeUtil = {}
--- 将秒时间戳转换为日期表
function TimeUtil.timeToDate(timestamp)
return os.date("*t", timestamp)
end
--- 比较两个日期是否相同
function TimeUtil.datesIsSame(date1, date2)
return date1.year == date2.year and
date1.month == date2.month and
date1.day == date2.day
end
--- 将秒时间戳转换为%Y-%m-%d %H:%M:%S
function TimeUtil.timeFormat(sec, format)
format = format or "%Y-%m-%d %H:%M:%S"
return os.date(format, sec)
end
--- 判断两个秒时间戳是否跨天
function TimeUtil.isSameDay(timestampA, timestampB)
local dateA = TimeUtil.timeToDate(timestampA)
local dateB = TimeUtil.timeToDate(timestampB)
return TimeUtil.datesIsSame(dateA, dateB)
end
--- 判断两个 毫秒 时间戳是否跨天
function TimeUtil.isSameDay4Millis(timestampA, timestampB)
return TimeUtil.isSameDay(
math.floor(timestampA / 1000),
math.floor(timestampB / 1000)
)
end
--- 比较两个日期是否是同一个周
---@param stampA number 时间戳毫秒
---@param stampB number 时间戳毫秒
function TimeUtil.isSameWeek4Millis(stampA, stampB)
return TimeUtil.isSameWeek(
math.floor(stampA / 1000),
math.floor(stampB / 1000)
)
end
--- 比较两个日期是否是同一个周
---@param stampA number 时间戳秒
---@param stampB number 时间戳秒
function TimeUtil.isSameWeek(stampA, stampB)
-- 检查时间戳是否为空
if not stampA or not stampB then
return false
end
-- 转换时间戳为日期表
local dateA = os.date("*t", stampA)
local dateB = os.date("*t", stampB)
-- 获取每个时间戳对应的一年的第几周
-- 注意os.date("*t") 返回的表中没有直接提供周数,需要手动计算
local weekA = os.date("%W", stampA) + 1 -- %W 返回一年中的第几周从0开始计数
local weekB = os.date("%W", stampB) + 1
-- 获取每个时间戳对应的年份
local yearA = dateA.year
local yearB = dateB.year
-- 比较年份和周数
return yearA == yearB and weekA == weekB
end
--- 比较两个日期是否是同一个月
---@param stampA number 时间戳毫秒
---@param stampB number 时间戳毫秒
function TimeUtil.isSameMonth4Millis(stampA, stampB)
return TimeUtil.isSameMonth(
math.floor(stampA / 1000),
math.floor(stampB / 1000)
)
end
--- 比较两个日期是否是同一个月
---@param stampA number 时间戳秒
---@param stampB number 时间戳秒
function TimeUtil.isSameMonth(stampA, stampB)
-- 检查时间戳是否为空
if not stampA or not stampB then
return false
end
-- 转换时间戳为日期表
local dateA = os.date("*t", stampA)
local dateB = os.date("*t", stampB)
-- 获取每个时间戳对应的年份和月份
local yearA = dateA.year
local monthA = dateA.month
local yearB = dateB.year
local monthB = dateB.month
-- 比较年份和月份
return yearA == yearB and monthA == monthB
end
--- 判断两个时间戳是否在24小时之内
function TimeUtil.within24hours(timestamp1, timestamp2)
local diff = os.difftime(timestamp2, timestamp1)
return diff <= 24 * 60 * 60
end
--判断当前时间是否是每天的凌晨 主要用于每天凌晨的服务器数据刷新
function TimeUtil.isDayZero(seconds)
local now = TimeUtil.timeToDate(seconds)
return now.hour == 0 and now.min == 0 and now.sec >= 0
end
--判断当前时间是否是周一的凌晨 主要用于每周凌晨的服务器数据刷新
function TimeUtil.isMondayZero(seconds)
local now = TimeUtil.timeToDate(seconds)
return now.wday == 1 and now.hour == 0 and now.min == 0 and now.sec >= 0
end
--判断当前时间是否是每月1号的凌晨 主要用于每月凌晨的服务器数据刷新
function TimeUtil.isMonthZero(seconds)
local now = TimeUtil.timeToDate(seconds)
return now.day == 1 and now.hour == 0 and now.min == 0 and now.sec >= 0
end
--- 判断两个秒时间戳是否同一小时
function TimeUtil.isSameHour4Sec(timestampA, timestampB)
local dateA = TimeUtil.timeToDate(timestampA)
local dateB = TimeUtil.timeToDate(timestampB)
return TimeUtil.isSameHour(dateA, dateB)
end
--- 比较两个日期是否同一小时
function TimeUtil.isSameHour(dateA, dateB)
return dateA.year == dateB.year and
dateA.month == dateB.month and
dateA.day == dateB.day and
dateA.hour == dateB.hour
end
--- 根据秒时间戳获取年月日 00:00:00
function TimeUtil.creteTimeDayStart4Sec(sec)
local date = TimeUtil.timeToDate(sec)
return TimeUtil.creteTimeDayStart(date.year, date.month, date.day)
end
--- 获取年月日 00:00:00 时间戳
function TimeUtil.creteTimeDayStart(year, month, day)
return os.time({ year = year, month = month, day = day, hour = 0, min = 0, sec = 0 })
end
--- 结束时间就是23:59:59 时间戳
function TimeUtil.creteTimeDayEnd4Sec(sec)
--把指定的时间,添加指定天数的秒后转行成那一天结束时间
local date = TimeUtil.timeToDate(sec)
return TimeUtil.creteTimeDayEnd(date.year, date.month, date.day)
end
--- 获取年月日结束时间就是23:59:59 时间戳
function TimeUtil.creteTimeDayEnd(year, month, day)
return os.time({ year = year, month = month, day = day, hour = 23, min = 59, sec = 59 })
end
--- 计算两个日期之间的天数差,如果返回0表示购买当天
function TimeUtil.diffDays(sec1, sec2)
local timestamp1 = TimeUtil.creteTimeDayStart4Sec(sec1)
local timestamp2 = TimeUtil.creteTimeDayStart4Sec(sec2)
-- 计算两个时间戳之间的差值
local difference = math.abs(timestamp2 - timestamp1)
-- 将差值转换成天数
local days = difference / (24 * 60 * 60)
return math.floor(days)
end
--- 每天按照某个时间刷新
---timestampA 当前时间
---timestampB 上次时间
---hour 几点刷新
function TimeUtil.dayFlush(timestampA, timestampB, hour)
local timeDiff = timestampA - timestampB
local oneDay = 24 * 60 * 60
if timeDiff >= oneDay then
return true
end
local dateA = TimeUtil.timeToDate(timestampA)
local dateB = TimeUtil.timeToDate(timestampB)
if dateA.day ~= dateB.day then
if dateB.hour >= hour and dateA.hour < hour then
return false
end
return true
end
if dateA.hour >= hour and dateB.hour < hour then
return true
end
return false
end
--- 是否为同一天(以指定的小时为刷新节点)
---t1 当前时间(秒)
---t2 上次时间(秒)
---n 刷新节点(24小时制,不填默认0点)
function TimeUtil.isSameDayWithNum(t1, t2, n)
local diff = string.tonumber(n) * 60 * 60
return TimeUtil.isSameDay(tonumber(t1) - diff, tonumber(t2) - diff)
end
---获取本周周一的凌晨时间戳
---@param sec number 时间戳秒
function TimeUtil.monday(sec)
-- 获取当前时间
local date = TimeUtil.timeToDate(sec)
--print("date", sec, date.wday)
-- 计算从周日到今天的天数差,注意 Lua 的 week 中 Sunday 是 1Monday 是 2
local daysToMonday = date.wday > 1 and date.wday - 2 or 6
-- 设置时间为本周一的零点
date.hour = 0
date.min = 0
date.sec = 0
date.day = date.day - daysToMonday
-- 计算本周一的时间戳
return os.time(date)
end
---当前时间追加多少天
---@param sec number 时间戳秒
---@param change number 增加的天数
function TimeUtil.addDay(sec, change)
-- 获取当前时间
local date = TimeUtil.timeToDate(sec)
date.day = date.day + change
-- 计算本周一的时间戳
return os.time(date)
end
---当前时间追加多少天 的开始时间 00:00:00
---@param sec number 时间戳秒
---@param change number 增加的天数
function TimeUtil.addDayStart(sec, change)
-- 获取当前时间
local date = TimeUtil.timeToDate(sec)
date.day = date.day + change
date.sec = 0
date.min = 0
date.hour = 0
-- 计算本周一的时间戳
return os.time(date)
end
---当前时间追加多少天 的结束时间 23:59:59
---@param sec number 时间戳秒
---@param change number 增加的天数
function TimeUtil.addDayEnd(sec, change)
-- 获取当前时间
local date = TimeUtil.timeToDate(sec)
date.day = date.day + change
date.sec = 59
date.min = 59
date.hour = 23
-- 计算本周一的时间戳
return os.time(date)
end
--print("dd", TimeUtil.timeFormat(1730540988), TimeUtil.monday(1730540988), TimeUtil.timeFormat(TimeUtil.monday(1730540988)))
--print("dd", TimeUtil.timeFormat(1730600988), TimeUtil.monday(1730600988), TimeUtil.timeFormat(TimeUtil.monday(1730600988)))
--
--print("dd", TimeUtil.timeFormat(os.time()), TimeUtil.monday(os.time()), TimeUtil.timeFormat(TimeUtil.monday(os.time())))
--
--print("dd", TimeUtil.addDayEnd(os.time(), 3), TimeUtil.timeFormat(TimeUtil.addDayEnd(os.time(), 3)))