updated uptime format f026cd61
Steve · 2024-08-25 16:03 1 file(s) · +27 −1
main.go +27 −1
7 7
	"io/ioutil"
8 8
	"log"
9 9
	"net/http"
10 +
	"strings"
10 11
	"time"
11 12
12 13
	"github.com/shirou/gopsutil/cpu"
186 187
		return SystemStats{}, err
187 188
	}
188 189
190 +
	uptime := time.Duration(hostInfo.Uptime) * time.Second
191 +
189 192
	return SystemStats{
190 193
		CPUUsage:    c[0],
191 194
		MemoryUsed:  v.Used,
192 195
		MemoryTotal: v.Total,
193 196
		OS:          "Debian GNU/Linux 12 (bookworm) aarch64",
194 197
		Kernel:      hostInfo.KernelVersion,
195 -
		Uptime:      (time.Duration(hostInfo.Uptime) * time.Second).String(),
198 +
		Uptime:      formatUptime(uptime),
196 199
		CPUModel:    "BCM2835 (4) @ 1.800GHz",
197 200
	}, nil
198 201
}
209 212
	}
210 213
	return fmt.Sprintf("%.1f %ciB", float64(bytes)/float64(div), "KMGTPE"[exp])
211 214
}
215 +
216 +
func formatUptime(uptime time.Duration) string {
217 +
	days := int(uptime.Hours() / 24)
218 +
	hours := int(uptime.Hours()) % 24
219 +
	minutes := int(uptime.Minutes()) % 60
220 +
	seconds := int(uptime.Seconds()) % 60
221 +
222 +
	var parts []string
223 +
	if days > 0 {
224 +
		parts = append(parts, fmt.Sprintf("%dd", days))
225 +
	}
226 +
	if hours > 0 {
227 +
		parts = append(parts, fmt.Sprintf("%dh", hours))
228 +
	}
229 +
	if minutes > 0 {
230 +
		parts = append(parts, fmt.Sprintf("%dm", minutes))
231 +
	}
232 +
	if seconds > 0 || len(parts) == 0 {
233 +
		parts = append(parts, fmt.Sprintf("%ds", seconds))
234 +
	}
235 +
236 +
	return strings.Join(parts, " ")
237 +
}