From 3b5a7dcf0f763f4665db74e7185813d221554deb Mon Sep 17 00:00:00 2001 From: Jason Stephenson Date: Wed, 14 Nov 2012 10:40:35 -0500 Subject: [PATCH] Add datecmp to OpenILS::Application::AppUtils. datecmp is a handy subroutine for comparing two DateTime objects or string represenations. It returns -1, 0, and 1, much like the perl cmp operator. If only 1 date is specified, then it will be compared to DateTime->now. Signed-off-by: Jason Stephenson Signed-off-by: Ben Shum --- .../lib/OpenILS/Application/AppUtils.pm | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm index 2529d6b6a8..e4d1e7d36e 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm @@ -15,6 +15,8 @@ use Unicode::Normalize; use OpenSRF::Utils::SettingsClient; use UUID::Tiny; use Encode; +use DateTime; +use DateTime::Format::ISO8601; # --------------------------------------------------------------------------- # Pile of utilty methods used accross applications. @@ -2028,5 +2030,44 @@ sub basic_opac_copy_query { }; } +# Compare two dates, date1 and date2. If date2 is not defined, then +# DateTime->now will be used. Assumes dates are in ISO8601 format as +# supported by DateTime::Format::ISO8601. (A future enhancement might +# be to support other formats.) +# +# Returns -1 if $date1 < $date2 +# Returns 0 if $date1 == $date2 +# Returns 1 if $date1 > $date2 +sub datecmp { + my $self = shift; + my $date1 = shift; + my $date2 = shift; + + # Check for timezone offsets and limit them to 2 digits: + if ($date1 && $date1 =~ /(?:-|\+)\d\d\d\d$/) { + $date1 = substr($date1, 0, length($date1) - 2); + } + if ($date2 && $date2 =~ /(?:-|\+)\d\d\d\d$/) { + $date2 = substr($date2, 0, length($date2) - 2); + } + + # check date1: + unless (UNIVERSAL::isa($date1, "DateTime")) { + $date1 = DateTime::Format::ISO8601->parse_datetime($date1); + } + + # Check for date2: + unless ($date2) { + $date2 = DateTime->now; + } else { + unless (UNIVERSAL::isa($date2, "DateTime")) { + $date2 = DateTime::Format::ISO8601->parse_datetime($date2); + } + } + + return DateTime->compare($date1, $date2); +} + + 1; -- 2.43.2