* fixing "start by" operator
[koha.git] / C4 / Date.pm
index 5e3a6cf..8c3f72d 100644 (file)
@@ -17,29 +17,21 @@ $VERSION = 0.01;
 @EXPORT = qw(
              &display_date_format
              &format_date
+             &format_date_in_iso
 );
 
 
-
 sub get_date_format
 {
        #Get the database handle
        my $dbh = C4::Context->dbh;
-
-       #Query the database to get the dateformat
-       my $sth = $dbh->prepare("SELECT value FROM systempreferences WHERE variable='dateformat'");
-
-       $sth->execute();
-
-       my ($dateformat) = $sth->fetchrow;
-       
-       return $dateformat
+       return C4::Context->preference('dateformat');
 }
 
 sub display_date_format
 {
        my $dateformat = get_date_format();
-       
+
        if ( $dateformat eq "us" )
        {
                return "mm/dd/yyyy";
@@ -64,20 +56,28 @@ sub format_date
        my $olddate = shift;
        my $newdate;
 
+       if ( ! $olddate )
+       {
+               return "";
+       }
+
        my $dateformat = get_date_format();
-       
+
        if ( $dateformat eq "us" )
        {
+               Date_Init("DateFormat=US");
                $olddate = ParseDate($olddate);
                $newdate = UnixDate($olddate,'%m/%d/%Y');
        }
        elsif ( $dateformat eq "metric" )
        {
+               Date_Init("DateFormat=metric");
                $olddate = ParseDate($olddate);
                $newdate = UnixDate($olddate,'%d/%m/%Y');
        }
        elsif ( $dateformat eq "iso" )
        {
+               Date_Init("DateFormat=iso");
                $olddate = ParseDate($olddate);
                $newdate = UnixDate($olddate,'%Y-%m-%d');
        }
@@ -87,4 +87,40 @@ sub format_date
        }
 }
 
+sub format_date_in_iso
+{
+        my $olddate = shift;
+        my $newdate;
+
+        if ( ! $olddate )
+        {
+                return "";
+        }
+                
+        my $dateformat = get_date_format();
+
+        if ( $dateformat eq "us" )
+        {
+                Date_Init("DateFormat=US");
+                $olddate = ParseDate($olddate);
+        }
+        elsif ( $dateformat eq "metric" )
+        {
+                Date_Init("DateFormat=metric");
+                $olddate = ParseDate($olddate);
+        }
+        elsif ( $dateformat eq "iso" )
+        {
+                Date_Init("DateFormat=iso");
+                $olddate = ParseDate($olddate);
+        }
+        else
+        {
+                return "9999-99-99";
+        }
+
+       $newdate = UnixDate($olddate, '%Y-%m-%d');
+
+       return $newdate;
+}
 1;