Lua 时间工具类

目录

一、前言

二、函数介绍

[1.DayOfWeek 枚举定义](#1.DayOfWeek 枚举定义)

2.GetTimeUntilNextTarget

3.GetSpecificWeekdayTime

三、完整代码

四、总结


一、前言

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

二、函数介绍

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

1.DayOfWeek 枚举定义

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

Lua 复制代码
DayOfWeek = {
    Sunday = 1,
    Monday = 2,
    Tuesday = 3,
    Wednesday = 4,
    Thursday = 5,
    Friday = 6,
    Saturday = 7
}

2.GetTimeUntilNextTarget

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

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

    currentDayStartTime.hour = targetHour
    currentDayStartTime.min = targetMinute
    currentDayStartTime.sec = targetSecond

    if os.time(currentDayStartTime) <= currentTime then
        currentDayStartTime.day = currentDayStartTime.day + 1
    end

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

3.GetSpecificWeekdayTime

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

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

    -- 计算当前日期和目标日期之间的天数差
    local dayDiff = targetWeekday - currentDateTime.wday
    if dayDiff <= 0 then
        dayDiff = dayDiff + 7
    end

    -- 计算目标日期的年、月、日
    local targetTimestamp = os.time(currentDateTime) + dayDiff * 24 * 60 * 60
    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

三、完整代码

Lua 复制代码
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 currentDayStartTime = os.date("*t", currentTime)

    currentDayStartTime.hour = targetHour
    currentDayStartTime.min = targetMinute
    currentDayStartTime.sec = targetSecond

    if os.time(currentDayStartTime) <= currentTime then
        currentDayStartTime.day = currentDayStartTime.day + 1
    end

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

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

    -- 计算当前日期和目标日期之间的天数差
    local dayDiff = targetWeekday - currentDateTime.wday
    if dayDiff <= 0 then
        dayDiff = dayDiff + 7
    end

    -- 计算目标日期的年、月、日
    local targetTimestamp = os.time(currentDateTime) + dayDiff * 24 * 60 * 60
    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 小时前
基于Splash的搜狗图片动态页面渲染爬取实战指南
开发语言·爬虫·python
xlq223221 小时前
22.多态(下)
开发语言·c++·算法
未来之窗软件服务1 小时前
操作系统应用(三十三)php版本选择系统—东方仙盟筑基期
开发语言·php·仙盟创梦ide·东方仙盟·服务器推荐
是Dream呀2 小时前
昇腾实战|算子模板库Catlass与CANN生态适配
开发语言·人工智能·python·华为
零匠学堂20252 小时前
移动学习系统,如何提升企业培训效果?
java·开发语言·spring boot·学习·音视频
小杨快跑~2 小时前
从装饰者到桥接再到工厂:模式组合的艺术
java·开发语言·设计模式
say_fall2 小时前
C语言编程实战:每日一题:随机链表的复制
c语言·开发语言·链表
拾贰_C2 小时前
【Python | Anaconda】 python-Anaconda 一些命令使用
开发语言·python
二川bro2 小时前
循环性能提升:Python向量化计算技巧
开发语言·python
TracyCoder1232 小时前
大白话讲Java NIO
java·开发语言·nio