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