123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- ---
- --- 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 是 1,Monday 是 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
- --- 获取某一天凌晨
- function TimeUtil.earlyOneMorning(year,month,day)
- return os.time({ year = tonumber(year),
- month = tonumber(month),
- day = tonumber(day),
- hour = 0,
- min = 0,
- sec = 0
- })
- end
- --- 获取某一天午夜
- function TimeUtil.midnightOneDay(year,month,day)
- return os.time({ year = tonumber(year),
- month = tonumber(month),
- day = tonumber(day),
- hour = 23,
- min = 59,
- sec = 59
- })
- end
- --- 根据字符串事件获取对应的秒时间戳
- --- yyyy-MM-dd HH:mm:ss
- function TimeUtil.getSec(time_str)
- local year, month, day, hour, min, sec = time_str:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
- local time_table = {
- year = tonumber(year),
- month = tonumber(month),
- day = tonumber(day),
- hour = tonumber(hour),
- min = tonumber(min),
- sec = tonumber(sec),
- }
- return os.time(time_table)
- end
- --- 根据字符串事件获取对应时间凌晨时间戳
- --- yyyy-MM-dd HH:mm:ss
- function TimeUtil.getDawnSec(time_str)
- local year, month, day, hour, min, sec = time_str:match("(%d+)-(%d+)-(%d+) (%d+):(%d+):(%d+)")
- local time_table = {
- year = tonumber(year),
- month = tonumber(month),
- day = tonumber(day),
- hour = 0,
- min = 0,
- sec = 0,
- }
- return os.time(time_table)
- end
|