Added format_date_in_iso function.
[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              &format_date_in_iso
21 );
22
23
24
25 sub get_date_format
26 {
27         #Get the database handle
28         my $dbh = C4::Context->dbh;
29
30         #Query the database to get the dateformat
31         my $sth = $dbh->prepare("SELECT value FROM systempreferences WHERE variable='dateformat'");
32
33         $sth->execute();
34
35         my ($dateformat) = $sth->fetchrow;
36         
37         return $dateformat
38 }
39
40 sub display_date_format
41 {
42         my $dateformat = get_date_format();
43         
44         if ( $dateformat eq "us" )
45         {
46                 return "mm/dd/yyyy";
47         }
48         elsif ( $dateformat eq "metric" )
49         {
50                 return "dd/mm/yyyy";
51         }
52         elsif ( $dateformat eq "iso" )
53         {
54                 return "yyyy-mm-dd";
55         }
56         else
57         {
58                 return "Invalid date format: $dateformat. Please change in system preferences";
59         }
60 }
61
62
63 sub format_date
64 {
65         my $olddate = shift;
66         my $newdate;
67
68         my $dateformat = get_date_format();
69         
70         if ( $dateformat eq "us" )
71         {
72                 Date_Init("DateFormat=US");
73                 $olddate = ParseDate($olddate);
74                 $newdate = UnixDate($olddate,'%m/%d/%Y');
75         }
76         elsif ( $dateformat eq "metric" )
77         {
78                 Date_Init("DateFormat=metric");
79                 $olddate = ParseDate($olddate);
80                 $newdate = UnixDate($olddate,'%d/%m/%Y');
81         }
82         elsif ( $dateformat eq "iso" )
83         {
84                 Date_Init("DateFormat=iso");
85                 $olddate = ParseDate($olddate);
86                 $newdate = UnixDate($olddate,'%Y-%m-%d');
87         }
88         else
89         {
90                 return "Invalid date format: $dateformat. Please change in system preferences";
91         }
92 }
93
94 sub format_date_in_iso
95 {
96         my $olddate = shift;
97         my $newdate;
98                 
99         my $dateformat = get_date_format();
100
101         if ( $dateformat eq "us" )
102         {
103                 Date_Init("DateFormat=US");
104                 $olddate = ParseDate($olddate);
105         }
106         elsif ( $dateformat eq "metric" )
107         {
108                 Date_Init("DateFormat=metric");
109                 $olddate = ParseDate($olddate);
110         }
111         elsif ( $dateformat eq "iso" )
112         {
113                 Date_Init("DateFormat=iso");
114                 $olddate = ParseDate($olddate);
115         }
116         else
117         {
118                 return "9999-99-99";
119         }
120
121         $newdate = UnixDate($olddate, '%Y-%m-%d');
122
123         return $newdate;
124 }
125 1;