bugfix: made yuipath available to login page template
[koha_fer] / C4 / Date.pm
index 09ed758..f8bb6db 100644 (file)
@@ -28,8 +28,10 @@ $VERSION = 0.01;
 
 @EXPORT = qw(
              &display_date_format
+             &get_date_format_string_for_DHTMLcalendar
              &format_date
              &format_date_in_iso
+             &fixdate
 );
 
 
@@ -62,6 +64,24 @@ sub display_date_format
        }
 }
 
+sub get_date_format_string_for_DHTMLcalendar {
+    my $dateformat = get_date_format();
+
+    if ( $dateformat eq 'us' ) {
+        return '%m/%d/%Y';
+    }
+    elsif ( $dateformat eq 'metric' ) {
+        return '%d/%m/%Y';
+    }
+    elsif ( $dateformat eq "iso" ) {
+        return '%Y-%m-%d';
+    }
+    else {
+        return 'Invalid date format: '
+          . $dateformat . '.'
+          . ' Please change in system preferences';
+    }
+}
 
 sub format_date
 {
@@ -164,4 +184,54 @@ sub check_whether_iso
     return 1 if (length($olddate[0])==4 && length($olddate[1])<=2 && length($olddate[2])<=2);
     return 0;
 }
+
+=head2 fixdate
+
+( $date, $invalidduedate ) = fixdate( $year, $month, $day );
+
+=cut
+
+sub fixdate {
+    my ( $year, $month, $day ) = @_;
+    my $invalidduedate;
+    my $date;
+    if ( $year && $month && $day ) {
+        if ( ( $year eq 0 ) && ( $month eq 0 ) && ( $year eq 0 ) ) {
+        }
+        else {
+            if ( ( $year eq 0 ) || ( $month eq 0 ) || ( $year eq 0 ) ) {
+                $invalidduedate = 1;
+            }
+            else {
+                if (
+                    ( $day > 30 )
+                    && (   ( $month == 4 )
+                        || ( $month == 6 )
+                        || ( $month == 9 )
+                        || ( $month == 11 ) )
+                  )
+                {
+                    $invalidduedate = 1;
+                }
+                elsif ( ( $day > 29 ) && ( $month == 2 ) ) {
+                    $invalidduedate = 1;
+                }
+                elsif (
+                       ( $month == 2 )
+                    && ( $day > 28 )
+                    && (   ( $year % 4 )
+                        && ( ( !( $year % 100 ) || ( $year % 400 ) ) ) )
+                  )
+                {
+                    $invalidduedate = 1;
+                }
+                else {
+                    $date = "$year-$month-$day";
+                }
+            }
+        }
+    }
+    return ( $date, $invalidduedate );
+}
+
 1;