From 8fbc841497880425a6fce2901b022faa714a5452 Mon Sep 17 00:00:00 2001 From: minster586 <43217359+minster586@users.noreply.github.com> Date: Wed, 27 Aug 2025 03:20:12 -0400 Subject: [PATCH] Added support for days --- README.md | 5 ++++- obs_uptime_to_file.lua | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a6b4764..6e210cd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/obs_uptime_to_file.lua b/obs_uptime_to_file.lua index a3437ab..17103f7 100644 --- a/obs_uptime_to_file.lua +++ b/obs_uptime_to_file.lua @@ -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)