【问题标题】:How to get timezone_offset(Eg: UTC+05:30) using Lua如何使用 Lua 获取 timezone_offset(例如:UTC+05:30)
【发布时间】:2021-10-04 20:18:33
【问题描述】:

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 变量中以供以后使用。

【问题讨论】:

标签: lua timezone lua-scripting-library


【解决方案1】:
-----------------------------------------------------------------
-- 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

【讨论】:

    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2019-09-18
    • 2013-02-12
    • 2013-09-29
    • 2020-06-27
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    相关资源
    最近更新 更多