您当前的位置: 首页 >  Jave.Lin

Lua - 从指定时区提供的时间戳、时区的 UTC 时差,转换为对应当前本地 UTC 时差后的时间

Jave.Lin 发布时间:2020-12-24 10:38:42 ,浏览量:4

封装使用

先看看封装后的使用 在这里插入图片描述

注意输出:

Source date time data :
  USA_datetime_str : 
    2020-11-20 15:30:10
  USA_UTC_offset_hours : 
    -5
Tranform to :
  CHINA_BeiJing_datetime_str : 
    2020-11-21 4:30:10
  CHINA_BeiJing_UTC_offset_hours : 
    8
Source Code
Const = {}
LuaUtil = {}

Const.EnableTimeZoneSummer = true
Const.EnableTimeZoneFall = false

--[[
    @desc: 可替换包含{xxx}的内容
    author: jave.lin
    time:2020-12-09 14:30:58
    --@format_str: 带占位格式的字符串 "{year}-{month}-{day} {hour}:{min}:{sec}"
	--@tbl: 包含{xxx}的内容:{ year = 2020, month = 12, day = 9, hour = 14, min = 30, sec = 58}
	--@p: nil or "{(%w+)}
    @return: 替换了 {xxx} 的内容
]]
table.format = function(format_str, tbl, p)
	p = p or "{(%w+)}"
	return tostring(string.gsub(format_str, p, tbl))
end

--[[
    @desc: 获取 timestamp 时间戳对应的时间日期的表现方式
    author: jave.lin
    time:2020-12-09 15:32:31
	--@timestamp: 时间戳,单位:秒
    --@format_str: 带占位格式的字符串 "{year}-{month}-{day} {hour}:{min}:{sec}"
	--@p: nil or "{(%w+)}
    @return: 返回 timestamp 对应的日期时间的表示字符串
]]
function LuaUtil.getDateTimeStr(timestamp, format_str, p)
    format_str = format_str or "{year}-{month}-{day} {hour}:{min}:{sec}"
    return table.format(format_str, os.date("*t", timestamp), p)
end
function LuaUtil.getDateTimeStrByTbl(datetime_tbl, format_str, p)
    format_str = format_str or "{year}-{month}-{day} {hour}:{min}:{sec}"
    return table.format(format_str, datetime_tbl, p)
end

--[[
    @desc: 获取本地时区相对 UTC 的时差值,单位:秒,如:北京时差 +8,那么返回:8 * 3600 秒
    author: jave.lin
    time:2020-12-24 09:44:02
    @return: 返回相对 UTC 时区的时差值:秒
]]
function LuaUtil.getLocalUtcOffsetSeconds()
    if LuaUtil.__localUtcOffsetSeconds == nil then
        local now_tick = os.time()
        local date_local = os.date("*t", now_tick) -- local
        local date_utc = os.date("!*t", now_tick) -- utc
        local local_utc_offset = os.time(date_local) - os.time(date_utc)
        LuaUtil.__localUtcOffsetSeconds = local_utc_offset
    end
    return LuaUtil.__localUtcOffsetSeconds
end

--[[
    @desc: 获取本地时区相对 UTC 的时差值,单位:小时,如:北京时差 +8,那么返回:8 小时
    author: jave.lin
    time:2020-12-24 09:44:02
    @return: 返回相对 UTC 时区的时差值:小时
]]
function LuaUtil.getLocalUtcOffsetHours()
    if LuaUtil.__localUtcOffsetHours == nil then
        LuaUtil.__localUtcOffsetHours = LuaUtil.getLocalUtcOffsetSeconds() / 3600
    end
    return LuaUtil.__localUtcOffsetHours
end

-- src_zone_timestamp src zone 的时间戳
-- src_zone_offset_hour src zone 的相对 utc 的时间戳
-- return 相对当前 local(本地)的时间戳
--[[
    @desc: 将 src 指定的时间戳(秒)、时区值(小时),转为相对本地相对 utc 的偏差后的时间戳
    author: jave.lin
    time:2020-12-24 09:48:07
    --@src_zone_timestamp: src 指定的时间戳(秒)
	--@src_zone_offset_hour: src 时区值(小时)
    @return: 返回相对本地相对 utc 的偏差后的时间戳
]]
function LuaUtil.srcToLocalUtcOffsetTimestamp(src_zone_timestamp, src_zone_offset_hour)
    local src_utc_timestamp = (src_zone_timestamp - (src_zone_offset_hour * 3600))
    local to_local_utc_timestamp = src_utc_timestamp + LuaUtil.getLocalUtcOffsetSeconds()
    -- 3-5月就是春季,6-8就是夏季,如果是9-11就是秋季,如果是12-2就是冬季
    -- local date_time_tbl = os.date("*t", to_local_utc_timestamp)
    -- 夏令时、冬令时我们不需要额外处理,lua 底层有处理
    -- 冬令时:就是标准时间,按理不需要调整,所以下面的 - 3600 操作是有问题的
    -- os.date 返回的 table 数据中 date_time_tbl.isdst 就是是否夏令时的一个标记,内部有处理,但是显示的话,要是按标准的时间来显示,所以不需要处理
    -- if 
    -- Const.EnableTimeZoneSummer and 
    -- date_time_tbl.month >= 3 and 
    -- date_time_tbl.month = 9 and
    -- date_time_tbl.month             
关注
打赏
1688896170
查看更多评论
0.0778s