dw设计一个简单网站/如何进行品牌宣传与推广
目录
一、前言
二、函数介绍
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 = targetHourtargetDataTime.min = targetMinutetargetDataTime.sec = targetSecondif os.time(targetDataTime) <= currentTime thentargetDataTime = currentTime + (24 * 60 * 60)targetDataTime = os.date("*t", targetDataTime)targetDataTime.hour = targetHourtargetDataTime.min = targetMinutetargetDataTime.sec = targetSecondendreturn 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.wdayif dayDiff < 0 and currentDateTime.hour >= targetHour thentimeDiff = (dayDiff + 7) * 24 * 60 * 60end-- 计算目标日期的年、月、日local targetTimestamp = os.time(currentDateTime) + timeDifflocal targetDateTime = os.date("*t", targetTimestamp)-- 设置目标时间的小时、分钟和秒targetDateTime.hour = targetHourtargetDateTime.min = targetMinutetargetDateTime.sec = targetSecond-- 如果当前时间已经过了目标时间点,就将目标日期调整为下一周的相同星期几local currentTimestamp = os.time(currentDateTime)local targetTimestamp = os.time(targetDateTime)if currentTimestamp >= targetTimestamp thentargetTimestamp = targetTimestamp + 7 * 24 * 60 * 60targetDateTime = os.date("*t", targetTimestamp)targetDateTime.hour = targetHourtargetDateTime.min = targetMinutetargetDateTime.sec = targetSecondendreturn 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 = targetHourtargetDataTime.min = targetMinutetargetDataTime.sec = targetSecondif os.time(targetDataTime) <= currentTime thentargetDataTime = currentTime + (24 * 60 * 60)targetDataTime = os.date("*t", targetDataTime)targetDataTime.hour = targetHourtargetDataTime.min = targetMinutetargetDataTime.sec = targetSecondendreturn 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.wdayif dayDiff < 0 and currentDateTime.hour >= targetHour thentimeDiff = (dayDiff + 7) * 24 * 60 * 60end-- 计算目标日期的年、月、日local targetTimestamp = os.time(currentDateTime) + timeDifflocal targetDateTime = os.date("*t", targetTimestamp)-- 设置目标时间的小时、分钟和秒targetDateTime.hour = targetHourtargetDateTime.min = targetMinutetargetDateTime.sec = targetSecond-- 如果当前时间已经过了目标时间点,就将目标日期调整为下一周的相同星期几local currentTimestamp = os.time(currentDateTime)local targetTimestamp = os.time(targetDateTime)if currentTimestamp >= targetTimestamp thentargetTimestamp = targetTimestamp + 7 * 24 * 60 * 60targetDateTime = os.date("*t", targetTimestamp)targetDateTime.hour = targetHourtargetDateTime.min = targetMinutetargetDateTime.sec = targetSecondendreturn os.time(targetDateTime) - os.time()
endreturn Utils
四、总结
通过这些函数的封装,我们可以更加轻松地处理日期和时间相关的逻辑,提高代码的可读性和可维护性,感谢大家的支持。
相关文章:

Lua 时间工具类
目录 一、前言 二、函数介绍 1.DayOfWeek 枚举定义 2.GetTimeUntilNextTarget 3.GetSpecificWeekdayTime 三、完整代码 四、总结 一、前言 当我们编写代码时,我们经常会遇到需要处理日期和时间的情况。为了更方便地处理这些需求,我们可以创建一个…...

Qt——Qt网络编程之TCP通信客户端的实现(使用QTcpSocket实现一个TCP客户端例程)
【系列专栏】:博主结合工作实践输出的,解决实际问题的专栏,朋友们看过来! 《项目案例分享》 《极客DIY开源分享》 《嵌入式通用开发实战》 《C++语言开发基础总结》 《从0到1学习嵌入式Linux开发》 《QT开发实战》 《Android开发实战》 《实用硬件方案设计》 《结构建模设…...

Qt信号槽与函数直接调用性能对比
1. 测试方法 定义一个类Recv,其中包含一个成员变量num和一个成员函数add(),add()实现num的递增。 另一个类Send通过信号槽或直接调用的方法调用Recv的add函数。 单独开一个线程Watcher,每秒计算num变量的增长数值,作为add函数被调…...

Python中的异常处理:try-except-finally详解与自定义异常类
Python中的异常处理:try-except-finally详解与自定义异常类 在Python编程中,异常处理是确保程序健壮性和可靠性的重要部分。当程序遇到无法预料的错误时,异常处理机制能够防止程序崩溃,并允许我们采取适当的措施来解决问题。本文…...

