当前位置: 首页 > news >正文

lua 写一个 不同时区之间转换日期和时间 函数

这个函数用于调整时间戳以适应不同的时区。它接受五个参数:`format`、`timeStamp`、`dontFixForTimeOffset`、`currentServerTimeZone`和`showLog`。返回  os.date,可以转化成指定格式的年月日时间

### 功能
该函数的主要功能是根据给定的时区偏移量调整时间戳,并返回格式化后的日期字符串。如果`dontFixForTimeOffset`为真或者时间戳小于31536001(即1970年1月1日之后的秒数),则直接返回原始时间戳的格式化日期。否则,它会根据当前服务器时区与本地时区之间的差异调整时间戳,并返回调整后的日期字符串。

### 参数
- `format`:日期格式字符串,用于指定返回日期字符串的格式。
- `timeStamp`:要调整的时间戳。如果未提供,则默认使用当前时间。
- `dontFixForTimeOffset`:布尔值,指示是否不调整时区偏移量。
- `currentServerTimeZone`:当前服务器的时区偏移量。如果未提供,则使用默认的`TimeUtil.timeZone`。
- `showLog`:布尔值,指示是否在日志中显示调试信息。

### 实现原理
1. **日志记录**:如果`showLog`为真,则记录时间戳和其类型。
2. **默认时间戳**:如果未提供时间戳,则使用当前时间。
3. **时区偏移量计算**:如果`dontFixForTimeOffset`为假且时间戳大于31536001,则计算当前服务器时区与本地时区之间的偏移量。
4. **时间戳格式判断**:判断时间戳是秒还是毫秒。如果是毫秒,则将其转换为秒。
5. **时区调整**:根据时区偏移量调整时间戳,并考虑夏令时的影响。
6. **格式化日期**:使用调整后的时间戳格式化日期字符串。
7. **日志记录**:如果`showLog`为真,则记录调整后的时间戳。
8. **返回结果**:返回格式化后的日期字符串。

### 注意事项
- 确保在调用此函数之前,`TimeUtil`对象已经正确初始化,并且`getTimeZone`方法能够返回正确的时区偏移量。
- `currentServerTimeZone`参数应该是一个整数,表示时区偏移量(例如,中国标准时间(CST)为+8小时,偏移量为8)。
- `timeStamp`参数可以是秒或毫秒,函数会自动进行转换。
- 如果`showLog`为真,则会在日志中输出调试信息,这可能会影响性能,建议在生产环境中关闭。


function TimeUtil:fixTimeZoneFor_LUA_OS_DATE(format, timeStamp,dontFixForTimeOffset, currentServerTimeZone, showLog)if showLog thenprint("[fixTimeZoneFor_LUA_OS_DATE] : %s-%s",timeStamp,type(timeStamp))endif timeStamp == nil thentimeStamp = os.time()endif dontFixForTimeOffset or timeStamp < 31536001 thenreturn os.date(format, timeStamp)else-- 8 hour * 3600 seconds = 28800 secondslocal timeZone = self:getTimeZone()if currentServerTimeZone == nil thencurrentServerTimeZone = TimeUtil.timeZoneendlocal timeZoneOffset = currentServerTimeZone - timeZonelocal isMilli = falseif timeStamp == nil thenisMilli = falseelsetimeStamp = math.ceil(timeStamp)-- 判定毫秒与秒local numberStr = tostring(math.ceil(timeStamp))if #numberStr <= 10 thenisMilli = falseelseif #numberStr == 13 thenisMilli = trueendendlocal eastEightTimeStampif isMilli == false theneastEightTimeStamp = timeStamp + (timeZoneOffset) * 3600 + (os.date("*t", timeStamp).isdst and -1 or 0) * 3600elseif isMilli thentimeStamp = math.ceil(tonumber(timeStamp) / 1000)eastEightTimeStamp = timeStamp + (timeZoneOffset) * 3600 + (os.date("*t", timeStamp).isdst and -1 or 0) * 3600elseLogger.print("[FixTimeZone Exception] Fatal timestamp format : %s", timeStamp)endif showLog thenLogger.print("[eastEightTimeStamp] : %s", eastEightTimeStamp)end--return timeStampreturn os.date(format, eastEightTimeStamp)endend

http://www.lryc.cn/news/404071.html

相关文章:

  • 谷粒商城——session共享
  • Java 语言及其常用集合类的操作,以及反射机制与注解
  • 《系统架构设计师教程(第2版)》第12章-信息系统架构设计理论与实践-02-信息系统架构
  • 用html做python教程01
  • PHP接口与性状的优雅应用
  • R语言模型评估网格搜索
  • Haproxy服务
  • Unity VR开发入门:探索虚拟现实世界的无限可能
  • 系统架构设计师教程(清华第二版) 第3章 信息系统基础知识-3.2 业务处理系统-解读
  • 32_ConvNeXt网络详解
  • Langchain[3]:Langchain架构演进与功能扩展:流式事件处理、事件过滤机制、回调传播策略及装饰器应用
  • java导出PDF详细教程+各种踩坑
  • 【博士每天一篇文献-算法】连续学习算法之HNet:Continual learning with hypernetworks
  • 使用 tcpdump 进行网络流量捕获与分析
  • k8s集群 安装配置 Prometheus+grafana
  • 【Java--数据结构】二叉树oj题(上)
  • 微服务之间Feign调用
  • 【Qt】按钮的属性相关API
  • blender和3dmax和maya和c4d比较
  • visio保存一部分图/emf图片打开很模糊/emf插入到word或ppt中很模糊
  • 沙尘传输模拟教程(基于wrf-chem)
  • 使用 Python 进行测试(8)纯净测试
  • python的tkinter、socket库开发tcp的客户端和服务端
  • Python面试题:Python中的异步编程:详细讲解asyncio库的使用
  • 【信号频率估计】MVDR算法及MATLAB仿真
  • HarmonyOS NEXT零基础入门到实战-第二部分
  • 《小程序02:云开发之增删改查》
  • SQL执行流程、SQL执行计划、SQL优化
  • 【前端】JavaScript入门及实战41-45
  • 更加深入Mysql-04-MySQL 多表查询与事务的操作