adding a warn to log why we're redirecting to installer
[koha.git] / C4 / Date.pm
1 package C4::Date;
2 # This file is part of Koha.
3 #
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.
8 #
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.
12 #
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
16
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);
20
21 require Exporter;
22
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
24
25 $VERSION = 0.01;
26
27 @ISA = qw(Exporter);
28
29 @EXPORT = qw(
30              &display_date_format
31              &get_date_format_string_for_DHTMLcalendar
32              &format_date
33              &format_date_in_iso
34              &fixdate
35 );
36
37
38 sub get_date_format
39 {
40         #Get the database handle
41         my $dbh = C4::Context->dbh;
42         return C4::Context->preference('dateformat');
43 }
44
45 sub display_date_format
46 {
47         my $dateformat = get_date_format();
48
49         if ( $dateformat eq "us" )
50         {
51                 return "mm/dd/yyyy";
52         }
53         elsif ( $dateformat eq "metric" )
54         {
55                 return "dd/mm/yyyy";
56         }
57         elsif ( $dateformat eq "iso" )
58         {
59                 return "yyyy-mm-dd";
60         }
61         else
62         {
63                 return "Invalid date format: $dateformat. Please change in system preferences";
64         }
65 }
66
67 sub get_date_format_string_for_DHTMLcalendar {
68     my $dateformat = get_date_format();
69
70     if ( $dateformat eq 'us' ) {
71         return '%m/%d/%Y';
72     }
73     elsif ( $dateformat eq 'metric' ) {
74         return '%d/%m/%Y';
75     }
76     elsif ( $dateformat eq "iso" ) {
77         return '%Y-%m-%d';
78     }
79     else {
80         return 'Invalid date format: '
81           . $dateformat . '.'
82           . ' Please change in system preferences';
83     }
84 }
85
86 sub format_date
87 {
88         my $olddate = shift;
89         my $newdate;
90
91         if ( ! $olddate )
92         {
93                 return "";
94         }
95
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" )
105       {
106           $newdate = sprintf("%02d/%02d/%04d",$month,$day,$year);
107       }
108       elsif ( $dateformat eq "metric" )
109       {
110           $newdate = sprintf("%02d/%02d/%04d",$day,$month,$year);
111       }
112       elsif ( $dateformat eq "iso" )
113       {
114   #             Date_Init("DateFormat=iso");
115           $newdate = sprintf("%04d-%02d-%02d",$year,$month,$day);
116       }
117       else
118       {
119           return "Invalid date format: $dateformat. Please change in system preferences";
120       }
121 #       warn "newdate :$newdate";
122     }
123     return $newdate;
124 }
125
126 sub format_date_in_iso
127 {
128     my $olddate = shift;
129     my $newdate;
130
131     if ( ! $olddate )
132     {
133             return "";
134     }
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" )
145       {
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)
151         }
152       }
153       elsif ( $dateformat eq "metric" )
154       {
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)
160         }
161       }
162       elsif ( $dateformat eq "iso" )
163       {
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)
169         }
170       }
171       else
172       {
173           return "9999-99-99";
174       }
175       $newdate = sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
176       return $newdate;
177     }
178 }
179
180 sub check_whether_iso
181 {
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;
186 }
187
188 =head2 fixdate
189
190 ( $date, $invalidduedate ) = fixdate( $year, $month, $day );
191
192 =cut
193
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 ) ) {
200         }
201         else {
202             if ( ( $year eq 0 ) || ( $month eq 0 ) || ( $year eq 0 ) ) {
203                 $invalidduedate = 1;
204             }
205             else {
206                 if (
207                     ( $day > 30 )
208                     && (   ( $month == 4 )
209                         || ( $month == 6 )
210                         || ( $month == 9 )
211                         || ( $month == 11 ) )
212                   )
213                 {
214                     $invalidduedate = 1;
215                 }
216                 elsif ( ( $day > 29 ) && ( $month == 2 ) ) {
217                     $invalidduedate = 1;
218                 }
219                 elsif (
220                        ( $month == 2 )
221                     && ( $day > 28 )
222                     && (   ( $year % 4 )
223                         && ( ( !( $year % 100 ) || ( $year % 400 ) ) ) )
224                   )
225                 {
226                     $invalidduedate = 1;
227                 }
228                 else {
229                     $date = "$year-$month-$day";
230                 }
231             }
232         }
233     }
234     return ( $date, $invalidduedate );
235 }
236
237 1;