【发布时间】:2014-06-22 06:07:43
【问题描述】:
我有一个日志文件,我想根据时间戳逐块读取它(5 分钟数据,一次一个)。样本是
2014/04/24-23:29:20.003078-<String>
2014/04/24-23:29:32.003157-<String>
2014/04/24-23:29:33.004872-<String>
2014/04/24-23:29:43.005785-<String>
现在我打开文件并使用触发器操作来查看行时间戳是否在 5 分钟之间。 (第一块我将从 2014/04/24-00:00:00 到 2014/04/24-00:05:00 开始)。但是触发器什么也没返回。我将 DATE 字符串作为参数(如 scr.pl 04/24/2014)。我的代码是:
$curr = timelocal(0, 0, 0, (split /\//, $ARGV[0])[1], (split /\//, $ARGV[0])[0]-1, (split /\//, $ARGV[0])[-1]);
$currentTime = strftime "%Y/%m/%d-%H:%M:%S", localtime($curr);
$curr += 300;
$nextTime = strftime "%Y/%m/%d-%H:%M:%S", localtime($curr);
$file='Output.txt';
open(INFO, $file) or die("Could not open file.");
foreach $line (<INFO>) {
print "$currentTime\n\n$nextTime";
if (/$currentTime/../$nextTime/){
$dataChunk = "$dataChunk\n$line"; #nothing gets added to $dataChunk
}else{
<DO SOME STUFF on DATACHUNK above>
}
}
close(<INFO>);
任何想法为什么没有返回任何东西?
我现在正在使用以下代码。它有效,但再次慢于我的预期。
$currentTime = timelocal(0, 0, 0, (split /\//, $ARGV[0])[1], (split /\//, $ARGV[0])[0]-1, (split /\//, $ARGV[0])[-1]);
$nextTime = $currentTime + 300;
my $date = substr($line1,0,19); #2014/04/24-23:29:21
my ($year,$mon,$mday,$hour,$min,$sec) = split(/[\s\/\-:]+/, $date);
my $time = timelocal($sec,$min,$hour,$mday,$mon-1,$year);
if ($currentTime <= $time && $nextTime > $time)
【问题讨论】:
标签: perl parsing time timestamp