timeutil v1.0.0

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.

Installation

Recommended: Install via installer (handles dependencies automatically):

wget run https://raw.githubusercontent.com/Twijn/cc-misc/main/util/installer.lua timeutil
View on GitHub →

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:

wget https://raw.githubusercontent.com/Twijn/cc-misc/main/util/timeutil.lua

Examples

Using with Runtime Installation

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

Usage Examples

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()

Functions

module.every(cb, intervalTime, fileName)

View source on GitHub

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.

Parameters:
Returns: TimeutilInterval # Interval object with control methods

module.everyLoaded(cb, intervalTime, fileName)

View source on GitHub

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.

Parameters:
Returns: TimeutilInterval # Interval object with control methods

module.run()

View source on GitHub

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)

View source on GitHub

Format a duration in seconds into a human-readable relative time string

Parameters:
Returns: string # Formatted time string (e.g., "5.2 minutes", "1 day", "30 seconds")