mirror of
https://github.com/neovim/neovim.git
synced 2026-07-13 12:50:31 +00:00
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:
@@ -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
|
||||
|
||||
4
runtime/lua/vim/_meta/vvars.gen.lua
generated
4
runtime/lua/vim/_meta/vvars.gen.lua
generated
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user