perl 为标准输出添加时间

suse 11 sp3 默认 perl 5.10 不支持新的方法,只能用比较原始的

use Time::HiRes qw(gettimeofday);
$one = gettimeofday;
print STDOUT "one = $one\n";

($sec,$milli) = gettimeofday;
print STDOUT "sec = $sec, milli = $milli\n";

$milli2 = sprintf("%.*s", 3, $milli);
print STDOUT "sec = $sec, milli = $milli, milli2 = $milli2\n";
$time = join ('',$sec,$milli2);
print STDOUT "time = $time\n";

Output:
one = 1227582343.55258
sec = 1227582343, milli = 552702
sec = 1227582343, milli = 552702, milli2 = 552
time = 1227582343552

给标准输出添加时间列,带毫秒

vmstat 1|perl -M'POSIX qw(strftime setsid)' -M'Time::HiRes qw(gettimeofday)'  -nle 'BEGIN{setsid();$SIG{CHLD} = "IGNORE";}($sec,$milli)=gettimeofday;$tm=strftime("%Y-%m-%d %H:%M:%S",localtime($sec)).".".$milli;$dt=substr($tm,0,10);print $tm." $_";'

参考
How to get micro seconds with time stamp in Perl?

Epoch time in milliseconds: Is there a better way?

© 2017, 新之助meow. 原创文章转载请注明: 转载自http://www.xinmeow.com

0.00 avg. rating (0% score) - 0 votes
点赞