timeutil is a C-backed Lua module for wall-clock time, monotonic time, and
blocking sleep.
The module includes POSIX, Windows, and macOS-specific code paths, and is intended for Lua 5.1, Lua 5.2, Lua 5.3, Lua 5.4, Lua 5.5, and LuaJIT.
luarocks install timeutillocal time = require("timeutil")
local start = time.mono()
time.sleep(0.05)
print(time.now() - start)Returns Unix wall-clock time in seconds.
local time = require("timeutil")
local now = time.now()
local seconds = math.floor(now)
local ms = math.floor((now - seconds) * 1000)
local dt = string.format("%s.%03d", os.date("%Y-%m-%d %H:%M:%S", seconds), ms)
print(dt) --> YYYY-MM-DD HH:mm:ss.SSSReturns monotonic elapsed time in seconds.
local time = require("timeutil")
local monotime = time.mono
local start = monotime()
time.sleep(0.5)
local stop = monotime()
local elapsed_ms = math.floor((stop - start) * 1000)
print(elapsed_ms.."ms") --> 500msBlocks for a finite non-negative duration.
local time = require("timeutil")
time.sleep(1.5) -- sleep for 1.5 secondsUse scripts/build-and-test.sh to build the rock with
LuaRocks and run the tests across the Lua versions
installed on the machine.
For local development, run:
luarocks --lua-version=5.x make rockspecs/timeutil-scm-1.rockspec