Skip to content

Instantly share code, notes, and snippets.

@cxmeel
Created May 2, 2024 11:04
Show Gist options
  • Save cxmeel/9f7539817adab3a0fea703dd22403a57 to your computer and use it in GitHub Desktop.
Save cxmeel/9f7539817adab3a0fea703dd22403a57 to your computer and use it in GitHub Desktop.
--[[
Parses a ISO 8601 UTC offset string into a float.
parseIsoUtcOffset("-0500") --> -5
parseIsoUtcOffset("+02") --> 2
parseIsoUtcOffset("-04:30") --> -4.5
parseIsoUtcOffset("Z") --> 0
local localTimezoneOffset = os.date("%z")
parseIsoUtcOffset(localTimezoneOffset)
]]
local function parseIsoUtcOffset(isoOffset: string)
local sign, hour, minute = isoOffset:match("([%+%-])(%d%d):?(%d?%d?)")
if not hour then
return 0
end
minute = if minute and minute ~= ""
then (tonumber(minute, 10) :: number) / 60
else "00"
local offset = tonumber(`{sign}{hour + minute}`, 10)
return offset or 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment