fix(excmd): use realtime for v:starttime, :uptime #39425

Problem:
`v:starttime`, `:uptime` use a monotonic high-resolution timer. This
only works as long as the timer keeps running (if the computer is
suspended the timer is paused). This is somewhat unintuitive, and
doesn't match the behavior of the `uptime` shell command.

Solution:
Implement `os_realtime` to get the real time since the
epoch in nanoseconds.
This commit is contained in:
Till Bungert
2026-04-28 01:01:47 +02:00
committed by GitHub
parent 46c83ce321
commit a0820481f2
7 changed files with 30 additions and 9 deletions

View File

@@ -259,7 +259,9 @@ function M.ex_terminal(eap, shell_argv)
end
function M.ex_uptime()
local uptime = math.floor((uv.hrtime() - vim.v.starttime) / 1e9)
-- os.time() might lead to uptime == -1 when this is called too quickly after startup
local now = assert(uv.clock_gettime('realtime'))
local uptime = math.floor((now.sec * 1e9 + now.nsec - vim.v.starttime) / 1e9)
local uptime_display = time.fmt_rtime(uptime)
api.nvim_echo({ { N_('Up %s'):format(uptime_display) } }, true, {})
end

View File

@@ -667,13 +667,13 @@ vim.v.shell_error = ...
--- @type table[]
vim.v.stacktrace = ...
--- Timestamp (monotonic nanoseconds) when the Nvim process
--- Timestamp (nanoseconds from UNIX epoch) when the Nvim process
--- started.
---
--- To see the current "uptime":
---
--- ```lua
--- vim.print(('uptime: %d seconds'):format((vim.uv.hrtime() - vim.v.starttime) / 1e9))
--- vim.print(('uptime: %d seconds'):format(os.time() - (vim.v.starttime / 1e9)))
--- ```
---
--- Read-only.