diff --git a/Koha/Template/Plugin/KohaDates.pm b/Koha/Template/Plugin/KohaDates.pm index 783833f7df..a5a48b6c92 100644 --- a/Koha/Template/Plugin/KohaDates.pm +++ b/Koha/Template/Plugin/KohaDates.pm @@ -32,12 +32,27 @@ sub filter { $config->{with_hours} //= 0; my $dt = dt_from_string( $text, 'iso' ); + $dt->add( %$_ ) for _parse_config_for_durations($config); # Allow date add/subtract; see _parse_config_for_durations return $config->{as_due_date} ? output_pref({ dt => $dt, as_due_date => 1, dateformat => $config->{dateformat} }) : output_pref({ dt => $dt, dateonly => !$config->{with_hours}, dateformat => $config->{dateformat} }); } +sub _parse_config_for_durations { +# Supports passing things like add_years => 1 or subtract_years => 1 +# Same for months, weeks, days, hours, minutes or seconds +# Returns a list of hashrefs like { years => 1 }, { days => -1 } that can be passed to dt->add + my $config = shift; + my @results; + foreach my $entry ( keys %$config ) { + if( $entry =~ /^(add|subtract)_(years|months|weeks|days|hours|minutes|seconds)$/ ) { + push @results, { $2 => ( $1 eq 'add' ? 1 : -1 ) * $config->{$entry} }; + } + } + return @results; +} + sub output_preference { my ( $self, @params ) = @_; return output_pref( @params );