vscode软件上安装 Fitten Code插件及使用
一. 简介 前面几篇文章学习了 Pycharm开发工具上安装 Fitten Code插件,以及 Fitten Code插件的使用。 Fitten Code插件是是一款由非十大模型驱动的 AI 编程助手,它可以自动生成代码,提升开发效率,帮您调试 Bug,节省…...

人工智能小作业
1.问题 将下列句子用一阶谓词形式表示: (1)雪是白的。 (2)数a和数b之和大于数c。 (3)201班的学生每人都有一台笔记本电脑。 2.答案 句子(1)“雪是白的”可以表示为: White(雪)。 句子(2)“数a和数b…...

程序员搞副业一些会用到的工具
微信号采集(爬虫)技术的选型 那么,我们应该使用什么技术来从庞大的网页内容中自动筛选和提取微信号呢?答案就是:数据采集技术,也就是爬虫技术。 然而,数据采集技术种类繁多,我们具体应该采用哪一个呢&…...

k8s更改master节点IP
背景 搭建集群的同事未规划网络,导致其中有一台master ip是192.168.7.173,和其他集群节点的IP192.168.0.x或192.168.1.x相隔太远,现在需要对网络做整改,方便管理配置诸如绑定限速等操作。 master节点是3节点的。此博客属于事后记…...

c++【入门】已知一个圆的半径,求解该圆的面积和周长?
限制 时间限制 : 1 秒 内存限制 : 128 MB 已知一个圆的半径,求解该圆的面积和周长 输入 输入只有一行,只有1个整数。 输出 输出只有两行,一行面积,一行周长。(保留两位小数)。 令pi3.1415926 样例…...

c#通过sqlsugar查询信息并日期排序
c#通过sqlsugar查询信息并日期字段排序 public static List<Sugar_Get_Info_Class> Get_xml_lot_xx(string lot_number){DBContext<Sugar_Get_Info_Class> db_data DBContext<Sugar_Get_Info_Class>.OpDB();Expression<Func<Sugar_Get_Info_Class, b…...

使用 Qwen-Agent 将 8k 上下文记忆扩展到百万量级
节前,我们组织了一场算法岗技术&面试讨论会,邀请了一些互联网大厂朋友、今年参加社招和校招面试的同学。 针对大模型技术趋势、大模型落地项目经验分享、新手如何入门算法岗、该如何准备面试攻略、面试常考点等热门话题进行了深入的讨论。 汇总合集…...

Vyper重入漏洞解析
什么是重入攻击 Reentrancy攻击是以太坊智能合约中最具破坏性的攻击之一。当一个函数对另一个不可信合约进行外部调用时,就会发生重入攻击。然后,不可信合约会递归调用原始函数,试图耗尽资金。 当合约在发送资金之前未能更新其状态时&#…...

53.ReentrantLock原理
ReentrantLock使用 ReentrantLock 实现了Lock接口, 内置了Sync同步器继承了AbstractQueuedSynchronizer。 Sync是抽象类,有两个实现NonfairSync非公平,FairSync公平。 所以ReentrantLock有公平锁和非公平锁。默认是非公平锁。 public sta…...

“论边缘计算及应用”必过范文,突击2024软考高项论文
论文真题 边缘计算是在靠近物或数据源头的网络边缘侧,融合网络、计算、存储、应用核心能力的分布式开放平台(架构),就近提供边缘智能服务。边缘计算与云计算各有所长,云计算擅长全局性、非实时、长周期的大数据处理与分析,能够在…...

浅谈安全用电管理系统对重要用户的安全管理
1用电安全管理的重要性 随着社会经济的不断发展,电网建设力度的不断加大,供电的可靠性和供电质量日益提高,电网结构也在不断完善。但在电网具备供电的条件下,部分高危和重要电力用户未按规定实现双回路电源线路供电࿱…...

Docker的资源限制
文章目录 一、什么是资源限制1、Docker的资源限制2、内核支持Linux功能3、OOM异常4、调整/设置进程OOM评分和优先级4.1、/proc/PID/oom_score_adj4.2、/proc/PID/oom_adj4.3、/proc/PID/oom_score 二、容器的内存限制1、实现原理2、命令格式及指令参数2.1、命令格式2.2、指令参…...

MongoDB $rename 给字段一次重新命名的机会
学习mongodb,体会mongodb的每一个使用细节,欢迎阅读威赞的文章。这是威赞发布的第58篇mongodb技术文章,欢迎浏览本专栏威赞发布的其他文章。 在日常编写程序过程中,命名错误是经常出现的错误。拼写错误的单词,大小写字…...

OnlyOwner在Solidity中是一个修饰符,TypeError:
目录 OnlyOwner在Solidity中是一个修饰符 TypeError: Data location must be "memory" or "calldata" for parameter in function, but none was given. function AddDOm (address dataOwnermAddress, string dataProduct, string dataNotes) OnlyOwner …...

