Linux   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了linux – 你如何在shell中获得clock_gettime(2)时钟?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我认为日期没有这样的选择 / proc / uptime是bootbased,而不是单调的. 最后我找到了cat / proc / timer_list | grep现在产生nsecs的数量,这是通过ktime_get获得的,如果我理解正确的话,它返回单调时间,但这非常麻烦. update:返回值必须与clock_gettime返回的值相同 这不回答当前的问题,但回答了原来的问题.因此,它一直被保
我认为日期没有这样的选择

/ proc / uptime是bootbased,而不是单调的.

最后我找到了cat / proc / timer_list | grep现在产生nsecs的数量,这是通过ktime_get获得的,如果我理解正确的话,它返回单调时间,但这非常麻烦.

update:返回值必须与clock_gettime返回的值相同

解决方法

这不回答当前的问题,但回答了原来的问题.因此,它一直被保留,因为它对目前为止的一些人有用.

shell中你可以使用日期工具:

date +%s.%N
date +%s%N
nanoseconds_since_70=$(date +%s%N)

从男人约会:

%s     seconds since 1970-01-01 00:00:00 UTC
       %N     nanoseconds (000000000..999999999)

纳秒部分以正确的方式补充秒:当%N从999999999变为0时,%s增加一秒.我没有参(请编辑,如果你能找到它),但只是工作.

date utility x clock_gettime

请注意,该数字不受时区更改的影响,但会受系统时钟更改的影响,例如系统管理员,NTP和调用功能所做的更改.但是,除了管理员更改外,clock_gettime函数中的CLOCK_MONOTONIC也会受到影响.

CLOCK_MONOTONIC -- Clock  that  cAnnot  be set and represents monotonic time
 since some unspecified starTing point.  This clock is not affected by 
 disconTinuous jumps in the system time (e.g.,if the system administrator 
 manually changes the clock),but is affected by the incremental adjustments
 performed by adjtime(3) and NTP.

较新的系统有更好的解决方案:CLOCK_MONOTIC_RAW.尽管如此,这是一个sHell解决方案.

了解更多

Monotonic function in Wikipedia

@caf用户回答Difference between CLOCK_REALTIME and CLOCK_MONOTONIC?

CLOCK_MONOTONIC represents the absolute elapsed wall-clock time since some
arbitrary,fixed point in the past. it isn't affected by changes in the 
system time-of-day clock. 

If you want to compute the elapsed time between two events observed on the one
machine without an intervening reboot,CLOCK_MONOTONIC is the best option.

大佬总结

以上是大佬教程为你收集整理的linux – 你如何在shell中获得clock_gettime(2)时钟?全部内容,希望文章能够帮你解决linux – 你如何在shell中获得clock_gettime(2)时钟?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。