Module eco.time

Time utilities.

This module provides a simple timer abstraction built on Linux timerfd, integrated with lua-eco's coroutine scheduler.

Functions

timer (cb[, ...]) Create a timer (not started).
at (delay, cb[, ...]) Create and start a timer with a relative delay.
on (ts, cb[, ...]) Create and start a timer with an absolute timestamp.
sleep (delay) Alias of eco.sleep.
now () Get current time

Fields

CLOCK_MONOTONIC Clock id for monotonic time.
CLOCK_REALTIME Clock id for realtime clock.
TFD_TIMER_ABSTIME timerfd_settime() flag: interpret it_value as an absolute time.

Class timer

timer_methods:cancel () Cancel the timer callback.
timer_methods:set (delay) Arm (or re-arm) the timer.
timer_methods:close () Close the timer and release its file descriptor.


Functions

timer (cb[, ...])
Create a timer (not started).

The timer will not start until you call timer:set.

Parameters:

  • cb function Callback called as cb(tmr, ...).
  • ... any Extra arguments passed to callback. (optional)

Returns:

    timer tmr
at (delay, cb[, ...])
Create and start a timer with a relative delay.

This is a convenience wrapper around timer + timer:set.

Parameters:

  • delay number Delay in seconds.
  • cb function Callback called as cb(tmr, ...).
  • ... any Extra arguments passed to callback. (optional)

Returns:

  1. timer tmr
  2. string err
on (ts, cb[, ...])
Create and start a timer with an absolute timestamp.

Parameters:

  • ts number Absolute timestamp (seconds since epoch).
  • cb function Callback called as cb(tmr, ...).
  • ... any Extra arguments passed to callback. (optional)

Returns:

  1. timer tmr
  2. string err
sleep (delay)
Alias of eco.sleep.

Parameters:

  • delay number Sleep time in seconds.
now ()
Get current time

Returns:

    number Current time in seconds.

Fields

CLOCK_MONOTONIC
Clock id for monotonic time.
CLOCK_REALTIME
Clock id for realtime clock.
TFD_TIMER_ABSTIME
timerfd_settime() flag: interpret it_value as an absolute time.

Class timer

Timer object.

A timer instance is created by timer, at, or on.

The callback signature is cb(tmr, ...).

timer_methods:cancel ()
Cancel the timer callback.

This cancels the pending read waiting for the timer to expire.

timer_methods:set (delay)
Arm (or re-arm) the timer.

For timers created by timer and at, delay is a relative delay in seconds.

For timers created by on, delay is an absolute timestamp (seconds since epoch), and will be passed to timerfd_settime() with TFD_TIMER_ABSTIME.

Parameters:

  • delay number Delay (relative or absolute, depending on timer type).

Returns:

  1. boolean ok
  2. string err
timer_methods:close ()
Close the timer and release its file descriptor.
generated by LDoc 1.5.0 Last updated 2026-04-09 14:48:22