Add datecmp to OpenILS::Application::AppUtils.
authorJason Stephenson <jstephenson@mvlc.org>
Wed, 14 Nov 2012 15:40:35 +0000 (10:40 -0500)
committerBen Shum <bshum@biblio.org>
Tue, 18 Dec 2012 05:57:02 +0000 (00:57 -0500)
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 <jstephenson@mvlc.org>
Signed-off-by: Ben Shum <bshum@biblio.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm

index 2529d6b..e4d1e7d 100644 (file)
@@ -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;