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
相关推荐
EanoJiang12 小时前
【UE】支持所有Graph的截图插件
游戏
rockey6271 天前
基于AScript的Lua脚本语言发布啦
c#·.net·lua·script·动态脚本
雪碧透心凉_2 天前
Python游戏自动化实战案例
python·游戏·自动化
笨鸟先飞的橘猫2 天前
c++游戏后端开源框架学习——wukong(五、登录)
c++·游戏·开源
五仁烧饼3 天前
Unity Addressables 静默资源策略
游戏·unity·addressables
MyBili4 天前
游戏报错缺少dll、无法启动?GRLPackage 运行库合集一键修复指南
游戏·.net·directx·运行库·xna·openal·vc++ redis
MC皮蛋侠客4 天前
Redis 系列(七):事务、Pipeline 与 Lua 脚本——从原子性到 Functions
数据库·redis·lua
ownkss4 天前
游戏回本周期怎么预测:步骤、指标与复盘重点
游戏·游戏发行·游戏运营·游戏分发
ue星空4 天前
Godot 2D像素游戏移动抖动、模糊问题解决方案
游戏·游戏引擎·godot
智塑未来5 天前
打开快、切换顺、游戏稳:鸿蒙的日常流畅表现
游戏·华为·harmonyos