Convert linux sysuptime to well format date
Here's a way without perl
:
awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}' /proc/uptime
You can do this for example with perl and some simple math:
cat /proc/uptime | perl -ne '/(\d*)/ ; printf "%02d:%02d:%02d:%02d\n",int($1/86400),int(($1%86400)/3600),int(($1%3600)/60),$1%60'
If you do not need the seconds, you can simply run the uptime
command. Its output can then be simply transformed to DD:HH:MM.
For example using (works this way only if uptime > 1h)
uptime | perl -ne '/(\d*) day[^\d]*(\d*):(\d*)/ ; printf "%02d:%02d:%02d\n", $1, $2, $3'
No, because it's not really a hard problem. Divide the number of seconds by 86400 using integer division to get the number of days. Take the remainder and divide that by 3600 to get the number of hours. Divide the remainder of that by 60 to get the number of minutes, and you're left with the number of seconds. All this is doable from the shell using the expr
command if your shell is ancient enough to not support arithmetic natively
Taking the number of seconds since the epoch and turning that into a human readable date is a hard problem, and so there are standard ways to do that, e.g. date -r SECONDS
from the shell. But that's a different problem.