[space-lua] Time left in day with progress bar visual - version for v2

Continuing the discussion from [Space-Script] Time left in day with progress bar visual:

This is a remake of the above space-script rewritten for v2 in space-lua

function timeLeftBar()
    local now = os.date("*t") -- get current time as a table
    local secondsPassed = now.hour * 3600 + now.min * 60 + now.sec
    local totalSeconds = 24 * 3600
    local secondsLeft = totalSeconds - secondsPassed
    local percentLeft = (secondsLeft / totalSeconds) * 100
    local percentPassed = 100 - percentLeft  -- time gone

    -- Format time left as HH:MM
    local hoursLeft = math.floor(secondsLeft / 3600)
    local minutesLeft = math.floor((secondsLeft % 3600) / 60)
    local timeStr = string.format("%02d:%02d", hoursLeft, minutesLeft)

    -- Create progress bar (🟥 = time gone, 🟩 = time left)
    local barLength = 24
    local filledLength = math.floor((percentPassed / 100) * barLength)
    local bar = string.rep("🟥", filledLength) .. string.rep("🟩", barLength - filledLength)

    -- Combine everything
    local tlb = string.format("%s   %s   (%.1f%% left)", timeStr, bar, percentLeft)
    return tlb
end

Example how to use it in your page:

🕑 Time Left: ${timeLeftBar()}

This will look like this:

2 Likes

And here if you want to track only your working hours (8:00 - 16:30 in the example below).
Notice the inverted color logic compared to the above example ( :green_square: - time gone, :red_square: - time left):

function workTimeLeftBar()
    local now = os.date("*t") -- current time
    local currentSeconds = now.hour * 3600 + now.min * 60 + now.sec

    -- Define working window: 08:00 -> 16:30
    local startSeconds = 8 * 3600
    local endSeconds   = 16 * 3600 + 30 * 60
    local totalSeconds = endSeconds - startSeconds

    -- Clamp currentSeconds to working window
    if currentSeconds < startSeconds then
        currentSeconds = startSeconds
    elseif currentSeconds > endSeconds then
        currentSeconds = endSeconds
    end

    -- Work out progress within window
    local secondsPassed = currentSeconds - startSeconds
    local secondsLeft   = totalSeconds - secondsPassed
    local percentPassed = (secondsPassed / totalSeconds) * 100
    local percentLeft   = 100 - percentPassed

    -- Format time left as HH:MM
    local hoursLeft   = math.floor(secondsLeft / 3600)
    local minutesLeft = math.floor((secondsLeft % 3600) / 60)
    local timeStr     = string.format("%02d:%02d", hoursLeft, minutesLeft)

    -- Progress bar (17 slots, one for each 30 minutes)
    local barLength    = 17
    local filledLength = math.floor((percentPassed / 100) * barLength)
    local bar = string.rep("🟩", filledLength) .. string.rep("🟥", barLength - filledLength)

    return string.format("%s   %s   (%.1f%% left)", timeStr, bar, percentLeft)
end

use it in your page with:

🕑 Worktime Left: ${workTimeLeftBar()}

1 Like