我将我的笔记本电脑置于挂起模式,稍后将其唤醒,现在它显示正常运行时间约为 23 小时(使用uptime),这显然不是真的。我怀疑uptime只是返回启动时和现在的时间戳之间的差异。
有没有办法显示正常运行时间,不包括在挂起和休眠等低功耗模式下花费的时间?
以下命令适用于我的 Mint 19 上的 Python 2.7.15rc1。
它们将显示不包括睡眠时间的正常运行时间。
python3 -c 'import time;print(f" {(time.clock_gettime(time.CLOCK_MONOTONIC))}")'
Run Code Online (Sandbox Code Playgroud)
上面显示的时间以秒为单位,带小数。
python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));print(f" {second} seconds")'
Run Code Online (Sandbox Code Playgroud)
上面显示的时间以秒为单位,没有小数点。
python3 -c 'import time;second=int(time.clock_gettime(time.CLOCK_MONOTONIC));minute=second//60;print(f" {minute} minutes")'
Run Code Online (Sandbox Code Playgroud)
上面显示的时间以分钟为单位。
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")'
Run Code Online (Sandbox Code Playgroud)
上面显示了以小时和分钟为单位的时间。
python3 -c 'import time,datetime;print(datetime.timedelta(seconds=time.clock_gettime(time.CLOCK_MONOTONIC)))'
Run Code Online (Sandbox Code Playgroud)
上面显示了以小时、分钟和秒为单位的时间。
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")'
Run Code Online (Sandbox Code Playgroud)
上面显示了以天、小时和分钟为单位的时间。
我已经使用CLOCK_MONOTONIC和Awk创建了一个脚本,如果我的 Mint 在同一日期启动多次,该脚本将计算总时间。该Awk命令甚至可以用来计算一周/一个月/一年的总时间。
您可能想要使用 C function clock_gettime(CLOCK_MONOTONIC_RAW),任何实用程序(据我所知)都没有提供该功能。我看到一个答案,给出了一个有关如何访问它的python 示例,这可能比编译 C 程序来执行相同的操作更容易。
| 归档时间: |
|
| 查看次数: |
2109 次 |
| 最近记录: |