Skip to content

Commit ad1260e

Browse files
cameronrsudo-tee
authored andcommitted
fix(formatter): don't use relative timestamps
Since we're not rerendering, relative timestamps don't make sense as they don't update.
1 parent 83f6168 commit ad1260e

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

lua/opencode/ui/formatter.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ function M._format_patch(output, part)
194194
' %s Restore point `%s` - %s',
195195
icons.get('restore_point'),
196196
restore_point.id:sub(1, 8),
197-
util.time_ago(restore_point.created_at)
197+
util.format_time(restore_point.created_at)
198198
)
199199
)
200200
local restore_line = output:get_line_count()
@@ -235,7 +235,7 @@ function M.format_message_header(message)
235235
local icon = message.info.role == 'user' and icons.get('header_user') or icons.get('header_assistant')
236236

237237
local time = message.info.time and message.info.time.created or nil
238-
local time_text = (time and ' (' .. util.time_ago(time) .. ')' or '')
238+
local time_text = (time and ' (' .. util.format_time(time) .. ')' or '')
239239
local role_hl = 'OpencodeMessageRole' .. role:sub(1, 1):upper() .. role:sub(2)
240240
local model_text = message.info.modelID and ' ' .. message.info.modelID or ''
241241
local debug_text = config.debug and ' [' .. message.info.id .. ']' or ''

lua/opencode/util.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,24 @@ function M.time_ago(timestamp)
187187
end
188188
end
189189

190+
--- Format a timestamp as time (e.g., "10:23 AM" or "13 Oct 2025 03:32 PM")
191+
--- @param timestamp number
192+
--- @return string: Formatted time string
193+
function M.format_time(timestamp)
194+
if timestamp > 1e12 then
195+
timestamp = math.floor(timestamp / 1000)
196+
end
197+
198+
local now = os.time()
199+
local today_start = os.time(os.date('*t', now)) - os.date('*t', now).hour * 3600 - os.date('*t', now).min * 60 - os.date('*t', now).sec
200+
201+
if timestamp >= today_start then
202+
return os.date('%I:%M %p', timestamp)
203+
else
204+
return os.date('%d %b %Y %I:%M %p', timestamp)
205+
end
206+
end
207+
190208
function M.index_of(tbl, value)
191209
for i, v in ipairs(tbl) do
192210
if v == value then

tests/helpers.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function M.replay_setup()
3434

3535
renderer.reset()
3636

37-
M.mock_time_ago()
37+
M.mock_time_utils()
3838

3939
if not config.config then
4040
config.config = vim.deepcopy(config.defaults)
@@ -131,9 +131,10 @@ function M.mock_notify()
131131
}
132132
end
133133

134-
function M.mock_time_ago()
134+
function M.mock_time_utils()
135135
local util = require('opencode.util')
136136
local original_time_ago = util.time_ago
137+
local original_format_time = util.format_time
137138

138139
---@diagnostic disable-next-line: duplicate-set-field
139140
util.time_ago = function(timestamp)
@@ -142,9 +143,11 @@ function M.mock_time_ago()
142143
end
143144
return os.date('!%Y-%m-%d %H:%M:%S', timestamp)
144145
end
146+
util.format_time = util.time_ago
145147

146148
return function()
147149
util.time_ago = original_time_ago
150+
util.format_time = original_format_time
148151
end
149152
end
150153

0 commit comments

Comments
 (0)