Created
April 30, 2021 08:16
-
-
Save lamchau/aaa29981259652b4a1ea569eeba0ca33 to your computer and use it in GitHub Desktop.
human readable uptime (osx)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env fish | |
set -l boottime_in_seconds (sysctl -n kern.boottime | rg '(?:\bsec\s+=\s+)(\d+)' --replace '$1' --only-matching) | |
set -l now_in_seconds (date +%s) | |
set -l seconds_elapsed (math $now_in_seconds - $boottime_in_seconds) | |
set -l uptime (awk -v seconds_elapsed=$seconds_elapsed ' | |
BEGIN { | |
ONE_HOUR_IN_SECONDS = int(60 * 60); | |
ONE_DAY_IN_SECONDS = int(24 * ONE_HOUR_IN_SECONDS); | |
ONE_WEEK_IN_SECONDS = int(7 * ONE_DAY_IN_SECONDS); | |
seconds = seconds_elapsed; | |
weeks = int(seconds / ONE_WEEK_IN_SECONDS); | |
seconds -= int(weeks * ONE_WEEK_IN_SECONDS); | |
days = int(seconds / ONE_DAY_IN_SECONDS); | |
seconds -= int(days * ONE_DAY_IN_SECONDS); | |
hours = int(seconds / ONE_HOUR_IN_SECONDS); | |
seconds -= int(hours * ONE_HOUR_IN_SECONDS); | |
minutes = int(seconds / 60); | |
seconds -= int(minutes * 60); | |
if (weeks > 0) | |
printf("%.0f weeks, %.0f days, %.0f hours, %.0f minutes, %.0f seconds", weeks, days, hours, minutes, seconds); | |
else if (days > 0) | |
printf("%.0f days, %.0f hours, %.0f minutes, %.0f seconds", days, hours, minutes, seconds); | |
else if (hours > 0) | |
printf("%.0f hours, %.0f minutes, %.0f seconds", hours, minutes, seconds); | |
else | |
printf("%.0f minutes, %.0f seconds", minutes, seconds); | |
exit | |
}' | |
) | |
printf "uptime: %s\n" $uptime |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment