A timing utility module for ComputerCraft that provides persistent interval management with two different timing modes: absolute time-based and accumulated runtime-based. Features: Absolute time intervals (based on system time), accumulated time intervals (based on actual runtime), persistent state across computer restarts, pretty-printed time formatting, manual execution control, and automatic interval management with run loop.
Recommended: Install via installer (handles dependencies automatically):
This pattern downloads and runs libraries at runtime, automatically installing any that are missing:
-- Auto-install and require libraries
local libs = {"timeutil"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true
for _, lib in ipairs(libs) do
if not fs.exists(libDir .. lib .. ".lua") then
allExist = false
break
end
end
if not allExist then
shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end
local timeutil = require(libDir .. "timeutil")
-- Use the library
-- (your code here)
Alternative: Direct download via wget:
This example shows how to download and use libraries with automatic installation:
-- Auto-install and require libraries
local libs = {"timeutil"} -- Add more libraries as needed
local libDir = (fs.exists("disk") and "disk/lib/" or "/lib/")
local allExist = true
for _, lib in ipairs(libs) do
if not fs.exists(libDir .. lib .. ".lua") then
allExist = false
break
end
end
if not allExist then
shell.run("wget", "run", "https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua", table.unpack(libs))
end
local timeutil = require(libDir .. "timeutil")
-- Use the library
local timeutil = require("timeutil")
timeutil.every(function()
print("Running backup...")
end, 600, "backup_last_run")
timeutil.everyLoaded(function()
print("Heartbeat")
end, 30, "heartbeat_elapsed")
timeutil.run()
module.every(cb, intervalTime, fileName)Create an absolute time-based interval that runs based on system time This type of interval will "catch up" if the computer was offline, running immediately if the interval time has passed since the last recorded execution.
cb (function): Callback function to execute when interval triggersintervalTime (number): Interval duration in secondsfileName (string): File path to persist the last run timestampmodule.everyLoaded(cb, intervalTime, fileName)Create a runtime-based interval that accumulates time only when the program is running This type of interval will NOT catch up after downtime, only counting actual runtime. Useful for operations that should happen after X seconds of actual program execution.
cb (function): Callback function to execute when interval triggersintervalTime (number): Interval duration in seconds of actual runtimefileName (string): File path to persist the accumulated elapsed timemodule.run()Start the main interval management loop This function blocks and continuously checks all registered intervals, executing them when their time has elapsed. Runs indefinitely until terminated.
module.getRelativeTime(sec)Format a duration in seconds into a human-readable relative time string
sec (number): Duration in seconds