179 lines
4.6 KiB
Lua
179 lines
4.6 KiB
Lua
DateOBJ = {}
|
||
DateOBJ.__cname = "DateOBJ"
|
||
DateOBJ.__index = DateOBJ
|
||
|
||
|
||
---获取明天0点时间
|
||
---@param days number 默认为明天 从今天起算
|
||
---@param now number 现在时间戳 不是必传
|
||
function DateOBJ.getTomorrowZero(days, now)
|
||
days = days or 1;
|
||
now = now or os.time()
|
||
local nextTime = os.date("*t", now + (86400 * days))
|
||
nextTime.hour = 0
|
||
nextTime.min = 0
|
||
nextTime.sec = 0
|
||
return os.time(nextTime);
|
||
end
|
||
|
||
---获取今天00点的字符串格式化时间
|
||
function DateOBJ.getTodayZero(now)
|
||
now = now or os.time();
|
||
local nextTime = os.date("*t", now)
|
||
nextTime.hour = 0
|
||
nextTime.min = 0
|
||
nextTime.sec = 0
|
||
return os.time(nextTime);
|
||
end
|
||
|
||
---获取本周指定周几0点时间如果小于等于当前周几自动滑到下周
|
||
---@param week number 1-7 周一-周日
|
||
function DateOBJ.getAutoWeekZero(week)
|
||
week = week or 1;
|
||
local now = os.time();
|
||
local w = tonumber(os.date("%w", now));
|
||
if w == 0 then w = 7 end
|
||
if week <= w then
|
||
week = week + 7
|
||
end
|
||
return DateOBJ.getTomorrowZero((week - w), now);
|
||
end
|
||
|
||
---获取本周指定周几0点时间
|
||
---@param week number 1-7 周一-周日
|
||
function DateOBJ.getWeekZero(week)
|
||
week = week or 1;
|
||
local now = os.time();
|
||
local w = tonumber(os.date("%w", now));
|
||
if w == 0 then w = 7 end
|
||
return DateOBJ.getTomorrowZero((week - w), now);
|
||
end
|
||
|
||
---获取下周指定周几0点时间
|
||
---@param week number 1-7 周一-周日
|
||
function DateOBJ.getNextWeekZero(week)
|
||
week = week or 1;
|
||
local now = os.time();
|
||
local w = tonumber(os.date("%w", now));
|
||
if w == 0 then w = 7 end
|
||
return DateOBJ.getTomorrowZero((week - w) + 7, now);
|
||
end
|
||
|
||
---获取月指定时间(精确版, 包括闰月计算)
|
||
---@param day number 指定当月的第几天
|
||
---@param now number 当前时间戳,默认为当前时间
|
||
function DateOBJ.getMonthZero(day, now)
|
||
day = day or 1
|
||
now = now or os.time()
|
||
local monTime = os.date("*t", now)
|
||
|
||
-- 获取当前月份的天数
|
||
local function getDaysInMonth(year, month)
|
||
if month == 2 then
|
||
-- 闰年判断:能被4整除且不能被100整除,或者能被400整除
|
||
if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
|
||
return 29
|
||
else
|
||
return 28
|
||
end
|
||
end
|
||
-- 大月31天,小月30天
|
||
local daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
|
||
return daysInMonth[month]
|
||
end
|
||
|
||
-- 确保指定的天数在当前月份的范围内
|
||
local daysInCurrentMonth = getDaysInMonth(monTime.year, monTime.month)
|
||
if day > daysInCurrentMonth then
|
||
day = daysInCurrentMonth
|
||
elseif day < 1 then
|
||
day = 1
|
||
end
|
||
|
||
monTime.day = day
|
||
monTime.hour = 0
|
||
monTime.min = 0
|
||
monTime.sec = 0
|
||
return os.time(monTime)
|
||
end
|
||
|
||
---获取下月指定时间(精确版, 包括闰月计算)
|
||
---@param day number 指定下月的第几天
|
||
---@param now number 当前时间戳,默认为当前时间
|
||
function DateOBJ.getNextMonthZero(day, now)
|
||
day = day or 1
|
||
now = now or os.time()
|
||
local monTime = os.date("*t", now)
|
||
|
||
-- 获取下个月的月份和年份
|
||
monTime.month = monTime.month + 1
|
||
if monTime.month > 12 then
|
||
monTime.month = 1
|
||
monTime.year = monTime.year + 1
|
||
end
|
||
|
||
-- 获取当前月份的天数
|
||
local function getDaysInMonth(year, month)
|
||
if month == 2 then
|
||
-- 闰年判断:能被4整除且不能被100整除,或者能被400整除
|
||
if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
|
||
return 29
|
||
else
|
||
return 28
|
||
end
|
||
end
|
||
-- 大月31天,小月30天
|
||
local daysInMonth = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
|
||
return daysInMonth[month]
|
||
end
|
||
|
||
-- 确保指定的天数在下个月的范围内
|
||
local daysInNextMonth = getDaysInMonth(monTime.year, monTime.month)
|
||
if day > daysInNextMonth then
|
||
day = daysInNextMonth
|
||
elseif day < 1 then
|
||
day = 1
|
||
end
|
||
|
||
monTime.day = day
|
||
monTime.hour = 0
|
||
monTime.min = 0
|
||
monTime.sec = 0
|
||
return os.time(monTime)
|
||
end
|
||
|
||
---获取准确的年月日的时间戳
|
||
---@param year number 年份
|
||
---@param month number 月份
|
||
---@param day number 天
|
||
---@param hour? number 小时
|
||
---@param min? number 分钟
|
||
---@param sec? number 秒
|
||
function DateOBJ.getDateZero(year, month, day, hour, min, sec)
|
||
year = year or os.date("%Y")
|
||
month = month or os.date("%m")
|
||
day = day or os.date("%d")
|
||
hour = hour or 0
|
||
min = min or 0
|
||
sec = sec or 0
|
||
|
||
-- 确保月份在1-12之间
|
||
if month < 1 then month = 1 end
|
||
if month > 12 then month = 12 end
|
||
|
||
-- 确保天数在1-31之间
|
||
if day < 1 then day = 1 end
|
||
if day > 31 then day = 31 end
|
||
|
||
local dateTable = {
|
||
year = year,
|
||
month = month,
|
||
day = day,
|
||
hour = hour,
|
||
min = min,
|
||
sec = sec
|
||
}
|
||
return os.time(dateTable)
|
||
end
|
||
|
||
return DateOBJ
|