Here we go again...
[koha.git] / C4 / Date.pm
1 package C4::Date;
2
3 use strict;
4 use C4::Context;
5 use Date::Manip;
6
7 require Exporter;
8
9 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
10
11 $VERSION = 0.01;
12
13 @ISA = qw(Exporter);
14
15 @EXPORT = qw(
16              &display_date_format
17              &format_date
18              &format_date_in_iso
19 );
20
21 sub get_date_format
22 {
23         #Get the database handle
24         my $dbh = C4::Context->dbh;
25
26         #Query the database to get the dateformat
27         my $sth = $dbh->prepare("SELECT value FROM systempreferences WHERE variable='dateformat'");
28
29         $sth->execute();
30
31         my ($dateformat) = $sth->fetchrow;
32         
33         return $dateformat
34 }
35
36 sub display_date_format
37 {
38         my $dateformat = get_date_format();
39         
40         if ( $dateformat eq "us" )
41         {
42                 return "mm/dd/yyyy";
43         }
44         elsif ( $dateformat eq "metric" )
45         {
46                 return "dd/mm/yyyy";
47         }
48         elsif ( $dateformat eq "iso" )
49         {
50                 return "yyyy-mm-dd";
51         }
52         else
53         {
54                 return "Invalid date format: $dateformat. Please change in system preferences";
55         }
56 }
57
58
59 sub format_date
60 {
61         my $olddate = shift;
62         my $newdate;
63
64         my $dateformat = get_date_format();
65         
66         if ( $dateformat eq "us" )
67         {
68                 Date_Init("DateFormat=US");
69                 $olddate = ParseDate($olddate);
70                 $newdate = UnixDate($olddate,'%m/%d/%Y');
71         }
72         elsif ( $dateformat eq "metric" )
73         {
74                 Date_Init("DateFormat=metric");
75                 $olddate = ParseDate($olddate);
76                 $newdate = UnixDate($olddate,'%d/%m/%Y');
77         }
78         elsif ( $dateformat eq "iso" )
79         {
80                 Date_Init("DateFormat=iso");
81                 $olddate = ParseDate($olddate);
82                 $newdate = UnixDate($olddate,'%Y-%m-%d');
83         }
84         else
85         {
86                 return "Invalid date format: $dateformat. Please change in system preferences";
87         }
88 }
89
90 sub format_date_in_iso
91 {
92         my $olddate = shift;
93         my $newdate;
94                 
95         my $dateformat = get_date_format();
96
97         if ( $dateformat eq "us" )
98         {
99                 Date_Init("DateFormat=US");
100                 $olddate = ParseDate($olddate);
101         }
102         elsif ( $dateformat eq "metric" )
103         {
104                 Date_Init("DateFormat=metric");
105                 $olddate = ParseDate($olddate);
106         }
107         elsif ( $dateformat eq "iso" )
108         {
109                 Date_Init("DateFormat=iso");
110                 $olddate = ParseDate($olddate);
111         }
112         else
113         {
114                 return "9999-99-99";
115         }
116
117         $newdate = UnixDate($olddate, '%Y-%m-%d');
118
119         return $newdate;
120 }
121
122 1;
123 __END__