This project was created because I wanted Lua developers to experience the same simplicity and power of Python’s built‑in time module. The goal of this library is to give you function names, behaviors, inputs, and outputs identical to Python’s time module`, but fully implemented in pure Lua, working on all operating systems.
If you know Python’s time, then you already know this module.
You can install this module with LuaRocks forever.
luarocks install luatimeThe module must be required exactly like Python with one difference just in its name:
local luatime = require("luatime")All function names and parameters match Python’s time module.
Returns the current time in seconds since the epoch (UNIX timestamp).
local now = luatime.time()
print(now)Pauses execution for the given number of seconds.
luatime.sleep(2)
print("2 seconds passed")Returns a clock value that always increases. Cannot go backward.
local t1 = luatime.monotonic()
luatime.sleep(1)
local t2 = luatime.monotonic()
print(t2 - t1)High‑precision performance timer (alias of monotonic).
local t = luatime.perf_counter()
for i = 1, 1e6 do end
print(luatime.perf_counter() - t)Returns CPU time used by the program.
local t = luatime.process_time()
for i = 1, 5e6 do end
print(luatime.process_time() - t)Converts a timestamp to a human‑readable string.
print(luatime.ctime())
print(luatime.ctime(0))Returns a table representing the local time.
local t = luatime.localtime()
print(t.year, t.month, t.day)Returns a table representing UTC time.
local t = luatime.gmtime()
print(t.hour)Converts a time table back into a timestamp.
local ts = luatime.mktime{year=2025, month=1, day=1, hour=0}
print(ts)Parses a time string according to a format and returns a time structure.
local t = luatime.strptime("2023-12-25 15:30:45", "%Y-%m-%d %H:%M:%S")
print(t.year, t.month, t.day)Returns the current time in nanoseconds since the epoch (approximate).
local now_ns = luatime.time_ns()
print(now_ns)Nanosecond version of monotonic().
local t1 = luatime.monotonic_ns()
luatime.sleep(1)
local t2 = luatime.monotonic_ns()
print(t2 - t1)Nanosecond version of perf_counter().
Nanosecond version of process_time().
Returns information about a clock.
local info = luatime.get_clock_info("monotonic")
print(info.resolution)luatime.timezone: Offset of local timezone from UTCluatime.altzone: Offset of alternative timezoneluatime.daylight: Whether daylight saving time is in effectluatime.tzname: Tuple of timezone names
print(luatime.timezone)
print(luatime.tzname[1])| Python Function | Lua Function | Same Behavior |
|---|---|---|
| time() | luatime.time() | ✔️ |
| sleep() | luatime.sleep() | ✔️ |
| monotonic() | luatime.monotonic() | ✔️ |
| perf_counter() | luatime.perf_counter() | ✔️ |
| process_time() | luatime.process_time() | ✔️ |
| ctime() | luatime.ctime() | ✔️ |
| localtime() | luatime.localtime() | ✔️ |
| gmtime() | luatime.gmtime() | ✔️ |
| mktime() | luatime.mktime() | ✔️ |
| strftime() | luatime.strftime() | ✔️ |
| strptime() | luatime.strptime() | ✔️ |
| time_ns() | luatime.time_ns() | ✔️ |
| monotonic_ns() | luatime.monotonic_ns() | ✔️ |
| perf_counter_ns() | luatime.perf_counter_ns() | ✔️ |
| process_time_ns() | luatime.process_time_ns() | ✔️ |
| get_clock_info() | luatime.get_clock_info() | ✔️ |
| timezone | luatime.timezone | ✔️ |
| altzone | luatime.altzone | ✔️ |
| daylight | luatime.daylight | ✔️ |
| tzname | luatime.tzname | ✔️ |
This library exists so that developers who already know Python’s time can use identical functions in Lua. My goal was to make your transition between languages easier, cleaner, and more enjoyable.