add call to doc-head-open.inc and doc-head-close.inc
[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
27 require Exporter;
28
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30
31 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
32     shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v );
33 };
34
35 @ISA = qw(Exporter);
36
37 @EXPORT = qw(
38   &display_date_format
39   &format_date
40   &format_date_in_iso
41 );
42
43 =head1 DESCRIPTION
44
45 C4::Circulation::Date provides routines for format dates to display in human readable forms.
46
47 =head1 FUNCTIONS
48
49 =over 2
50
51 =cut
52
53 =head2 get_date_format
54
55   $dateformat = get_date_format();
56
57 Takes no input, and returns the format that the library prefers dates displayed in
58
59
60 =cut
61
62 sub get_date_format {
63
64     # Get the database handle
65     my $dbh = C4::Context->dbh;
66     return C4::Context->preference('dateformat');
67 }
68
69 =head2 display_date_format
70
71   $displaydateformat = display_date_format();
72
73 Takes no input, and returns a string showing the format the library likes dates displayed in
74
75
76 =cut
77
78 sub display_date_format {
79     my $dateformat = get_date_format();
80
81     if ( $dateformat eq "us" ) {
82         return "mm/dd/yyyy";
83     }
84     elsif ( $dateformat eq "metric" ) {
85         return "dd/mm/yyyy";
86     }
87     elsif ( $dateformat eq "iso" ) {
88         return "yyyy-mm-dd";
89     }
90     else {
91         return
92 "Invalid date format: $dateformat. Please change in system preferences";
93     }
94 }
95
96 =head2 format_date
97
98   $formatteddate = format_date($date);
99
100 Takes a date, from mysql and returns it in the format specified by the library
101 This is less flexible than C4::Date::format_date, which can handle dates of many formats 
102 if you need that flexibility use C4::Date, if you are just using it to format the output from mysql as
103 in circulation.pl use this one, it is much faster.
104 =cut
105
106
107 sub format_date {
108     my $olddate = shift;
109     my $newdate;
110
111     if ( !$olddate ) {
112         return "";
113     }
114
115     my $dateformat = get_date_format();
116
117     if ( $dateformat eq "us" ) {
118         my @datearray=split('-',$olddate);
119         $newdate = "$datearray[1]/$datearray[2]/$datearray[0]";
120     }
121     elsif ( $dateformat eq "metric" ) {
122         my @datearray=split('-',$olddate);
123         $newdate = "$datearray[2]/$datearray[1]/$datearray[0]";
124     }
125     elsif ( $dateformat eq "iso" ) {
126         $newdate = $olddate;
127     }
128     else {
129         return
130 "Invalid date format: $dateformat. Please change in system preferences";
131     }
132 }
133
134 1;