数据Ant-Design-Vue动态表头并填充
Ant-Design-Vue是一款基于Vue.js的UI组件库,广泛应用于前端开发中。在Ant-Design-Vue中,提供了许多常用的组件,包括表格组件。表格组件可以方便地展示和处理大量的数据。 在实际的开发中,我们经常会遇到需要根据后台返回的数据动…...

验证码案例
目录 前言 一、Hutool工具介绍 1.1 Maven 1.2 介绍 1.3 实现类 二、验证码案例 2.1 需求 2.2 约定前后端交互接口 2.2.1 需求分析 2.2.2 接口定义 2.3 后端生成验证码 2.4 前端接收验证码图片 2.5 后端校验验证码 2.6 前端校验验证码 2.7 后端完整代码 前言…...

python身份证ocr接口功能免费体验、身份证实名认证接口
翔云人工智能API开放平台提供身份证实名认证接口、身份证识别接口,两者的相结合可以实现身份证的快速、精准核验,当用户在进行身份证实名认证操作时,仅需上传身份证照片,证件识别接口即可快速、精准的对证件上的文字信息进行快速提…...

屏幕空间反射技术在AI绘画中的作用
在数字艺术和游戏开发的世界中,真实感渲染一直是追求的圣杯。屏幕空间反射(Screen Space Reflection,SSR)技术作为一种先进的图形处理手段,它通过在屏幕空间内模拟光线的反射来增强场景的真实感和视觉冲击力。随着人工…...

JDK下载安装Java SDK
Android中国开发者官网 Android官网 (VPN翻墙) 通过brew命令 下载OracleJDK(推荐) 手动下载OracleJDK(不推荐) oracle OracleJDK下载页 查找硬件设备是否已存在JDK环境 oracle官网 备注: JetPack JavaDevelopmentKit Java开发的系统SDK OpenJDK 开源免费SDK …...

【ARM Cache 系列文章 1.2 -- Data Cache 和 Unified Cache 的详细介绍】
请阅读【ARM Cache 及 MMU/MPU 系列文章专栏导读】 及【嵌入式开发学习必备专栏】 文章目录 Data Cache and Unified Cache数据缓存 (Data Cache)统一缓存 (Unified Cache)数据缓存与统一缓存的比较小结 Data Cache and Unified Cache 在 ARM架构中,缓存(…...

Debian13将正式切换到基于内存的临时文件系统
以前的内存很小,旅行者一号上的计算机内存只有68KB,现在的内存可以几十G,上百G足够把系统全部装载在内存里运行,获得优异的性能和极速响应体验。 很多小型系统能做到这一点,Linux没有那么激进,不过Debian …...

设计模式-工厂方法(创建型)
创建型-工厂方法 简单工厂 将被创建的对象称为“产品”,将生产“产品”对象称为“工厂”;如果创建的产品不多,且不需要生产新的产品,那么只需要一个工厂就可以,这种模式叫做“简单工厂”,它不属于23中设计…...

分布式事务大揭秘:使用MQ实现最终一致性
本文作者:小米,一个热爱技术分享的29岁程序员。如果你喜欢我的文章,欢迎关注我的微信公众号“软件求生”,获取更多技术干货! 大家好,我是小米,一个热爱分享技术的29岁程序员,今天我们来聊聊分布式事务中的一种经典实现方式——MQ最终一致性。这是一个在互联网公司中广…...

【IoT NTN】3GPP R18中关于各类IoT设备在NTN中的增强和扩展
博主未授权任何人或组织机构转载博主任何原创文章,感谢各位对原创的支持! 博主链接 本人就职于国际知名终端厂商,负责modem芯片研发。 在5G早期负责终端数据业务层、核心网相关的开发工作,目前牵头6G技术研究。 博客内容主要围绕…...

vs - vs2015编译gtest-v1.12.1
文章目录 vs - vs2015编译gtest-v1.12.1概述点评笔记将工程迁出到本地后,如果已经编译过工程,将工程Revert, Clean up 干净。编译用的CMake, 优先用VS2019自带的打开VS2015X64本地命令行编译gtest工程测试安装自己写个测试工程,看看编译出来的…...

你好GPT-4o——对GPT-4o发布的思考与看法
你好GPT-4o 前言 2024年5月13日,OpenAI官网发布了他们的新一代自然语言处理交互系统——GPT-4o。这是OpenAI继GPT4之后又一个新的旗舰模型。 GPT-4o(“o”代表“omni”)是迈向更自然的人机交互的一步——它接受文本、音频、图像和视频的任意…...