catalogue - exit after redirect in detail.pl and updateitem.pl
[koha.git] / C4 / Date.pm
blobf8bb6dbb0f6b52a0e25a7fe11c32ea0292b784aa
1 package C4::Date;
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
7 # version.
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
17 use strict;
18 use C4::Context;
19 use Date::Calc qw(Parse_Date Decode_Date_EU Decode_Date_US Time_to_Date check_date);
21 require Exporter;
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
25 $VERSION = 0.01;
27 @ISA = qw(Exporter);
29 @EXPORT = qw(
30 &display_date_format
31 &get_date_format_string_for_DHTMLcalendar
32 &format_date
33 &format_date_in_iso
34 &fixdate
38 sub get_date_format
40 #Get the database handle
41 my $dbh = C4::Context->dbh;
42 return C4::Context->preference('dateformat');
45 sub display_date_format
47 my $dateformat = get_date_format();
49 if ( $dateformat eq "us" )
51 return "mm/dd/yyyy";
53 elsif ( $dateformat eq "metric" )
55 return "dd/mm/yyyy";
57 elsif ( $dateformat eq "iso" )
59 return "yyyy-mm-dd";
61 else
63 return "Invalid date format: $dateformat. Please change in system preferences";
67 sub get_date_format_string_for_DHTMLcalendar {
68 my $dateformat = get_date_format();
70 if ( $dateformat eq 'us' ) {
71 return '%m/%d/%Y';
73 elsif ( $dateformat eq 'metric' ) {
74 return '%d/%m/%Y';
76 elsif ( $dateformat eq "iso" ) {
77 return '%Y-%m-%d';
79 else {
80 return 'Invalid date format: '
81 . $dateformat . '.'
82 . ' Please change in system preferences';
86 sub format_date
88 my $olddate = shift;
89 my $newdate;
91 if ( ! $olddate )
93 return "";
96 # warn $olddate;
97 # $olddate=~s#/|\.|-##g;
98 my ($year,$month,$day)=Parse_Date($olddate);
99 ($year,$month,$day)=split /-|\/|\.|:/,$olddate unless ($year && $month);
100 # warn "$olddate annee $year mois $month jour $day";
101 if ($year>0 && $month>0){
102 my $dateformat = get_date_format();
103 $dateformat="metric" if (index(":",$olddate)>0);
104 if ( $dateformat eq "us" )
106 $newdate = sprintf("%02d/%02d/%04d",$month,$day,$year);
108 elsif ( $dateformat eq "metric" )
110 $newdate = sprintf("%02d/%02d/%04d",$day,$month,$year);
112 elsif ( $dateformat eq "iso" )
114 # Date_Init("DateFormat=iso");
115 $newdate = sprintf("%04d-%02d-%02d",$year,$month,$day);
117 else
119 return "Invalid date format: $dateformat. Please change in system preferences";
121 # warn "newdate :$newdate";
123 return $newdate;
126 sub format_date_in_iso
128 my $olddate = shift;
129 my $newdate;
131 if ( ! $olddate )
133 return "";
135 if (check_whether_iso($olddate)){
136 return $olddate;
137 } else {
138 my $dateformat = get_date_format();
139 my ($year,$month,$day);
140 my @date;
141 my $tmpolddate=$olddate;
142 $tmpolddate=~s#/|\.|-|\\##g;
143 $dateformat="metric" if (index(":",$olddate)>0);
144 if ( $dateformat eq "us" )
146 ($month,$day,$year)=split /-|\/|\.|:/,$olddate unless ($year && $month);
147 if ($month>0 && $day >0){
148 @date = Decode_Date_US($tmpolddate);
149 } else {
150 @date=($year, $month,$day)
153 elsif ( $dateformat eq "metric" )
155 ($day,$month,$year)=split /-|\/|\.|:/,$olddate unless ($year && $month);
156 if ($month>0 && $day >0){
157 @date = Decode_Date_EU($tmpolddate);
158 } else {
159 @date=($year, $month,$day)
162 elsif ( $dateformat eq "iso" )
164 ($year,$month,$day)=split /-|\/|\.|:/,$olddate unless ($year && $month);
165 if ($month>0 && $day >0){
166 @date=($year, $month,$day) if (check_date($year,$month,$day));
167 } else {
168 @date=($year, $month,$day)
171 else
173 return "9999-99-99";
175 $newdate = sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
176 return $newdate;
180 sub check_whether_iso
182 my $olddate = shift;
183 my @olddate= split /\-/,$olddate ;
184 return 1 if (length($olddate[0])==4 && length($olddate[1])<=2 && length($olddate[2])<=2);
185 return 0;
188 =head2 fixdate
190 ( $date, $invalidduedate ) = fixdate( $year, $month, $day );
192 =cut
194 sub fixdate {
195 my ( $year, $month, $day ) = @_;
196 my $invalidduedate;
197 my $date;
198 if ( $year && $month && $day ) {
199 if ( ( $year eq 0 ) && ( $month eq 0 ) && ( $year eq 0 ) ) {
201 else {
202 if ( ( $year eq 0 ) || ( $month eq 0 ) || ( $year eq 0 ) ) {
203 $invalidduedate = 1;
205 else {
206 if (
207 ( $day > 30 )
208 && ( ( $month == 4 )
209 || ( $month == 6 )
210 || ( $month == 9 )
211 || ( $month == 11 ) )
214 $invalidduedate = 1;
216 elsif ( ( $day > 29 ) && ( $month == 2 ) ) {
217 $invalidduedate = 1;
219 elsif (
220 ( $month == 2 )
221 && ( $day > 28 )
222 && ( ( $year % 4 )
223 && ( ( !( $year % 100 ) || ( $year % 400 ) ) ) )
226 $invalidduedate = 1;
228 else {
229 $date = "$year-$month-$day";
234 return ( $date, $invalidduedate );