package Ernad::Presort::Dates; use strict; use warnings; use Carp qw(confess); use File::Basename; use Ernad::Dates; use Ernad::Common; ## Fixme:: obsolete this in 2019, by returned the day after a yyyy-mm-dd_yyyy-mm-dd ## construct ## for use in unleash. It does not check if the file is there but a special ## setting "today_is_issuedate" sub set_issuedate { my $naf=shift; my $do_testing=shift // ''; if(not -f $naf) { confess "I need a file here, but you gave me me '$naf'."; } my $e=$main::e // confess "I need an erimp."; #my $issuedate_namf=&Ernad::Common::find_issuedate_from_file($naf) # or confess "I can't get the issuedate from the file $naf."; my $issuedate_namf=&find_issuedate($naf) or confess "I can't get the issuedate from the file $naf."; my $issuedate=$issuedate_namf; ## if issuedate is today, make sure we are not far off namf if(not $do_testing and not $e->{'conf'}->{'today_is_issuedate'}) { my $today=&Ernad::Dates::today; $issuedate=$today; my $yesterday=&Ernad::Dates::get_shift($today,-1); ## a security check if(not $do_testing and (not ($issuedate_namf eq $today or $issuedate_namf eq $yesterday))) { confess "Your issuedate on the namf $issuedate_namf is neither $today nor $yesterday."; } } return $issuedate; } ## retrieves issuedate from report's filename, ## taking in account a special case for namf. This will ## be generalized later. For now, only call at special points sub find_issuedate { my $file = shift // confess "I need a file name here"; my $rif_ext=$Ernad::Constant::c->{'rif_ext'} or die; my $bana=basename($file); if($bana=~m|^(\d{4}-\d{2}-\d{2})_\d{4}-\d{2}-\d{2}|) { my $first_date=$1; if((&Ernad::Dates::compare_dates('2017-12-23',$first_date)) > 0) { my $issuedate=&Ernad::Dates::get_shift($first_date,1); return $issuedate; } } if((not $file=~m|$rif_ext$|) and (not $file=~m|$rif_ext\.gz$|)) { #warn "file $file is not a rif"; } $bana=~m|^(\d{4}-\d{2}-\d{2})|; my $issuedate=$1 // ''; return $issuedate; } sub correct { my $date=shift; my $today=&Ernad::Dates::today; my $e=$main::e // confess "I need an erimp."; if($e->{'conf'}->{'today_is_issuedate'}) { my $yesterday=&Ernad::Dates::get_shift($today,-1); ## old dates, pre 2017-12-31 if(not ($date eq $today or $date eq $yesterday)) { return $date; } } return $today; } ## sub namf_files_by_date { my $dir=shift // confess 'I need a directory here'; my $get_date=shift // ''; if(not -d $dir) { confess "I don't see your directory $dir,"; } my $out; my @files=glob("$dir/*"); foreach my $file (@files) { my $bana=basename($file); if(not $bana=~m|^(\d{4}-\d{2}-\d{2})_\d{4}-\d{2}-\d{2}|) { next; } #my $issuedate=$1; ## moves one before if we have an issuedate #$issuedate=&correct($issuedate); ## check uniqueness my $issuedate=find_issuedate($file); if($out->{$issuedate}) { my $already_file=$out->{$issuedate}; confess "I have $already_file and $file of issuedate $issuedate"; } $out->{$issuedate}=$file; } ## return a specific file if this has been required. if($get_date) { return $out->{$get_date}; } return $out; } 1;