summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-12-21 13:48:15 +0100
committerDaniel Friesel <derf@finalrewind.org>2020-12-21 13:48:15 +0100
commit19b8380caa2ffa54f32c67d81197ced5d79cc3c6 (patch)
tree0f82c29132289543dfbce3b89c3556f2811c8cb9
parentb2d381a1267771a7045292acc2109f6ddab1fad5 (diff)
improve (in)valid date parser
-rwxr-xr-xbin/db-zugbildung-to-json60
1 files changed, 58 insertions, 2 deletions
diff --git a/bin/db-zugbildung-to-json b/bin/db-zugbildung-to-json
index 759f2f7..722c618 100755
--- a/bin/db-zugbildung-to-json
+++ b/bin/db-zugbildung-to-json
@@ -90,12 +90,12 @@ sub parse_dates {
}
if ( $mode eq 'range'
- and $date =~ m{ ^ ab \s* (?<day> \d{2} ) \. (?<month> [^.]* ) }x )
+ and $date =~ m{ ^ ab \s* (?<day> \d{2} ) \. (?<month> [IVX]* ) }x )
{
push( @valid_ranges, [ [ $+{month}, $+{day} ], undef ] );
}
elsif ( $mode eq 'range'
- and $date =~ m{ ^ bis \s* (?<day> \d{2} ) \. (?<month> [^.]* ) }x )
+ and $date =~ m{ ^ bis \s* (?<day> \d{2} ) \. (?<month> [IVX]* ) }x )
{
push( @valid_ranges, [ undef, [ $+{month}, $+{day} ] ] );
}
@@ -105,6 +105,16 @@ sub parse_dates {
{
push( @valid_ranges, [ [ $+{fm}, $+{fd} ], [ $+{tm}, $+{td} ] ] );
}
+ elsif ( $mode eq 'extra'
+ and $date =~ m{ (?<day> \d{2} ) \. (?<month> [IVX]* ) }x )
+ {
+ push( @valid_dates, [ $+{month}, $+{day} ] );
+ }
+ elsif ( $mode eq 'except'
+ and $date =~ m{ (?<day> \d{2} ) \. (?<month> [IVX]* ) }x )
+ {
+ push( @invalid_dates, [ $+{month}, $+{day} ] );
+ }
}
my %ret = (
@@ -128,6 +138,46 @@ sub parse_dates {
push( @{ $ret{valid} }, "${from_date}/${through_date}" );
}
+ # for several dates of the same month, only the last date has the month set
+ # (e.g. "24., 31.XII."). Walk through the list in reverse so ensure the
+ # month is always available.
+ my $month = undef;
+ for my $date ( reverse @valid_dates ) {
+ if ( not $date->[0] and not $month ) {
+ say STDERR "Skipping unhandled valid date in \"$text\"";
+ next;
+ }
+
+ # TODO Winterfahrplan: Use $year-1 for month XII
+ push(
+ @{ $ret{valid} },
+ sprintf( '%04d-%02d-%02d',
+ $year, $month{ $date->[0] || $month },
+ $date->[1] )
+ );
+ if ( $date->[0] ) {
+ $month = $date->[0];
+ }
+ }
+ $month = undef;
+ for my $date ( reverse @invalid_dates ) {
+ if ( not $date->[0] and not $month ) {
+ say STDERR "Skipping unhandled invalid date in \"$text\"";
+ next;
+ }
+
+ # TODO Winterfahrplan: Use $year-1 for month XII
+ push(
+ @{ $ret{invalid} },
+ sprintf( '%04d-%02d-%02d',
+ $year, $month{ $date->[0] || $month },
+ $date->[1] )
+ );
+ if ( $date->[0] ) {
+ $month = $date->[0];
+ }
+ }
+
return %ret;
}
@@ -571,6 +621,12 @@ for my $train_number ( keys %map ) {
}
}
+# Cleanup
+
+#for my $train (values %map) {
+# delete $train->{wagonorder_notes};
+#}
+
# broken umlauf (indentation changes on page break)
delete $map{104}{cycle};
delete $map{1588}{cycle};