Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions luatz/gettime.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ _M.source, _M.resolution, _M.gettime = (function()
end
end

local has_posix_time, posix_time = pcall(require, "posix.time")
if has_posix_time and posix_time.clock_gettime then
local clock_id = posix_time.CLOCK_REALTIME
local function timespec_to_number(timespec)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't pull out this function as I didn't want useless code in scope for the methods below

return tonumber(timespec.tv_sec) + tonumber(timespec.tv_nsec) * 1e-9
end
return "posix.time.clock_gettime(CLOCK_REALTIME)",
posix_time.clock_getres and timespec_to_number(posix_time.clock_getres(clock_id)) or 1e-9,
function()
return timespec_to_number(posix_time.clock_gettime(clock_id))
end
end

local has_unix, unix = pcall(require, "unix")
-- On Apple devices lunix only uses gettimeofday()
if has_unix and unix.clock_gettime and unix.uname and unix.uname().sysname ~= "Darwin" then
Expand Down