This is currently a copy of C4::Date
[koha.git] / C4 / Circulation / Date.pm
1 #!/usr/bin/perl -w
2
3 package C4::Circulation::Date;
4
5 # $Id:
6
7 # Copyright 2005 Katipo Communications                                                                                                                            
8 #                                                                                                                                                                      
9 # This file is part of Koha.                                                                                                                                           
10 #                                                                                                                                                                      
11 # Koha is free software; you can redistribute it and/or modify it under the                                                                                            
12 # terms of the GNU General Public License as published by the Free Software                                                                                            
13 # Foundation; either version 2 of the License, or (at your option) any later                                                                                           
14 # version.                                                                                                                                                             
15 #                                                                                                                                                                      
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY                                                                                              
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR                                                                                        
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                                                                                          
19 #                                                                                                                                                                      
20 # You should have received a copy of the GNU General Public License along with                                                                                         
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,                                                                                          
22 # Suite 330, Boston, MA  02111-1307 USA 
23
24 use strict;
25 use C4::Context;
26 use Date::Manip;
27
28 require Exporter;
29
30 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
31
32 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;                                                                                                                 
33                     shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };;
34
35 @ISA = qw(Exporter);
36
37 @EXPORT = qw(
38              &display_date_format
39              &format_date
40              &format_date_in_iso
41 );
42
43
44 sub get_date_format
45 {
46         #Get the database handle
47         my $dbh = C4::Context->dbh;
48         return C4::Context->preference('dateformat');
49 }
50
51 sub display_date_format
52 {
53         my $dateformat = get_date_format();
54
55         if ( $dateformat eq "us" )
56         {
57                 return "mm/dd/yyyy";
58         }
59         elsif ( $dateformat eq "metric" )
60         {
61                 return "dd/mm/yyyy";
62         }
63         elsif ( $dateformat eq "iso" )
64         {
65                 return "yyyy-mm-dd";
66         }
67         else
68         {
69                 return "Invalid date format: $dateformat. Please change in system preferences";
70         }
71 }
72
73
74 sub format_date
75 {
76         my $olddate = shift;
77         my $newdate;
78
79         if ( ! $olddate )
80         {
81                 return "";
82         }
83
84         my $dateformat = get_date_format();
85
86         if ( $dateformat eq "us" )
87         {
88                 Date_Init("DateFormat=US");
89                 $olddate = ParseDate($olddate);
90                 $newdate = UnixDate($olddate,'%m/%d/%Y');
91         }
92         elsif ( $dateformat eq "metric" )
93         {
94                 Date_Init("DateFormat=metric");
95                 $olddate = ParseDate($olddate);
96                 $newdate = UnixDate($olddate,'%d/%m/%Y');
97         }
98         elsif ( $dateformat eq "iso" )
99         {
100                 Date_Init("DateFormat=iso");
101                 $olddate = ParseDate($olddate);
102                 $newdate = UnixDate($olddate,'%Y-%m-%d');
103         }
104         else
105         {
106                 return "Invalid date format: $dateformat. Please change in system preferences";
107         }
108 }
109
110 sub format_date_in_iso
111 {
112         my $olddate = shift;
113         my $newdate;
114
115         if ( ! $olddate )
116         {
117                 return "";
118         }
119                 
120         my $dateformat = get_date_format();
121
122         if ( $dateformat eq "us" )
123         {
124                 Date_Init("DateFormat=US");
125                 $olddate = ParseDate($olddate);
126         }
127         elsif ( $dateformat eq "metric" )
128         {
129                 Date_Init("DateFormat=metric");
130                 $olddate = ParseDate($olddate);
131         }
132         elsif ( $dateformat eq "iso" )
133         {
134                 Date_Init("DateFormat=iso");
135                 $olddate = ParseDate($olddate);
136         }
137         else
138         {
139                 return "9999-99-99";
140         }
141
142         $newdate = UnixDate($olddate, '%Y-%m-%d');
143
144         return $newdate;
145 }
146 1;