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 then
        print("[fixTimeZoneFor_LUA_OS_DATE] : %s-%s",timeStamp,type(timeStamp))
    end

    if timeStamp == nil then
        timeStamp = os.time()
    end

    if dontFixForTimeOffset or timeStamp < 31536001 then
        return os.date(format, timeStamp)
    else
        -- 8 hour * 3600 seconds = 28800 seconds
        local timeZone = self:getTimeZone()
        if currentServerTimeZone == nil then
            currentServerTimeZone = TimeUtil.timeZone
        end

        local timeZoneOffset = currentServerTimeZone - timeZone
        local isMilli = false
        if timeStamp == nil then
            isMilli = false
        else
            timeStamp = math.ceil(timeStamp)
            -- 判定毫秒与秒
            local numberStr = tostring(math.ceil(timeStamp))
            if #numberStr <= 10 then
                isMilli = false
            elseif #numberStr == 13 then
                isMilli = true
            end
        end

        local eastEightTimeStamp
        if isMilli == false then
            eastEightTimeStamp = timeStamp + (timeZoneOffset) * 3600 + (os.date("*t", timeStamp).isdst and -1 or 0) * 3600
        elseif isMilli then
            timeStamp = math.ceil(tonumber(timeStamp) / 1000)
            eastEightTimeStamp = timeStamp + (timeZoneOffset) * 3600 + (os.date("*t", timeStamp).isdst and -1 or 0) * 3600
        else
            Logger.print("[FixTimeZone Exception] Fatal timestamp format : %s", timeStamp)
        end

        if showLog then
            Logger.print("[eastEightTimeStamp] : %s", eastEightTimeStamp)
        end
        --return timeStamp
        return os.date(format, eastEightTimeStamp)
    end

end

相关推荐

  1. lua 一个 同时之间转换日期时间 函数

    2024-07-21 05:36:02       20 阅读
  2. lua 一个函数 判断两个时间戳是否在同一周

    2024-07-21 05:36:02       19 阅读
  3. Qt - 同类之间函数信号的连接

    2024-07-21 05:36:02       40 阅读
  4. MySQL 日期时间函数

    2024-07-21 05:36:02       16 阅读
  5. Ubuntu设置时时间同步

    2024-07-21 05:36:02       48 阅读
  6. Lua语法(二)——闭包/日期时间

    2024-07-21 05:36:02       37 阅读
  7. MySQL 日期时间函数全面指南

    2024-07-21 05:36:02       45 阅读
  8. PostgreSQL日期时间相关函数

    2024-07-21 05:36:02       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-21 05:36:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-21 05:36:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-21 05:36:02       45 阅读
  4. Python语言-面向对象

    2024-07-21 05:36:02       55 阅读

热门阅读

  1. 探索Perl的文件系统插件:灵活的系统扩展

    2024-07-21 05:36:02       19 阅读
  2. Spring Boot中的404错误:原因、影响及处理策略

    2024-07-21 05:36:02       21 阅读
  3. Perl并发编程秘籍:线程间通信的艺术

    2024-07-21 05:36:02       16 阅读
  4. PyTorch LSTM 单步、多步时间预测

    2024-07-21 05:36:02       18 阅读
  5. Android 14 适配之— BluetoothAdapter、JobScheduler、 Tiles

    2024-07-21 05:36:02       20 阅读
  6. 厦门大学学报哲学社会科学版

    2024-07-21 05:36:02       16 阅读
  7. 【机器学习】FlyFlowerSong【人工智能】资源指南

    2024-07-21 05:36:02       17 阅读
  8. 【19】成绩计算

    2024-07-21 05:36:02       14 阅读