How to display uptime with sleep-mode time excluded

sleepuptime

I put my laptop to suspend mode, woke it up later, and now it shows uptime some 23 hours (with uptime), which is obviously not true. I suspect uptime simply returns the difference between the timestamp at boot and now.

Is there a way to show uptime excluding time spent in low power modes like suspend and hibernate?

Best Answer

The following commands work with Python 2.7.15rc1 on my Mint 19.
They will display the uptime excluding sleep time.

python3 -c 'import time;print(f" {(time.clock_gettime(time.CLOCK_MONOTONIC))}")'

The above shows the time in seconds with a decimal.

python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));print(f" {second} seconds")'

The above shows the time in seconds with no decimal.

python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));minute=second//60;print(f" {minute} minutes")'

The above shows the time in minutes.

python3 -c 'import time;s=int(time.clock_gettime(time.CLOCK_MONOTONIC));h=s//3600;m=(s-h*3600)//60;print(f"{h} hours {m} minutes")'

The above shows the time in hours and minutes.

python3 -c 'import time,datetime;print(datetime.timedelta(seconds=time.clock_gettime(time.CLOCK_MONOTONIC)))'

The above shows the time in hours, minutes, and seconds.

python3 -c 'import time,datetime;d=datetime.datetime(1,1,1)+datetime.timedelta(seconds=time.clock_gettime(time.CLOCK_MONOTONIC));print(f"{d.day-1} days, {d.hour} hours, {d.minute} minutes")'

The above shows the time in days, hours, and minutes.

I have used CLOCK_MONOTONIC and Awk to create a script that will calculate the total time if my Mint has been started more than once on the same date. The Awk command can even be used to calculate the total time in one week/month/year.

Related Question