Skip to content

fix(parse): offset parsing when seconds are decimal #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions spec/date_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ describe("Testing the 'date' module", function()
assert(date("1976-W01-1 13:00+01:00")==date(1975,12,29,12))
assert(date("1976-W01-1 0700-0500 ")==date(1975,12,29,12))

-- timezone offset with colon
assert(date("2023-08-22T17:40:23.308574-05:00")==date(2023,08,22,22,40,23.308574000))
-- timezone offset without colon
assert(date("2023-08-22T17:40:23.308574-0500")==date(2023,08,22,22,40,23.308574000))
-- timezone offset without minutes
assert(date("2023-08-22T17:40:23.308574-05")==date(2023,08,22,22,40,23.308574000))
-- timezone offset without colon with positive offset and non-zero minutes
assert(date("2023-08-22T19:40:23.308574+08:45")==date(2023,08,22,10,55,23.308574000))
-- timezone offset without colon with no decimal seconds
assert(date("2023-08-22T19:40:23-09:30")==date(2023,08,23,5,10,23))

local a = date(2006, 8, 13) assert(a == date("Sun Aug 13 2006"))
local b = date("Jun 13 1999") assert(b == date(1999, 6, 13))
local c = date(1234483200) assert(c == date("Feb 13 2009"))
Expand Down
2 changes: 1 addition & 1 deletion src/date.lua
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@
local function setzc(zs,zh,zm) setz( ((tonumber(zh)*60) + tonumber(zm))*( zs=='+' and -1 or 1) ) end

if not (sw("^(%d%d%d%d)",sety) and (sw("^(%-?)(%d%d)%1(%d%d)",function(_,a,b) setm(tonumber(a)); setd(tonumber(b)) end) or sw("^(%-?)[Ww](%d%d)%1(%d?)",function(_,a,b) w, u = tonumber(a), tonumber(b or 1) end) or sw("^%-?(%d%d%d)",setj) or sw("^%-?(%d%d)",function(a) setm(a);setd(1) end))
and ((sw("^%s*[Tt]?(%d%d):?",seth) and sw("^(%d%d):?",setr) and sw("^(%d%d)",sets) and sw("^([,%.]%d+)",adds))
and ((sw("^%s*[Tt]?(%d%d):?",seth) and sw("^(%d%d):?",setr) and sw("^(%d%d)",sets) and sw("^([,%.]%d+)",adds) and sw("%s*([+-])(%d%d):?(%d%d)%s*$",setzc))
or sw:finish() or (sw"^%s*$" or sw"^%s*[Zz]%s*$" or sw("^%s-([%+%-])(%d%d):?(%d%d)%s*$",setzc) or sw("^%s*([%+%-])(%d%d)%s*$",setzn))
) )
then --print(y,m,d,h,r,s,z,w,u,j)
Expand Down