Field weighting applied to ranked searches. A new facets table in mysql db
[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
29 require Exporter;
30
31 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
32
33 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; 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   &get_date_format_string_for_DHTMLcalendar
42   &Date_diff
43 );
44
45 sub get_date_format {
46
47     #Get the database handle
48     my $dbh = C4::Context->dbh;
49     return C4::Context->preference('dateformat');
50 }
51
52 sub display_date_format {
53     my $dateformat = get_date_format();
54
55     if ( $dateformat eq "us" ) {
56         return "mm/dd/yyyy";
57     }
58     elsif ( $dateformat eq "metric" ) {
59         return "dd/mm/yyyy";
60     }
61     elsif ( $dateformat eq "iso" ) {
62         return "yyyy-mm-dd";
63     }
64     else {
65         return
66 "Invalid date format: $dateformat. Please change in system preferences";
67     }
68 }
69
70 sub get_date_format_string_for_DHTMLcalendar {
71     my $dateformat = get_date_format();
72
73     if ( $dateformat eq 'us' ) {
74         return '%m/%d/%Y';
75     }
76     elsif ( $dateformat eq 'metric' ) {
77         return '%d/%m/%Y';
78     }
79     elsif ( $dateformat eq "iso" ) {
80         return '%Y-%m-%d';
81     }
82     else {
83         return 'Invalid date format: '
84           . $dateformat . '.'
85           . ' Please change in system preferences';
86     }
87 }
88
89 sub format_date {
90     my $olddate = shift;
91     my $newdate;
92
93     if ( !$olddate ) {
94         return "";
95     }
96
97     my $dateformat = get_date_format();
98
99     if ( $dateformat eq "us" ) {
100         Date_Init("DateFormat=US");
101         $olddate = ParseDate($olddate);
102         $newdate = UnixDate( $olddate, '%m/%d/%Y' );
103     }
104     elsif ( $dateformat eq "metric" ) {
105         Date_Init("DateFormat=metric");
106         $olddate = ParseDate($olddate);
107         $newdate = UnixDate( $olddate, '%d/%m/%Y' );
108     }
109     elsif ( $dateformat eq "iso" ) {
110         Date_Init("DateFormat=iso");
111         $olddate = ParseDate($olddate);
112         $newdate = UnixDate( $olddate, '%Y-%m-%d' );
113     }
114     else {
115         return
116 "Invalid date format: $dateformat. Please change in system preferences";
117     }
118 }
119
120 sub format_date_in_iso {
121     my $olddate = shift;
122     my $newdate;
123
124     if ( !$olddate ) {
125         return "";
126     }
127
128     my $dateformat = get_date_format();
129
130     if ( $dateformat eq "us" ) {
131         Date_Init("DateFormat=US");
132         $olddate = ParseDate($olddate);
133     }
134     elsif ( $dateformat eq "metric" ) {
135         Date_Init("DateFormat=metric");
136         $olddate = ParseDate($olddate);
137     }
138     elsif ( $dateformat eq "iso" ) {
139         Date_Init("DateFormat=iso");
140         $olddate = ParseDate($olddate);
141     }
142     else {
143         return "9999-99-99";
144     }
145
146     $newdate = UnixDate( $olddate, '%Y-%m-%d' );
147
148     return $newdate;
149 }
150 sub DATE_diff {
151 my ($date1,$date2)=@_;
152 my $dbh=C4::Context->dbh;
153 my $sth = $dbh->prepare("SELECT DATEDIFF(?,?)");
154         $sth->execute($date1,$date2);
155         my $difference = $sth->fetchrow;
156         $sth->finish;
157 return $difference;
158 }
159
160 1;