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