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

四、总结

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

相关推荐

  1. Lua 时间工具

    2024-06-09 01:04:01       7 阅读
  2. 日期时间工具

    2024-06-09 01:04:01       12 阅读
  3. LUA时间面板显示

    2024-06-09 01:04:01       24 阅读
  4. Lua 基础 05 时间

    2024-06-09 01:04:01       8 阅读
  5. Lua 模仿C++

    2024-06-09 01:04:01       40 阅读
  6. 时间:SimpleDateFormatk

    2024-06-09 01:04:01       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-09 01:04:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-09 01:04:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-09 01:04:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-09 01:04:01       18 阅读

热门阅读

  1. Ratchet websocket token 验证

    2024-06-09 01:04:01       8 阅读
  2. Composition API函数

    2024-06-09 01:04:01       8 阅读
  3. Python入门Git:探索版本控制的奥秘

    2024-06-09 01:04:01       10 阅读
  4. advices about writing promotion ppt

    2024-06-09 01:04:01       12 阅读
  5. KMeans聚类分析星

    2024-06-09 01:04:01       10 阅读
  6. 中介子方程七

    2024-06-09 01:04:01       12 阅读
  7. C++的封装(十二):外部构造函数

    2024-06-09 01:04:01       8 阅读
  8. 服务器硬件基础知识有哪些?

    2024-06-09 01:04:01       7 阅读
  9. Linux的命令补全脚本

    2024-06-09 01:04:01       7 阅读
  10. Android Camera APP预览画面镜像及旋转处理

    2024-06-09 01:04:01       11 阅读
  11. 请求分页存储管理方式

    2024-06-09 01:04:01       8 阅读