2 # This file is part of Koha.
4 # Koha is free software; you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation; either version 2 of the License, or (at your option) any later
9 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
15 # Suite 330, Boston, MA 02111-1307 USA
19 use Date::Calc qw(Parse_Date Decode_Date_EU Decode_Date_US Time_to_Date check_date);
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
38 #Get the database handle
39 my $dbh = C4::Context->dbh;
40 return C4::Context->preference('dateformat');
43 sub display_date_format
45 my $dateformat = get_date_format();
47 if ( $dateformat eq "us" )
51 elsif ( $dateformat eq "metric" )
55 elsif ( $dateformat eq "iso" )
61 return "Invalid date format: $dateformat. Please change in system preferences";
77 # $olddate=~s#/|\.|-##g;
78 my ($year,$month,$day)=Parse_Date($olddate);
79 ($year,$month,$day)=split /-|\/|\.|:/,$olddate unless ($year && $month);
80 # warn "$olddate annee $year mois $month jour $day";
81 if ($year>0 && $month>0){
82 my $dateformat = get_date_format();
83 $dateformat="metric" if (index(":",$olddate)>0);
84 if ( $dateformat eq "us" )
86 $newdate = sprintf("%02d/%02d/%04d",$month,$day,$year);
88 elsif ( $dateformat eq "metric" )
90 $newdate = sprintf("%02d/%02d/%04d",$day,$month,$year);
92 elsif ( $dateformat eq "iso" )
94 # Date_Init("DateFormat=iso");
95 $newdate = sprintf("%04d-%02d-%02d",$year,$month,$day);
99 return "Invalid date format: $dateformat. Please change in system preferences";
101 # warn "newdate :$newdate";
106 sub format_date_in_iso
115 if (check_whether_iso($olddate)){
118 my $dateformat = get_date_format();
119 my ($year,$month,$day);
121 my $tmpolddate=$olddate;
122 $tmpolddate=~s#/|\.|-|\\##g;
123 $dateformat="metric" if (index(":",$olddate)>0);
124 if ( $dateformat eq "us" )
126 ($month,$day,$year)=split /-|\/|\.|:/,$olddate unless ($year && $month);
127 if ($month>0 && $day >0){
128 @date = Decode_Date_US($tmpolddate);
130 @date=($year, $month,$day)
133 elsif ( $dateformat eq "metric" )
135 ($day,$month,$year)=split /-|\/|\.|:/,$olddate unless ($year && $month);
136 if ($month>0 && $day >0){
137 @date = Decode_Date_EU($tmpolddate);
139 @date=($year, $month,$day)
142 elsif ( $dateformat eq "iso" )
144 ($year,$month,$day)=split /-|\/|\.|:/,$olddate unless ($year && $month);
145 if ($month>0 && $day >0){
146 @date=($year, $month,$day) if (check_date($year,$month,$day));
148 @date=($year, $month,$day)
155 $newdate = sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
160 sub check_whether_iso
163 my @olddate= split /\-/,$olddate ;
164 return 1 if (length($olddate[0])==4 && length($olddate[1])<=2 && length($olddate[2])<=2);