-
-
Save sunny352/cdd0bcf2f7ab82c740745c79f4f3c7ba to your computer and use it in GitHub Desktop.
lua ISO 8601 datetime parser - https://repl.it/IQuI/5
This file contains hidden or 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
function get_timezone() | |
local now = os.time() | |
return os.difftime(now, os.time(os.date("!*t", now))) | |
end | |
local epoch = get_timezone() | |
function parse_json_date(json_date) | |
local year, month, day, hour, minute, seconds, offsetsign, offsethour, offsetmin = json_date:match("(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%+%- ])(%d?%d?)%:?(%d?%d?)") | |
local timestamp = os.time{year = year, month = month, day = day, hour = hour, min = minute, sec = math.floor(seconds)} + epoch | |
local offset = 0 | |
if offsetsign ~= 'Z' then | |
offset = tonumber(offsethour) * 60 + tonumber(offsetmin) | |
if offsetsign == "-" then offset = -offset end | |
end | |
return timestamp - offset * 60 | |
end | |
print(string.format('%d', parse_json_date('2017-05-25T06:21:43Z'))) | |
print(string.format('%d', parse_json_date('2017-05-25T06:21:43+0830'))) | |
print(string.format('%d', parse_json_date('2017-05-25T06:21:43-0400'))) | |
print(string.format('%d', parse_json_date('2017-05-25T06:21:43.213Z'))) | |
print(string.format('%d', parse_json_date('2021-08-20T16:06:25.36825+08:00'))) | |
print(string.format('%d', parse_json_date('2021-08-20T08:06:25.36825Z'))) | |
print(string.format('%d', 1629446785)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment