您如何在shell中获得clock

您如何在shell中获得clock

本文介绍了您如何在shell中获得clock_gettime(2)时钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看不到date

/proc/uptime是基于引导的,而不是单调的.

/proc/uptime is bootbased, not monotonic.

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

And at last I found cat /proc/timer_list | grep now which yields number of nsecs which is obtained via ktime_get which is returning monotonic time if I understand correctly, but that's quite cumbersome.

更新:返回值必须与clock_gettime

推荐答案

这不会回答当前问题,但会回答原始问题.因此,它一直被保留,因为到目前为止它对某些人还是有用的.

This does not answer the current question but answers the original one. So, it is being kept as it has been useful to some people so far.

在shell中,您可以只使用date实用程序:

In shell you could just use the date utility:

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会增加一秒.我对此没有参考(如果可以找到,请编辑),但是可以使用.

The nanoseconds portion complement the seconds in the right way: when %N goes from 999999999 to 0 the %s increments one second. I dont have a reference for that (please edit if you can found it) but just works.

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

Note that the number is not affected by changes in time zone but will be affected by changes in the system clock, like the changes made by the system administrator, NTP and adjtime function. However the CLOCK_MONOTONIC in the clock_gettime function is also affected, except by the administrator changes.

 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解决方案.

Newer system has a better solution: CLOCK_MONOTIC_RAW. Despite that, this is a shell solution as requested.

维基百科中的单音功能

这CLOCK_REALTIME和CLOCK_MONOTONIC?差异的@caf用户答案>:

The @caf user answer from 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.

这篇关于您如何在shell中获得clock_gettime(2)时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:46