Added support for days

This commit is contained in:
minster586
2025-08-27 03:20:12 -04:00
parent 9920488a7c
commit 8fbc841497
2 changed files with 18 additions and 4 deletions

View File

@@ -18,10 +18,13 @@ This OBS Studio Lua script writes your current stream uptime to a text file, wit
7. (Optional) Set the `Update Interval (seconds)` to control how often the file is updated (default: 1 second).
## Format String
You can control how the uptime is displayed using the `Format String` property. Use these placeholders:
- `{d}`: Days (zero-padded, e.g., 02)
- `{h}`: Hours (zero-padded, e.g., 01)
- `{m}`: Minutes (zero-padded, e.g., 09)
- `{s}`: Seconds (zero-padded, e.g., 05)
- `{D}`: Days (no padding, e.g., 2, 12)
- `{H}`: Hours (no padding, e.g., 1, 12)
- `{M}`: Minutes (no padding, e.g., 9, 23)
- `{S}`: Seconds (no padding, e.g., 5, 45)
@@ -29,7 +32,7 @@ You can control how the uptime is displayed using the `Format String` property.
**Examples:**
- `{h}:{m}:{s}``01:23:45`
- `{H}:{M}:{S}``1:23:5`
- `{H}h {M}m``1h 23m` (or just `23m` if under 1 hour)
- `{D}d {H}h {M}m``2d 3h 15m` (or just `3h 15m` if under 1 day, or `15m` if under 1 hour)
- `{M} minutes``83 minutes`
## How It Works

View File

@@ -83,23 +83,32 @@ end
function format_time_custom(seconds)
local h = math.floor(seconds / 3600)
local d = math.floor(seconds / 86400)
local h = math.floor((seconds % 86400) / 3600)
local m = math.floor((seconds % 3600) / 60)
local s = seconds % 60
local fmt = format_string or "{h}:{m}:{s}"
-- Zero-padded
local dd = string.format("%02d", d)
local hh = string.format("%02d", h)
local mm = string.format("%02d", m)
local ss = string.format("%02d", s)
-- Non-padded
local D = tostring(d)
local H = tostring(h)
local M = tostring(m)
local S = tostring(s)
-- Smart removal for non-padded units
-- Remove '0h' or '0h ' or '0h,' etc. if h == 0
if d == 0 then
fmt = fmt:gsub("0d[ ,]*", "")
fmt = fmt:gsub("0d", "")
fmt = fmt:gsub("0D[ ,]*", "")
fmt = fmt:gsub("0D", "")
fmt = fmt:gsub("{D}d[ ,]*", "")
fmt = fmt:gsub("{D}d", "")
end
if h == 0 then
-- Remove patterns like '0h ', '0h,', '0h', '0h\n', etc.
fmt = fmt:gsub("0h[ ,]*", "")
fmt = fmt:gsub("0h", "")
fmt = fmt:gsub("0H[ ,]*", "")
@@ -116,9 +125,11 @@ function format_time_custom(seconds)
fmt = fmt:gsub("{M}m", "")
end
-- Replace placeholders
fmt = fmt:gsub("{d}", dd)
fmt = fmt:gsub("{h}", hh)
fmt = fmt:gsub("{m}", mm)
fmt = fmt:gsub("{s}", ss)
fmt = fmt:gsub("{D}", d ~= 0 and D or "")
fmt = fmt:gsub("{H}", h ~= 0 and H or "")
fmt = fmt:gsub("{M}", m ~= 0 and M or "")
fmt = fmt:gsub("{S}", S)