Fixed problems with non-US date such as 28/10/1983.
[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::Manip;
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 );
21
22
23
24 sub get_date_format
25 {
26         #Get the database handle
27         my $dbh = C4::Context->dbh;
28
29         #Query the database to get the dateformat
30         my $sth = $dbh->prepare("SELECT value FROM systempreferences WHERE variable='dateformat'");
31
32         $sth->execute();
33
34         my ($dateformat) = $sth->fetchrow;
35         
36         return $dateformat
37 }
38
39 sub display_date_format
40 {
41         my $dateformat = get_date_format();
42         
43         if ( $dateformat eq "us" )
44         {
45                 return "mm/dd/yyyy";
46         }
47         elsif ( $dateformat eq "metric" )
48         {
49                 return "dd/mm/yyyy";
50         }
51         elsif ( $dateformat eq "iso" )
52         {
53                 return "yyyy-mm-dd";
54         }
55         else
56         {
57                 return "Invalid date format: $dateformat. Please change in system preferences";
58         }
59 }
60
61
62 sub format_date
63 {
64         my $olddate = shift;
65         my $newdate;
66
67         my $dateformat = get_date_format();
68         
69         if ( $dateformat eq "us" )
70         {
71                 Date_Init("DateFormat=US");
72                 $olddate = ParseDate($olddate);
73                 $newdate = UnixDate($olddate,'%m/%d/%Y');
74         }
75         elsif ( $dateformat eq "metric" )
76         {
77                 Date_Init("DateFormat=metric");
78                 $olddate = ParseDate($olddate);
79                 $newdate = UnixDate($olddate,'%d/%m/%Y');
80         }
81         elsif ( $dateformat eq "iso" )
82         {
83                 Date_Init("DateFormat=iso");
84                 $olddate = ParseDate($olddate);
85                 $newdate = UnixDate($olddate,'%Y-%m-%d');
86         }
87         else
88         {
89                 return "Invalid date format: $dateformat. Please change in system preferences";
90         }
91 }
92
93 1;