Skip to content

Commit 72644e5

Browse files
authored
fix: parsing of RFC 3339 dates without explicit timezone (#154)
Relaxes the requirement of having an explicit timezone (`+HH:MM`) in the date string, and adds support for `Z` suffixes representing UTC.
1 parent 1d92a7f commit 72644e5

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

lua/crates/time.lua

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function DateTime.parse_rfc_3339(str)
1717
-- lua regex suports no {n} occurences
1818
local pat = "^([0-9][0-9][0-9][0-9])%-([0-9][0-9])%-([0-9][0-9])" -- date
1919
.. "T([0-9][0-9]):([0-9][0-9]):([0-9][0-9])%.[0-9]+" -- time
20-
.. "([%+%-])([0-9][0-9]):([0-9][0-9])$" -- offset
20+
.. "([%+%-Z])([0-9]?[0-9]?):?([0-9]?[0-9]?)$" -- offset
2121

2222
local year, month, day, hour, minute, second, offset, offset_hour, offset_minute = str:match(pat)
2323
if year then
@@ -29,6 +29,9 @@ function DateTime.parse_rfc_3339(str)
2929
elseif offset == "-" then
3030
h = tonumber(hour) - tonumber(offset_hour)
3131
m = tonumber(minute) - tonumber(offset_minute)
32+
elseif offset == 'Z' then
33+
h = tonumber(hour)
34+
m = tonumber(minute)
3235
end
3336
return DateTime.new(os.time({
3437
---@diagnostic disable-next-line: assign-type-mismatch

0 commit comments

Comments
 (0)