2 ## written by T Garip 2006-10-10
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
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
27 use DateTime::Format::ISO8601;
28 use DateTime::Format::Strptime;
29 use DateTime::Format::Duration;
33 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
43 &get_date_format_string_for_DHTMLcalendar
45 &get_today &DATE_Add_Duration &DATE_obj &get_duration
50 #Get the database handle
51 my $dbh = C4::Context->dbh;
52 return C4::Context->preference('dateformat');
55 sub display_date_format {
56 my $dateformat = get_date_format();
58 if ( $dateformat eq "us" ) {
61 elsif ( $dateformat eq "metric" ) {
64 elsif ( $dateformat eq "iso" ) {
69 "Invalid date format: $dateformat. Please change in system preferences";
73 sub get_date_format_string_for_DHTMLcalendar {
74 my $dateformat = get_date_format();
76 if ( $dateformat eq 'us' ) {
79 elsif ( $dateformat eq 'metric' ) {
82 elsif ( $dateformat eq "iso" ) {
86 return 'Invalid date format: '
88 . ' Please change in system preferences';
95 if ( !$olddate || $olddate eq "0000-00-00" ) {
99 my $olddate=substr($olddate,0,8);
100 my $dateformat = get_date_format();
101 eval{$newdate =DateTime::Format::ISO8601->parse_datetime($olddate);};
102 if ($@ || !$newdate){
103 ##MARC21 tag 008 has this format YYMMDD
104 my $parser = DateTime::Format::Strptime->new( pattern => '%y%m%d' );
105 $newdate =$parser->parse_datetime($olddate);
108 return ""; #### some script call format_date more than once --FIX scripts
111 if ( $dateformat eq "us" ) {
112 return $newdate->mdy('/');
115 elsif ( $dateformat eq "metric" ) {
116 return $newdate->dmy('/');
118 elsif ( $dateformat eq "iso" ) {
119 return $newdate->ymd;
123 "Invalid date format: $dateformat. Please change in system preferences";
128 sub format_date_in_iso {
132 if ( !$olddate || $olddate eq "0000-00-00" ) {
136 $parser = DateTime::Format::Strptime->new( pattern => '%d/%m/%Y' );
137 $newdate =$parser->parse_datetime($olddate);
139 $parser = DateTime::Format::Strptime->new( pattern => '%m/%d/%Y' );
140 $newdate =$parser->parse_datetime($olddate);
143 $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
144 $newdate =$parser->parse_datetime($olddate);
147 $parser = DateTime::Format::Strptime->new( pattern => '%y-%m-%d' );
148 $newdate =$parser->parse_datetime($olddate);
151 return $newdate->ymd if $newdate;
154 ## returns 1 if date1>date2 0 if date1==date2 -1 if date1<date2
155 my ($date1,$date2)=@_;
156 my $dt1=DateTime::Format::ISO8601->parse_datetime($date1);
157 my $dt2=DateTime::Format::ISO8601->parse_datetime($date2);
158 my $diff=DateTime->compare( $dt1, $dt2 );
163 my ($date,$amount)=@_;
164 my $dt1=DateTime::Format::ISO8601->parse_datetime($date);
165 $dt1->add( days=>$amount );
168 sub DATE_Add_Duration {
169 ## Similar as above but uses Duration object as amount --used heavily in serials
170 my ($date,$amount)=@_;
171 my $dt1=DateTime::Format::ISO8601->parse_datetime($date);
172 $dt1->add_duration($amount) ;
176 my $dt=DateTime->today;
181 # only send iso dates to this
183 my $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
184 my $newdate =$parser->parse_datetime($date);
192 }elsif ($period=~/week/){
194 }elsif ($period=~/year/){
196 }elsif ($period=~/month/){
199 my $parser=DateTime::Format::Duration->new(pattern => $parse );
200 my $duration=$parser->parse_duration($period);