我把我的笔记本电脑挂起,稍后唤醒它,现在它显示了大约23个小时的正常运行时间(用uptime),这显然是不正确的。我怀疑uptime只是返回启动时的时间戳和现在的时间戳之间的区别。
是否有一种方法可以显示正常运行时间,不包括在诸如暂停和hibernate等低功耗模式下花费的时间?
发布于 2019-07-27 06:35:58
下面的命令在我的Mint 19上使用Python2.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命令甚至可以用来计算一周/月/年的总时间。
发布于 2014-12-07 23:34:29
您可能希望使用C函数clock_gettime(CLOCK_MONOTONIC_RAW),它不是由任何实用程序(据我所知)表示的。我看到了一个关于如何访问它的python示例的答案,这可能比编译一个C程序来做同样的事情更容易。
https://unix.stackexchange.com/questions/172020
复制相似问题