=================================================================== RCS file: /cvs/pta/pta_import.pl,v retrieving revision 1.26 retrieving revision 1.27 diff -u -p -r1.26 -r1.27 --- pta/pta_import.pl 2020/11/30 14:38:35 1.26 +++ pta/pta_import.pl 2020/11/30 16:22:36 1.27 @@ -32,6 +32,13 @@ my %banks = ( "wellsfargo" => \&import_wellsfargo, ); +my %date_formats = ( + 'MM/DD/YYYY' => 's#(\d+)/(\d+)/(\d+)#$3$1$2#', + 'MM/DD/YY' => 's#(\d+)/(\d+)/(\d+)#20$3$1$2#', + 'YYYY-MM-DD' => 's#(\d+)-(\d+)-(\d+)#$1$2$3#', + 'DD.MM.YY' => 's#(\d+)\.(\d+)\.(\d+)#20$3$2$1#', +); + # === SUBROUTINES ===================================================== sub usage () { @@ -52,7 +59,7 @@ $banks{$account_name} or die "unknown accountname: $ac # Parse the configuration file. my $fn = "import/" . $account_name . ".txt"; open my $in, '<', $fn or die "$fn: $!"; -my ($cost_center_field, $csv_account, $date_field, $date_format, +my ($cost_center_field, $csv_account, $date_field, $date_regex, $delim, $header, $quantity_field, $quote, @amount_fields, @compiled, @description_fields, @ignored); while (<$in>) { @@ -75,7 +82,8 @@ while (<$in>) { } if (/^DATE\s+(\d+)\s+(\S+)/) { $date_field = $1 - 1; - $date_format = $2; + $date_regex = $date_formats{$2} + or die "unknown date format: $2"; next; } if (/^DELIM\s+(\S)$/) { @@ -160,17 +168,7 @@ LINE: while () { $matches or die "unmatched CSV line: $line"; my $date = $fields[$date_field] or die "date parse error: $line"; - if ($date_format eq "MM/DD/YYYY") { - $date =~ s#(\d+)/(\d+)/(\d+)#$3$1$2#; - } elsif ($date_format eq "MM/DD/YY") { - $date =~ s#(\d+)/(\d+)/(\d+)#20$3$1$2#; - } elsif ($date_format eq "YYYY-MM-DD") { - $date =~ s#(\d+)-(\d+)-(\d+)#$1$2$3#; - } elsif ($date_format eq "DD.MM.YY") { - $date =~ s#(\d+)\.(\d+)\.(\d+)#20$3$2$1#; - } else { - die "unhandled date format: $line"; - } + eval '$date =~ ' . $date_regex; foreach my $i (@amount_fields) { if (defined($fields[$i])) { $fields[$i] =~ s/,/./;