fixing some misspellings, reformatting a bit, etc.
[koha.git] / C4 / Date.pm
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # $Id$
21
22 package C4::Date;
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; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
33
34 @ISA = qw(Exporter);
35
36 @EXPORT = qw(
37   &display_date_format
38   &format_date
39   &format_date_in_iso
40   &today
41   get_date_format_string_for_DHTMLcalendar
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     my $dateformat = get_date_format();
53
54     if ( $dateformat eq "us" ) {
55         return "mm/dd/yyyy";
56     }
57     elsif ( $dateformat eq "metric" ) {
58         return "dd/mm/yyyy";
59     }
60     elsif ( $dateformat eq "iso" ) {
61         return "yyyy-mm-dd";
62     }
63     else {
64         return
65 "Invalid date format: $dateformat. Please change in system preferences";
66     }
67 }
68
69 sub get_date_format_string_for_DHTMLcalendar {
70     my $dateformat = get_date_format();
71
72     if ( $dateformat eq 'us' ) {
73         return '%m/%d/%Y';
74     }
75     elsif ( $dateformat eq 'metric' ) {
76         return '%d/%m/%Y';
77     }
78     elsif ( $dateformat eq "iso" ) {
79         return '%Y-%m-%d';
80     }
81     else {
82         return 'Invalid date format: '
83           . $dateformat . '.'
84           . ' Please change in system preferences';
85     }
86 }
87
88 sub format_date {
89     my $olddate = shift;
90     my $newdate;
91
92     if ( !$olddate ) {
93         return "";
94     }
95
96     my $dateformat = get_date_format();
97
98     if ( $dateformat eq "us" ) {
99         Date_Init("DateFormat=US");
100         $olddate = ParseDate($olddate);
101         $newdate = UnixDate( $olddate, '%m/%d/%Y' );
102     }
103     elsif ( $dateformat eq "metric" ) {
104         Date_Init("DateFormat=metric");
105         $olddate = ParseDate($olddate);
106         $newdate = UnixDate( $olddate, '%d/%m/%Y' );
107     }
108     elsif ( $dateformat eq "iso" ) {
109         Date_Init("DateFormat=iso");
110         $olddate = ParseDate($olddate);
111         $newdate = UnixDate( $olddate, '%Y-%m-%d' );
112     }
113     else {
114         return
115 "Invalid date format: $dateformat. Please change in system preferences";
116     }
117 }
118
119 sub format_date_in_iso {
120     my $olddate = shift;
121     my $newdate;
122
123     if ( !$olddate ) {
124         return "";
125     }
126
127     my $dateformat = get_date_format();
128
129     if ( $dateformat eq "us" ) {
130         Date_Init("DateFormat=US");
131         $olddate = ParseDate($olddate);
132     }
133     elsif ( $dateformat eq "metric" ) {
134         Date_Init("DateFormat=metric");
135         $olddate = ParseDate($olddate);
136     }
137     elsif ( $dateformat eq "iso" ) {
138         Date_Init("DateFormat=iso");
139         $olddate = ParseDate($olddate);
140     }
141     else {
142         return "9999-99-99";
143     }
144
145     $newdate = UnixDate( $olddate, '%Y-%m-%d' );
146
147     return $newdate;
148 }
149
150 #function to return a current date OUEST-PROVENCE
151 sub today {
152     my ($adddate) = @_;
153     my ( $j, $m, $a ) = (localtime)[ 3, 4, 5 ];
154     if ( $j < 10 ) {
155         $j = '0' . $j;
156     }
157     $m = $m + 1;
158     if ( $m < 10 ) {
159         $m = '0' . $m;
160     }
161     $a = $a + 1900 + $adddate;
162     return format_date("$a-$m-$j");
163 }
164
165 1;