我认为没有这样的选择 date
/proc/uptime 是基于引导的,而不是单调的.
最后我发现cat /proc/timer_list | grep now,ktime_get如果我理解正确的话,会产生nsecs的数量,通过它返回单调时间,但这非常麻烦.
update:返回的值必须与返回的值相同clock_gettime
这不回答当前的问题,但回答了原来的问题.因此,它一直被保留,因为它对目前为止的某些人有用.
在shell中你可以使用日期工具:
date +%s.%N
date +%s%N
nanoseconds_since_70=$(date +%s%N)
Run Code Online (Sandbox Code Playgroud)
从男人约会:
%s seconds since 1970-01-01 00:00:00 UTC
%N nanoseconds (000000000..999999999)
Run Code Online (Sandbox Code Playgroud)
纳秒部分以正确的方式补充秒:当%N从999999999变为0时,%s增加1秒.我没有参考(请编辑,如果你能找到它),但只是工作.
请注意,该数字不受时区更改的影响,但会受系统时钟更改的影响,例如系统管理员,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.
Run Code Online (Sandbox Code Playgroud)
较新的系统有更好的解决方案:CLOCK_MONOTIC_RAW.尽管如此,这是一个shell请求的解决方案.
@caf用户从CLOCK_REALTIME和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.
Run Code Online (Sandbox Code Playgroud)
/proc/uptime如果你每次都写出全线,咨询只是麻烦.为什么不这样包装:
$ alias now="awk '/^now/ {print \$3; exit}' /proc/timer_list"
$ now
396751009584948
Run Code Online (Sandbox Code Playgroud)
这也避免了猫的无用.
看起来它在python 3.3中可用:http: //www.python.org/dev/peps/pep-0418/
如果做不到这一点,你可以编写一个小型的C程序,它可以调用clock_gettime:http://linux.die.net/man/3/clock_gettime