0

In lua, i want to store the timezone_offset value(Eg: UTC+05:30) of my system to lua variable

请帮助我使用 lua 中的任何内置函数或 lua 中的自定义编写函数,或通过从 lua 或任何其他方式运行 powershell 命令以某种方式获取此值。我想将时区值存储到 lua 变量中以供以后使用。

4

1 回答 1

0
-----------------------------------------------------------------
-- Compute the difference in seconds between local time and UTC.
-----------------------------------------------------------------
function get_timezone_diff_seconds()
  local now = os.time()
  return os.difftime(now, os.time(os.date("!*t", now)))
end
------------------------------------------------------------------------------------
-- TIMEZONE OFFSET. Eg: UTC-08:00
-- Return a timezone string in ISO 8601:2000 standard form (UTC+hh:mm or UTC-hh:mm)
------------------------------------------------------------------------------------
function get_timezone_offset()
    local timezone_diff_seconds = get_timezone_diff_seconds()
    local h, m = math.modf(timezone_diff_seconds / 3600)
    local timezone_offset = ""
    
    if(timezone_diff_seconds > 0) then
        -- prefixed with '+' sign
        timezone_offset = string.format("UTC%+.2d:%.2d", h, math.abs(60 * m))
    else
        if(h == 0) then
            -- prefixed with '-' sign
            timezone_offset = string.format("UTC-%.2d:%.2d", h, math.abs(60 * m))
        else
            -- here h will be in negative number, so '-' sign is prefixed by h
            timezone_offset = string.format("UTC%.2d:%.2d", h, math.abs(60 * m))
        end
    end
      
    return timezone_offset
end
于 2021-11-01T16:03:54.237 回答