App-dategrep
view release on metacpan or search on metacpan
lib/App/dategrep/Iterator.pm view on Meta::CPAN
$self->skip_to_start;
return $self;
}
sub print {
my ( $self, $until ) = @_;
$until ||= $self->{end};
my $ignore = $self->{multiline} || $self->{skip_unparsable};
if ( $self->{next_line} ) {
print $self->{next_line};
}
while (1) {
my $line = $self->{fh}->getline;
if ( !$line ) {
$self->{eof} = 1;
return;
}
my ( $date, $error ) = $self->to_epoch($line);
if ($date) {
$self->{next_line} = $line;
$self->{next_date} = $date;
if ( $date >= $self->{end} ) {
$self->{eof} = 1;
return;
}
elsif ( $date >= $until ) {
return;
}
elsif ( $date < $self->{start} ) {
next;
}
else {
print $line;
}
}
elsif ( $self->{multiline} ) {
print $line;
}
elsif ( $self->{skip_unparsable} ) {
next;
}
else {
die "No date found in line $line";
}
}
return;
}
sub format_has_year {
App::dategrep::Strptime::has_year( shift->{format} );
}
sub to_epoch {
my ( $self, $line ) = @_;
if ( !$self->{format} ) {
my $format = $self->{date}->guess_format($line);
if ($format) {
$self->{format} = $format;
}
else {
return;
}
}
my $seconds =
$self->{date}->to_epoch( $line, $self->{format} );
if ( $seconds
&& $self->{next_date}
&& $self->{next_date} > $seconds
&& !$self->format_has_year )
{
$seconds = $self->{date}->to_epoch( $line, $self->{format},
{ year => ( localtime( $self->{next_date} ) )[5] + 1 } );
}
return $seconds;
}
1;
( run in 1.049 second using v1.01-cache-2.11-cpan-5a3173703d6 )