From 4489cc51482735c9c58486ab8304e0663f91ceb4 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 15 May 2023 09:48:58 +0000 Subject: [PATCH] Bug 33837: Add datetime comparison to filter_by_last_update Content-Type: text/plain; charset=utf-8 Test plan: Verify that filter_by_last_update still works as expected by running cleanup_database.pl with --messages DAYS parameter. Look at the oldest messages.message_date (or insert one). Run with -days X where today - X is before that date. Run with -days Y where today - Y is just after it. Note: misc/cronjobs/cleanup_database.pl -confirm -messages -1 -v will remove all messages including one you inserted just now. Signed-off-by: Marcel de Rooy --- Koha/Objects.pm | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Koha/Objects.pm b/Koha/Objects.pm index 133d0cb08e..6d1467c41d 100644 --- a/Koha/Objects.pm +++ b/Koha/Objects.pm @@ -232,11 +232,14 @@ sub update { =head3 filter_by_last_update -my $filtered_objects = $objects->filter_by_last_update +my $filtered_objects = $objects->filter_by_last_update({ + days => $x, from => $date1, to => $date2, days_inclusive => 1, datetime => 1, +}); -days exclusive by default (will be inclusive if days_inclusive is passed and set) -from inclusive -to inclusive +Note: days are exclusive by default (will be inclusive if days_inclusive is passed and set). +The parameters from and to are inclusive. They can be DateTime objects or date strings. +You should pass at least one of the parameters days, from or to. +The optional parameter datetime allows datetime comparison. =cut @@ -252,18 +255,19 @@ sub filter_by_last_update { or exists $params->{to}; my $dtf = Koha::Database->new->schema->storage->datetime_parser; + my $method = $params->{datetime} ? 'format_datetime' : 'format_date'; if ( exists $params->{days} ) { my $dt = Koha::DateUtils::dt_from_string(); my $operator = $days_inclusive ? '<=' : '<'; - $conditions->{$operator} = $dtf->format_date( $dt->subtract( days => $params->{days} ) ); + $conditions->{$operator} = $dtf->$method( $dt->subtract( days => $params->{days} ) ); } if ( exists $params->{from} ) { my $from = ref($params->{from}) ? $params->{from} : dt_from_string($params->{from}); - $conditions->{'>='} = $dtf->format_date( $from ); + $conditions->{'>='} = $dtf->$method( $from ); } if ( exists $params->{to} ) { my $to = ref($params->{to}) ? $params->{to} : dt_from_string($params->{to}); - $conditions->{'<='} = $dtf->format_date( $to ); + $conditions->{'<='} = $dtf->$method( $to ); } return $self->search( -- 2.30.2