Skip to content

BlueLua/timeutil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

timeutil ⏳

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.

Contents

Install

luarocks install timeutil

API

local time = require("timeutil")
local start = time.mono()
time.sleep(0.05)
print(time.now() - start)

Functions

timeutil.now()

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.SSS

timeutil.mono()

Returns 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") --> 500ms

timeutil.sleep(seconds)

Blocks for a finite non-negative duration.

local time = require("timeutil")
time.sleep(1.5) -- sleep for 1.5 seconds

Development

Use 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

About

C-backed Lua module for wall-clock time, monotonic time, and blocking sleep.

Topics

Resources

License

Stars

Watchers

Forks

Contributors