Skip to content

Commit

Permalink
feat: support more date inputs (#5136)
Browse files Browse the repository at this point in the history
* feat(DateExt): support more date inputs

* Update date_ext.lua

* some test cases

* Update spec/date_ext_spec.lua

Co-authored-by: mbergen <[email protected]>

---------

Co-authored-by: mbergen <[email protected]>
  • Loading branch information
hjpalpha and mbergen authored Nov 27, 2024
1 parent e49018c commit d2518ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions spec/date_ext_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ describe('Date', function()
DateExt.readTimestamp('2021-10-17 17:40 <abbr data-tz="-4:00">EDT</abbr>')
assert.stub(FormatDateSpy).was.called_with(mw.language, 'U', '20211017 17:40 -4:00')

DateExt.readTimestamp('2021-10-17 - 17:40 <abbr data-tz="-4:00">EDT</abbr>')
assert.stub(FormatDateSpy).was.called_with(mw.language, 'U', '20211017 17:40 -4:00')

DateExt.readTimestamp('2021-10-17 21:40')
assert.stub(FormatDateSpy).was.called_with(mw.language, 'U', '20211017 21:40')

DateExt.readTimestamp('2024-11-24T15:38:01')
assert.stub(FormatDateSpy).was.called_with(mw.language, 'U', '2024112415:38:01')

DateExt.readTimestamp('2024-11-24T15:38:01.999Z')
assert.stub(FormatDateSpy).was.called_with(mw.language, 'U', '2024112415:38:01.999Z')
end)
end)

Expand Down
4 changes: 3 additions & 1 deletion standard/date_ext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ function DateExt.readTimestamp(dateInput)

-- Extracts the '-4:00' out of <abbr data-tz="-4:00" title="Eastern Daylight Time (UTC-4)">EDT</abbr>
local tzTemplateOffset = dateInput:match('data%-tz%=[\"\']([%d%-%+%:]+)[\"\']')
local datePart = (mw.text.split(dateInput, '<', true)[1]):gsub('-', '')
local datePart = (mw.text.split(dateInput, '<', true)[1])
:gsub('-', '')
:gsub('T', '')
local timestampString = mw.getContentLanguage():formatDate('U', datePart .. (tzTemplateOffset or ''))
return tonumber(timestampString)
end
Expand Down

0 comments on commit d2518ef

Please sign in to comment.