Lua 时间工具类

目录

一、前言

二、函数介绍

     1.DayOfWeek 枚举定义

     2.GetTimeUntilNextTarget 

     3.GetSpecificWeekdayTime 

三、完整代码

四、总结


一、前言

当我们编写代码时,我们经常会遇到需要处理日期和时间的情况。为了更方便地处理这些需求,我们可以创建一个工具类来封装一些常用的日期和时间操作函数,从而简化我们的开发过程。在这篇文章中,我将介绍一个名为 "Utils" 的工具类,它包含了一些方便实用的日期和时间处理函数。

二、函数介绍

      首先,让我们来看看这个工具类包含的几个主要函数:

     1.DayOfWeek 枚举定义

在这个工具类中,我们首先定义了一个名为 "DayOfWeek" 的枚举,用来表示一周中的每一天对应的数字。这样做是为了方便后续的星期几计算,通过枚举的方式可以更清晰地表达代码的意图。

DayOfWeek = {
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7
}

     2.GetTimeUntilNextTarget 

这个函数用于计算距离下一个目标时间点还有多少秒。它接受目标时间点的小时、分钟和秒作为参数,然后通过比较当前时间与目标时间的大小关系来计算出距离下一个目标时间点还有多少秒,最后返回该秒数。这个函数的逻辑简单清晰,使用起来非常方便。

-- 获取距离下一个目标时间还有多少时间 最后返回是秒
function Utils.GetTimeUntilNextTarge(targetHour, targetMinute, targetSecond)
    local currentTime = os.time()
    local targetDataTime = os.date("*t", currentTime)
    targetDataTime.hour = targetHour
    targetDataTime.min = targetMinute
    targetDataTime.sec = targetSecond

    if os.time(targetDataTime) <= currentTime then
        targetDataTime = currentTime + (24 * 60 * 60)
        targetDataTime = os.date("*t", targetDataTime)
        targetDataTime.hour = targetHour
        targetDataTime.min = targetMinute
        targetDataTime.sec = targetSecond
    end

    return os.time(targetDataTime) - currentTime
end

     3.GetSpecificWeekdayTime 

这个函数用于获取当前周的某一天的特定时间点距离当前时间还有多少秒。它接受目标星期几、目标时间点的小时、分钟和秒作为参数,然后通过一系列的计算来得出距离下一个目标时间点还有多少秒,最后返回该秒数。这个函数的实现涉及到了对日期的加减和比较,通过这个函数可以方便地获取到指定时间点在本周内距离当前时间的时间差。

-- 获取当前周的某一天的特定时间点 最后返回是秒
function Utils.GetSpecificWeekdayTime(targetWeekday, targetHour, targetMinute, targetSecond)
    local currentDateTime = os.date("*t", os.time())

    local timeDiff = 0
    -- 计算当前日期和目标日期之间的天数差
    local dayDiff = targetWeekday - currentDateTime.wday
    if dayDiff < 0 and currentDateTime.hour >= targetHour then
        timeDiff = (dayDiff + 7) * 24 * 60 * 60
    end

    -- 计算目标日期的年、月、日
    local targetTimestamp = os.time(currentDateTime) + timeDiff
    local targetDateTime = os.date("*t", targetTimestamp)

    -- 设置目标时间的小时、分钟和秒
    targetDateTime.hour = targetHour
    targetDateTime.min = targetMinute
    targetDateTime.sec = targetSecond

    -- 如果当前时间已经过了目标时间点,就将目标日期调整为下一周的相同星期几
    local currentTimestamp = os.time(currentDateTime)
    local targetTimestamp = os.time(targetDateTime)
    if currentTimestamp >= targetTimestamp then
        targetTimestamp = targetTimestamp + 7 * 24 * 60 * 60
        targetDateTime = os.date("*t", targetTimestamp)
        targetDateTime.hour = targetHour
        targetDateTime.min = targetMinute
        targetDateTime.sec = targetSecond
    end

    return os.time(targetDateTime) - os.time()
end

三、完整代码

local Utils = {}
Utils.name = "Utils"
Utils.DayOfWeek = {
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7
}

-- 获取距离下一个目标时间还有多少时间 最后返回是秒
function Utils.GetTimeUntilNextTarge(targetHour, targetMinute, targetSecond)
    local currentTime = os.time()
    local targetDataTime = os.date("*t", currentTime)
    targetDataTime.hour = targetHour
    targetDataTime.min = targetMinute
    targetDataTime.sec = targetSecond

    if os.time(targetDataTime) <= currentTime then
        targetDataTime = currentTime + (24 * 60 * 60)
        targetDataTime = os.date("*t", targetDataTime)
        targetDataTime.hour = targetHour
        targetDataTime.min = targetMinute
        targetDataTime.sec = targetSecond
    end

    return os.time(targetDataTime) - currentTime
end

-- 获取当前周的某一天的特定时间点 最后返回是秒
function Utils.GetSpecificWeekdayTime(targetWeekday, targetHour, targetMinute, targetSecond)
    local currentDateTime = os.date("*t", os.time())

    local timeDiff = 0
    -- 计算当前日期和目标日期之间的天数差
    local dayDiff = targetWeekday - currentDateTime.wday
    if dayDiff < 0 and currentDateTime.hour >= targetHour then
        timeDiff = (dayDiff + 7) * 24 * 60 * 60
    end

    -- 计算目标日期的年、月、日
    local targetTimestamp = os.time(currentDateTime) + timeDiff
    local targetDateTime = os.date("*t", targetTimestamp)

    -- 设置目标时间的小时、分钟和秒
    targetDateTime.hour = targetHour
    targetDateTime.min = targetMinute
    targetDateTime.sec = targetSecond

    -- 如果当前时间已经过了目标时间点,就将目标日期调整为下一周的相同星期几
    local currentTimestamp = os.time(currentDateTime)
    local targetTimestamp = os.time(targetDateTime)
    if currentTimestamp >= targetTimestamp then
        targetTimestamp = targetTimestamp + 7 * 24 * 60 * 60
        targetDateTime = os.date("*t", targetTimestamp)
        targetDateTime.hour = targetHour
        targetDateTime.min = targetMinute
        targetDateTime.sec = targetSecond
    end

    return os.time(targetDateTime) - os.time()
end

return Utils

四、总结

通过这些函数的封装,我们可以更加轻松地处理日期和时间相关的逻辑,提高代码的可读性和可维护性,感谢大家的支持。

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张不爱写代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值