How to get timezone_offset(Eg: UTC+05:30) using Lua

248 Views Asked by At

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

Please help to me get this value somehow using any inbuild function in lua, or custom written function in lua, or by running powershell command from lua or any other way. I wanted to store timezone value to a lua variable for later usage.

1

There are 1 best solutions below

0
Vivek Raja On
-----------------------------------------------------------------
-- 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