modify advanced search query structure :
[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 );
35
36
37 sub get_date_format
38 {
39         #Get the database handle
40         my $dbh = C4::Context->dbh;
41         return C4::Context->preference('dateformat');
42 }
43
44 sub display_date_format
45 {
46         my $dateformat = get_date_format();
47
48         if ( $dateformat eq "us" )
49         {
50                 return "mm/dd/yyyy";
51         }
52         elsif ( $dateformat eq "metric" )
53         {
54                 return "dd/mm/yyyy";
55         }
56         elsif ( $dateformat eq "iso" )
57         {
58                 return "yyyy-mm-dd";
59         }
60         else
61         {
62                 return "Invalid date format: $dateformat. Please change in system preferences";
63         }
64 }
65
66 sub get_date_format_string_for_DHTMLcalendar {
67     my $dateformat = get_date_format();
68
69     if ( $dateformat eq 'us' ) {
70         return '%m/%d/%Y';
71     }
72     elsif ( $dateformat eq 'metric' ) {
73         return '%d/%m/%Y';
74     }
75     elsif ( $dateformat eq "iso" ) {
76         return '%Y-%m-%d';
77     }
78     else {
79         return 'Invalid date format: '
80           . $dateformat . '.'
81           . ' Please change in system preferences';
82     }
83 }
84
85 sub format_date
86 {
87         my $olddate = shift;
88         my $newdate;
89
90         if ( ! $olddate )
91         {
92                 return "";
93         }
94
95 #     warn $olddate;
96 #     $olddate=~s#/|\.|-##g;
97     my ($year,$month,$day)=Parse_Date($olddate);
98     ($year,$month,$day)=split /-|\/|\.|:/,$olddate unless ($year && $month);
99 #       warn "$olddate annee $year mois $month jour $day";
100     if ($year>0 && $month>0){
101       my $dateformat = get_date_format();
102       $dateformat="metric" if (index(":",$olddate)>0);
103       if ( $dateformat eq "us" )
104       {
105           $newdate = sprintf("%02d/%02d/%04d",$month,$day,$year);
106       }
107       elsif ( $dateformat eq "metric" )
108       {
109           $newdate = sprintf("%02d/%02d/%04d",$day,$month,$year);
110       }
111       elsif ( $dateformat eq "iso" )
112       {
113   #             Date_Init("DateFormat=iso");
114           $newdate = sprintf("%04d-%02d-%02d",$year,$month,$day);
115       }
116       else
117       {
118           return "Invalid date format: $dateformat. Please change in system preferences";
119       }
120 #       warn "newdate :$newdate";
121     }
122     return $newdate;
123 }
124
125 sub format_date_in_iso
126 {
127     my $olddate = shift;
128     my $newdate;
129
130     if ( ! $olddate )
131     {
132             return "";
133     }
134     if (check_whether_iso($olddate)){
135       return $olddate;
136     } else {
137       my $dateformat = get_date_format();
138       my ($year,$month,$day);
139       my @date;
140       my $tmpolddate=$olddate;
141       $tmpolddate=~s#/|\.|-|\\##g;
142       $dateformat="metric" if (index(":",$olddate)>0);
143       if ( $dateformat eq "us" )
144       {
145         ($month,$day,$year)=split /-|\/|\.|:/,$olddate unless ($year && $month);
146         if ($month>0 && $day >0){
147               @date = Decode_Date_US($tmpolddate);
148         } else {
149           @date=($year, $month,$day)
150         }
151       }
152       elsif ( $dateformat eq "metric" )
153       {
154         ($day,$month,$year)=split /-|\/|\.|:/,$olddate unless ($year && $month);
155         if ($month>0 && $day >0){
156               @date = Decode_Date_EU($tmpolddate);
157         } else {
158           @date=($year, $month,$day)
159         }
160       }
161       elsif ( $dateformat eq "iso" )
162       {
163         ($year,$month,$day)=split /-|\/|\.|:/,$olddate unless ($year && $month);
164         if ($month>0 && $day >0){
165           @date=($year, $month,$day) if (check_date($year,$month,$day));
166         } else {
167           @date=($year, $month,$day)
168         }
169       }
170       else
171       {
172           return "9999-99-99";
173       }
174       $newdate = sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
175       return $newdate;
176     }
177 }
178
179 sub check_whether_iso
180 {
181     my $olddate = shift;
182     my @olddate= split /\-/,$olddate ;
183     return 1 if (length($olddate[0])==4 && length($olddate[1])<=2 && length($olddate[2])<=2);
184     return 0;
185 }
186 1;