Created
May 2, 2024 11:04
-
-
Save cxmeel/9f7539817adab3a0fea703dd22403a57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
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