省電力モード時間以外の稼働時間を表示するには?

省電力モード時間以外の稼働時間を表示するには?

ラップトップを一時停止モードにして後で目覚めさせたところ、約23時間の稼働時間が表示されます(uptime)。これは間違いなく間違っています。私はuptimeそれが始まったときのタイムスタンプと今のタイムスタンプの間の違いを返すと思います。

一時停止や休止状態などの低電力モードで費やされた時間を除き、稼働時間を表示する方法はありますか?

ベストアンサー1

次のコマンドは私のMint 19のPython 2.7.15rc1で動作します。
睡眠時間以外の稼働時間が表示されます。

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

上記の時間は小数点以下の秒単位です。

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

上記の時間は小数点なしで秒単位です。

python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));minute=second//60;print(f" {minute} 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")'

上記には時間と分単位の時間が表示されます。

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

時間を時、分、秒単位で表示します。

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")'

日、時間、分単位で時間を表示します。

使ったCLOCK_MONOTONICそしてAwk私のMintが同じ日に複数回実行された場合、合計時間を計算するスクリプトを作成しました。これAwkこのコマンドを使用して、週/月/年単位で合計時間を計算することもできます。

おすすめ記事