Home > Software engineering >  How to ignore files over two hours from a timestamp range in Perl
How to ignore files over two hours from a timestamp range in Perl

Time:06-15

I am trying to set up a timestamp range for a file that is not over 2 hours in Perl. Taking times from file are formatted as "May 26 20:30 filename.csv" in perl format.

Here is what I am setting up:

use File::stat;
my $stat = stat($file);

# To start a time within a range or no more than 2 hours
my $start = UnixDate("now", "%m%d %H:%M"); 
my $stop = UnixDate("$start" 2, "%m%d %H:%M");  // here max it to 2 hours, not sure this part
if ($stat->$start <= $stat->$stop) { next; }

CodePudding user response:

Since you say that UnixDate returns an epoch timestamp, all you need is this:

my $cutoff = time - 2*60*60;

my $file_time = UnixDate( "%m%d %H:%M", $fn );
next if $file_time < $cutoff;
  •  Tags:  
  • perl
  • Related