new: function C4::Date::get_date_format_string_for_DHTMLcalendar based on
[koha_fer] / 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              get_date_format_string_for_DHTMLcalendar
22 );
23
24
25 sub get_date_format
26 {
27         #Get the database handle
28         my $dbh = C4::Context->dbh;
29         return C4::Context->preference('dateformat');
30 }
31
32 sub display_date_format
33 {
34         my $dateformat = get_date_format();
35
36         if ( $dateformat eq "us" )
37         {
38                 return "mm/dd/yyyy";
39         }
40         elsif ( $dateformat eq "metric" )
41         {
42                 return "dd/mm/yyyy";
43         }
44         elsif ( $dateformat eq "iso" )
45         {
46                 return "yyyy-mm-dd";
47         }
48         else
49         {
50                 return "Invalid date format: $dateformat. Please change in system preferences";
51         }
52 }
53
54 sub get_date_format_string_for_DHTMLcalendar {
55     my $dateformat = get_date_format();
56
57     if ($dateformat eq 'us') {
58         return '%m/%d/%Y';
59     }
60     elsif ($dateformat eq 'metric') {
61         return '%d/%m/%Y';
62     }
63     elsif ($dateformat eq "iso") {
64         return '%Y-%m-%d';
65     }
66     else {
67         return
68             'Invalid date format: '.$dateformat.'.'
69             .' 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;