Skip to content

Commit

Permalink
fix: parse dates without timezone offset (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrisoncramer authored Oct 14, 2024
1 parent a63823c commit 0f3841f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lua/gitlab/actions/summary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ M.build_info_lines = function()
local options = {
author = { title = "Author", content = "@" .. info.author.username .. " (" .. info.author.name .. ")" },
created_at = { title = "Created", content = u.format_to_local(info.created_at, vim.fn.strftime("%z")) },
updated_at = { title = "Updated", content = u.format_to_local(info.updated_at, vim.fn.strftime("%z")) },
updated_at = { title = "Updated", content = u.time_since(info.updated_at) },
detailed_merge_status = { title = "Status", content = info.detailed_merge_status },
draft = { title = "Draft", content = (info.draft and "Yes" or "No") },
conflicts = { title = "Merge Conflicts", content = (info.has_conflicts and "Yes" or "No") },
Expand Down
14 changes: 14 additions & 0 deletions lua/gitlab/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,20 @@ M.format_to_local = function(date_string, offset)
local tzOffsetSign, tzOffsetHour, tzOffsetMin
year, month, day, hour, min, sec, _, tzOffsetSign, tzOffsetHour, tzOffsetMin =
date_string:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+).(%d+)([%+%-])(%d%d):(%d%d)")

-- ISO 8601 format with just "Z" (aka no time offset)
-- 2021-01-01T00:00:00Z
if year == nil then
year, month, day, hour, min, sec = date_string:match("(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)Z")
tzOffsetSign = "-"
tzOffsetHour = "00"
tzOffsetMin = "00"
end

if year == nil then
return "Date Unparseable"
end

tzOffset = tzOffsetSign .. tzOffsetHour .. tzOffsetMin
end

Expand Down
8 changes: 8 additions & 0 deletions tests/spec/util_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ describe("utils/init.lua", function()
local want = "November 19, 2011"
assert.are.same(want, got)
end)

it("Parses TZ w/out offset (relative)", function()
local stamp = "2023-11-14T18:44:02Z"
local got = u.time_since(stamp, current_date)
local want = "5 days ago"
assert.are.same(want, got)
end)
end)

describe("remove_first_value", function()
Expand Down Expand Up @@ -209,6 +216,7 @@ describe("utils/init.lua", function()
{ "2016-11-21T20:25:09.482-05:00", "-0500", "11/21/2016 at 20:25" },
{ "2016-11-22T1:25:09.482-00:00", "-0000", "11/22/2016 at 01:25" },
{ "2017-3-22T20:25:09.482+07:00", "+0700", "03/22/2017 at 20:25" },
{ "2016-11-22T1:25:09Z", "-0000", "11/22/2016 at 01:25" },
}
for _, val in ipairs(tests) do
local got = u.format_to_local(val[1], val[2])
Expand Down

0 comments on commit 0f3841f

Please sign in to comment.