rel_3_0 moved to HEAD
[koha.git] / C4 / Date.pm
1 #!/usr/bin/perl -w
2
3 package C4::Date;
4
5 use strict;
6 use C4::Context;
7 use Date::Calc qw(Parse_Date Decode_Date_EU Decode_Date_US Time_to_Date check_date);
8
9 require Exporter;
10
11 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
12
13 $VERSION = 0.01;
14
15 @ISA = qw(Exporter);
16
17 @EXPORT = qw(
18              &display_date_format
19              &format_date
20              &format_date_in_iso
21 );
22
23
24 sub get_date_format
25 {
26         #Get the database handle
27         my $dbh = C4::Context->dbh;
28         return C4::Context->preference('dateformat');
29 }
30
31 sub display_date_format
32 {
33         my $dateformat = get_date_format();
34
35         if ( $dateformat eq "us" )
36         {
37                 return "mm/dd/yyyy";
38         }
39         elsif ( $dateformat eq "metric" )
40         {
41                 return "dd/mm/yyyy";
42         }
43         elsif ( $dateformat eq "iso" )
44         {
45                 return "yyyy-mm-dd";
46         }
47         else
48         {
49                 return "Invalid date format: $dateformat. Please change in system preferences";
50         }
51 }
52
53
54 sub format_date
55 {
56         my $olddate = shift;
57         my $newdate;
58
59         if ( ! $olddate )
60         {
61                 return "";
62         }
63
64 #     warn $olddate;
65 #     $olddate=~s#/|\.|-##g;
66     my ($year,$month,$day)=Parse_Date($olddate);
67     ($year,$month,$day)=split /-|\/|\.|:/,$olddate unless ($year && $month);
68 #       warn "$olddate annee $year mois $month jour $day";
69     if ($year>0 && $month>0){
70       my $dateformat = get_date_format();
71       $dateformat="metric" if (index(":",$olddate)>0);
72       if ( $dateformat eq "us" )
73       {
74           $newdate = sprintf("%02d/%02d/%04d",$month,$day,$year);
75       }
76       elsif ( $dateformat eq "metric" )
77       {
78           $newdate = sprintf("%02d/%02d/%04d",$day,$month,$year);
79       }
80       elsif ( $dateformat eq "iso" )
81       {
82   #             Date_Init("DateFormat=iso");
83           $newdate = sprintf("%04d-%02d-%02d",$year,$month,$day);
84       }
85       else
86       {
87           return "Invalid date format: $dateformat. Please change in system preferences";
88       }
89 #       warn "newdate :$newdate";
90     }
91     return $newdate;
92 }
93
94 sub format_date_in_iso
95 {
96     my $olddate = shift;
97     my $newdate;
98
99     if ( ! $olddate )
100     {
101             return "";
102     }
103     if (check_whether_iso($olddate)){
104       return $olddate;
105     } else {
106       my $dateformat = get_date_format();
107       my ($year,$month,$day);
108       my @date;
109       my $tmpolddate=$olddate;
110       $tmpolddate=~s#/|\.|-|\\##g;
111       $dateformat="metric" if (index(":",$olddate)>0);
112       if ( $dateformat eq "us" )
113       {
114         ($month,$day,$year)=split /-|\/|\.|:/,$olddate unless ($year && $month);
115         if ($month>0 && $day >0){
116               @date = Decode_Date_US($tmpolddate);
117         } else {
118           @date=($year, $month,$day)
119         }
120       }
121       elsif ( $dateformat eq "metric" )
122       {
123         ($day,$month,$year)=split /-|\/|\.|:/,$olddate unless ($year && $month);
124         if ($month>0 && $day >0){
125               @date = Decode_Date_EU($tmpolddate);
126         } else {
127           @date=($year, $month,$day)
128         }
129       }
130       elsif ( $dateformat eq "iso" )
131       {
132         ($year,$month,$day)=split /-|\/|\.|:/,$olddate unless ($year && $month);
133         if ($month>0 && $day >0){
134           @date=($year, $month,$day) if (check_date($year,$month,$day));
135         } else {
136           @date=($year, $month,$day)
137         }
138       }
139       else
140       {
141           return "9999-99-99";
142       }
143       $newdate = sprintf("%04d-%02d-%02d",$date[0],$date[1],$date[2]);
144       return $newdate;
145     }
146 }
147
148 sub check_whether_iso
149 {
150     my $olddate = shift;
151     my @olddate= split /\-/,$olddate ;
152     return 1 if (length($olddate[0])==4 && length($olddate[1])<=2 && length($olddate[2])<=2);
153     return 0;
154 }
155 1;