lua 写一个函数 判断两个时间戳是否在同一周

设计一个 函数 ,用于判断两个时间戳是否在同一周内
  • **参数**:
    • `stampA`:时间戳A。
    • `stampB`:时间戳B。
    • `resetInfo`(可选):重置时间信息,例如每天的12点重置。默认为`hour=0, min=0, sec=0`。
- **实现逻辑**:
    • 首先检查`stampA`和`stampB`是否为空,如果为空则返回`false`。
    • 将时间戳转换为整数。
    • 如果提供了`resetInfo`,则根据`resetInfo`中的小时、分钟和秒数调整时间戳。
    • 使用`getCurrentYearInfo`函数获取两个时间戳的年份和周索引。
    • 比较两个时间戳的年份和周索引,如果相同则返回`true`,否则返回`false`。

可以用于需要根据周来管理任务或活动的场景,例如每周发放奖励、每周统计数据等。

Lua 复制代码
--[[
    @desc 是否是同一周
    @param stampA 时间戳A
    @param stampB 时间戳B
    @param resetInfo[optional] 重置时间。比如12点重置,那么11点和13点就不是同一天,而23点和第二天11点是同一天。默认为hour=0,min=0,sec=0
]]
function TimeUtil:isSameWeek(stampA, stampB, resetInfo)
    if (not stampA) or (not stampB) then
        return false
    end
    stampA = TimeUtil:toInt(stampA)
    stampB = TimeUtil:toInt(stampB)
    if resetInfo then
        local resetSeconds = (resetInfo.hour or 0) * 3600 +
            (resetInfo.minute or 0) * 60 +
            (resetInfo.seconds or 0)
        stampA = stampA - resetSeconds
        stampB = stampB - resetSeconds
    end
    local dateA = TimeUtil:getCurrentYearInfo(stampA)
    local dateB = TimeUtil:getCurrentYearInfo(stampB)
    return dateA.weekIdx == dateB.weekIdx and dateA.year == dateB.year
end

function TimeUtil:getCurrentYearInfo(time_s)
    time_s = time_s or TimeUtil:getServerTime()
    local yearNum = tonumber(TimeUtil:fixTimeZoneFor_LUA_OS_DATE("%y", time_s))  -- [0 - 99  两位数的年份]
    local weekIndex = tonumber(TimeUtil:fixTimeZoneFor_LUA_OS_DATE("%W", time_s))  -- [0 - 52   一年中第几个星期]
    return {
        year = yearNum,
        weekIdx = weekIndex,
    }
end
相关推荐
泽虞1 天前
《Qt应用开发》笔记
linux·开发语言·c++·笔记·qt
专职1 天前
pytest详细教程
开发语言·python·pytest
专职1 天前
pytest+requests+allure生成接口自动化测试报告
开发语言·python·pytest
风起云涌~1 天前
【Java】浅谈ServiceLoader
java·开发语言
那我掉的头发算什么1 天前
【数据结构】优先级队列(堆)
java·开发语言·数据结构·链表·idea
.ZGR.1 天前
C语言:数组的增删查改2.0
c语言·开发语言
漫漫不慢.1 天前
算法练习-二分查找
java·开发语言·算法
掘根1 天前
【Qt】绘图
开发语言·qt
咖啡续命又一天1 天前
python 自动化采集 ChromeDriver 安装
开发语言·python·自动化
huohaiyu1 天前
synchronized (Java)
java·开发语言·安全·synchronized