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

四、总结

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

相关推荐
Java后端的Ai之路2 小时前
RAG 还是 Agent 自主检索?Claude Code 删掉向量库之后,我把选型讲透了
开发语言·人工智能·prompt·agent·rag
starzy19902 小时前
深入理解Scala高阶函数:函数式编程核心
开发语言·scala
czq_26867194878 小时前
Python打卡第46天
开发语言·python
Evand J10 小时前
【MATLAB例程介绍】(17)永磁同步电机(PMSM)基于自适应无迹卡尔曼滤波(AUKF)的电流噪声在线估计。测量噪声协方差自适应与转速估计对比仿真
开发语言·matlab·卡尔曼滤波·无迹卡尔曼滤波·pmsm·电机滤波
BaiPeter12 小时前
ZPL文件预览
开发语言·c++
噢,我明白了17 小时前
JAVA中的一些基础应用知识点
java·开发语言
时间的拾荒人18 小时前
Redis 入门详解:核心特性、重要文件与全局命令实战
开发语言·redis·面试
敲上瘾18 小时前
Qt Widgets入门:项目模板解析与Hello World
开发语言·数据库·c++·qt·ui·qt6.3
码途行者19 小时前
网站的SQL 注入:从攻击原理到防御措施
开发语言·sql·php
旖旎夜光19 小时前
【C++ 基础入门】C++ 多态(中):虚函数表、多态原理与动静绑定
开发语言·c++·笔记·学习