From 62db9ad83a410f2bb84b039b6871fd4b2bc1aefc Mon Sep 17 00:00:00 2001 From: dylan araps Date: Mon, 4 Jan 2016 15:30:14 +1100 Subject: [PATCH] Uptime support for OS X --- fetch.sh | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/fetch.sh b/fetch.sh index 99aeea83..2a53fc9f 100755 --- a/fetch.sh +++ b/fetch.sh @@ -252,8 +252,28 @@ getuptime () { ;; "Mac OS X") - uptime=$(uptime | awk -F',' '{print $1}') - uptime=${uptime/??:?? /} + # Get boottime in seconds + boot="$(sysctl -n kern.boottime)" + boot=${boot/{ sec = /} + boot=${boot/,*} + + # Get current date in seconds + now=$(date +%s) + uptime=$(($now-$boot)) + + # Convert uptime to days/hours/mins + secs=$((${uptime}%60)) + mins=$((${uptime}/60%60)) + hours=$((${uptime}/3600%24)) + days=$((${uptime}/86400)) + + uptime="up ${mins} minutes" + + [ "${hours}" -ne 0 ] && \ + uptime="up ${hours} hours, ${uptime/up/}" + + [ "${days}" -ne 0 ] && \ + uptime="up ${days} days, ${uptime/up/}" ;; "OpenBSD")