Bug 17502: Throw some exceptions in output_pref
[koha-ffzg.git] / Koha / DateUtils.pm
index e2e8cfc..66ad264 100644 (file)
@@ -19,10 +19,9 @@ package Koha::DateUtils;
 use Modern::Perl;
 use DateTime;
 use C4::Context;
-use Carp;
+use Koha::Exceptions;
 
 use base 'Exporter';
-use version; our $VERSION = qv('1.0.0');
 
 our @EXPORT = (
     qw( dt_from_string output_pref format_sqldatetime )
@@ -85,6 +84,16 @@ sub dt_from_string {
             (?<year>\d{4})
         |xms;
     }
+    elsif ( $date_format eq 'dmydot' ) {
+        # dmydot format is "dd.mm.yyyy[ hh:mm:ss]"
+        $regex = qr|
+            (?<day>\d{2})
+            .
+            (?<month>\d{2})
+            .
+            (?<year>\d{4})
+        |xms;
+    }
     elsif ( $date_format eq 'us' ) {
         # us format is "mm/dd/yyyy[ hh:mm:ss]"
         $regex = qr|
@@ -200,14 +209,16 @@ sub output_pref {
         $dt = $params;
     }
 
-    carp "output_pref should not be called with both dt and str parameters"
-        and return
-            if $dt and $str;
+    Koha::Exceptions::WrongParameter->throw( 'output_pref should not be called with both dt and str parameter' ) if $dt and $str;
 
-    $dt = eval { dt_from_string( $str ) } if $str;
-    carp "Invalid date '$str' passed to output_pref\n" if $@;
+    if ( $str ) {
+        local $@;
+        $dt = eval { dt_from_string( $str ) };
+        Koha::Exceptions::WrongParameter->throw("Invalid date '$str' passed to output_pref" ) if $@;
+    }
 
-    return unless defined $dt;
+    return if !defined $dt; # NULL date
+    Koha::Exceptions::WrongParameter->throw( 'dt is not a datetime' )  if ref($dt) ne 'DateTime';
 
     # FIXME: see bug 13242 => no TZ for dates 'infinite'
     if ( $dt->ymd !~ /^9999/ ) {
@@ -231,6 +242,12 @@ sub output_pref {
           ? $dt->strftime("%d/%m/%Y")
           : $dt->strftime("%d/%m/%Y $time");
     }
+    elsif ( $pref =~ m/^dmydot/ ) {
+        $date = $dateonly
+          ? $dt->strftime("%d.%m.%Y")
+          : $dt->strftime("%d.%m.%Y $time");
+    }
+
     elsif ( $pref =~ m/^us/ ) {
         $date = $dateonly
           ? $dt->strftime("%m/%d/%Y")