lua 实现 函数 判断两个时间戳是否在同一天

函数用于判断两个时间戳是否在同一天。下面是对代码的详细解释:

函数参数

    • `stampA` 和 `stampB`:两个时间戳,用于比较。
    • `resetInfo`:一个可选参数,包含小时、分钟和秒数,用于调整时间戳。

函数实现步骤

    1. **参数校验**:使用 `assert` 函数确保 `stampA` 和 `stampB` 都不为 `nil`。如果任一参数为 `nil`,则抛出错误信息。
    1. **时间戳转换**:调用 `TimeUtil:toInt` 方法将时间戳转换为整数。
    1. **时间戳排序**:确保 `stampA` 小于等于 `stampB`,如果不是则交换它们的值。
    1. **时间戳调整**:如果 `resetInfo` 参数存在,则根据 `resetInfo` 中的小时、分钟和秒数调整 `stampA` 和 `stampB`。
    1. **时间戳转换为日期**:调用 `TimeUtil:fixTimeZoneFor_LUA_OS_DATE` 方法将时间戳转换为日期表(`*t` 格式),分别得到 `dateA` 和 `dateB`。
    1. **日期比较**:检查 `dateA` 和 `dateB` 是否在同一天,即它们的 `day`、`month` 和 `year` 是否相同。如果相同则返回 `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:isSameDay(stampA, stampB, resetInfo)
    assert(stampA ~= nil, "error:stampA=nil")
    assert(stampB ~= nil, "error:stampB=nil")
    stampA = TimeUtil:toInt(stampA)
    stampB = TimeUtil:toInt(stampB)
    if stampA > stampB then
        stampA, stampB = stampB, stampA
    end
    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:fixTimeZoneFor_LUA_OS_DATE("*t", stampA)
    local dateB = TimeUtil:fixTimeZoneFor_LUA_OS_DATE("*t", stampB)
    -- 在创建新号的时候,上面的数值可能存在空值
    -- 目前知道导致红点初始化错误,红点初始化中断
    if dateA and dateB then
        return dateA.day == dateB.day and
        dateA.month == dateB.month and
        dateA.year == dateB.year
    else
        return false
    end
end
相关推荐
金銀銅鐵12 小时前
借助 Pygame 探索最大公约数的规律
python·数学·游戏
nujnewnehc5 天前
不会 py, 用 ai 写了个游戏辅助的感受
人工智能·游戏
jump_jump6 天前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github
XIAOHEZIcode7 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
Aloys_Code7 天前
逆向一个被遗忘的DVD游戏格式:从DES加密到Rust模拟器
游戏·模拟器·retroarch·复古游戏·native32·sunplus·赤刃·钢铁风暴
金銀銅鐵8 天前
用 Python 实现 Take-Away 游戏
python·游戏
金銀銅鐵8 天前
用 Pygame 实现 15 puzzle
python·数学·游戏
xingpanvip10 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
两水先木示10 天前
【Unity3D】小游戏启动优化、发热优化、蒙皮网格优化
游戏
资源分享助手10 天前
杀戮尖塔2下载、Slay the Spire 2中文版、卡牌肉鸽游戏、杀戮尖塔2联机、杀戮尖塔2攻略
游戏