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
相关推荐
xiezhr4 天前
米哈游36岁程序员被曝复工当晚猝死出租屋内
游戏·程序员·游戏开发
郑州光合科技余经理8 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1238 天前
matlab画图工具
开发语言·matlab
dustcell.8 天前
haproxy七层代理
java·开发语言·前端
norlan_jame8 天前
C-PHY与D-PHY差异
c语言·开发语言
多恩Stone8 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054968 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月8 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js
m0_531237178 天前
C语言-数组练习进阶
c语言·开发语言·算法
Railshiqian8 天前
给android源码下的模拟器添加两个后排屏的修改
android·开发语言·javascript