Skip to content

Commit 20251b4

Browse files
committed
Fix uptime calculation on Alpine Linux (busybox date compatibility)
busybox date doesn't support GNU date -d with ISO 8601 timestamps, causing start_epoch=0 and uptime showing ~20531 days. Now tries GNU date first, falls back to busybox date -D format. Fixes #17.
1 parent a8391c5 commit 20251b4

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

mtproxymax.sh

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,6 +2534,21 @@ restart_proxy_container() {
25342534
run_proxy_container
25352535
}
25362536

2537+
# Parse ISO 8601 timestamp to epoch (portable: GNU date, busybox date, Python fallback)
2538+
_iso_to_epoch() {
2539+
local ts="$1"
2540+
[ -z "$ts" ] && { echo "0"; return; }
2541+
# Strip sub-second precision and trailing Z (e.g. 2026-03-03T10:00:00.123456789Z -> 2026-03-03T10:00:00)
2542+
ts="${ts%%.*}"
2543+
ts="${ts%%Z}"
2544+
# GNU date: date -d "..." +%s
2545+
local epoch
2546+
epoch=$(date -d "${ts}" +%s 2>/dev/null) && { echo "$epoch"; return; }
2547+
# Busybox date: date -D '%Y-%m-%dT%H:%M:%S' -d "..." +%s
2548+
epoch=$(date -D '%Y-%m-%dT%H:%M:%S' -d "${ts}" +%s 2>/dev/null) && { echo "$epoch"; return; }
2549+
echo "0"
2550+
}
2551+
25372552
# Get container uptime
25382553
get_proxy_uptime() {
25392554
if ! is_proxy_running; then
@@ -2545,7 +2560,7 @@ get_proxy_uptime() {
25452560
[ -z "$started_at" ] && { echo "0"; return; }
25462561

25472562
local start_epoch now_epoch
2548-
start_epoch=$(date -d "${started_at}" +%s 2>/dev/null || echo "0")
2563+
start_epoch=$(_iso_to_epoch "$started_at")
25492564
now_epoch=$(date +%s)
25502565
[ "$start_epoch" -gt 0 ] 2>/dev/null && echo $((now_epoch - start_epoch)) || echo "0"
25512566
}
@@ -3374,7 +3389,10 @@ get_stats() {
33743389
get_uptime() {
33753390
local sa=$(docker inspect --format '{{.State.StartedAt}}' mtproxymax 2>/dev/null)
33763391
[ -z "$sa" ] && echo 0 && return
3377-
local se=$(date -d "$sa" +%s 2>/dev/null || echo 0)
3392+
# Portable ISO 8601 -> epoch (GNU date, busybox date)
3393+
sa="${sa%%.*}"; sa="${sa%%Z}"
3394+
local se
3395+
se=$(date -d "$sa" +%s 2>/dev/null) || se=$(date -D '%Y-%m-%dT%H:%M:%S' -d "$sa" +%s 2>/dev/null) || se=0
33783396
echo $(( $(date +%s) - se ))
33793397
}
33803398
@@ -5033,7 +5051,7 @@ show_main_menu() {
50335051
if [ -z "$_cached_start_epoch" ]; then
50345052
local started_at
50355053
started_at=$(docker inspect --format '{{.State.StartedAt}}' "$CONTAINER_NAME" 2>/dev/null)
5036-
_cached_start_epoch=$(date -d "${started_at}" +%s 2>/dev/null || echo "0")
5054+
_cached_start_epoch=$(_iso_to_epoch "$started_at")
50375055
fi
50385056
local up_secs=$(( $(date +%s) - _cached_start_epoch ))
50395057
uptime_str=$(format_duration "$up_secs")

0 commit comments

Comments
 (0)