2 ## written by T Garip 2006-10-10 tgarip@neu.edu.tr
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
51 #Get the database handle
52 my $dbh = C4::Context->dbh;
53 return C4::Context->preference('dateformat');
56 sub display_date_format {
57 my $dateformat = get_date_format();
59 if ( $dateformat eq "us" ) {
62 elsif ( $dateformat eq "metric" ) {
65 elsif ( $dateformat eq "iso" ) {
70 "Invalid date format: $dateformat. Please change in system preferences";
74 sub get_date_format_string_for_DHTMLcalendar {
75 my $dateformat = get_date_format();
77 if ( $dateformat eq 'us' ) {
80 elsif ( $dateformat eq 'metric' ) {
83 elsif ( $dateformat eq "iso" ) {
87 return 'Invalid date format: '
89 . ' Please change in system preferences';
96 if ( !$olddate || $olddate eq "0000-00-00" ) {
100 my $olddate=substr($olddate,0,8);
101 my $dateformat = get_date_format();
102 eval{$newdate =DateTime::Format::ISO8601->parse_datetime($olddate);};
103 if ($@ || !$newdate){
104 ##MARC21 tag 008 has this format YYMMDD
105 my $parser = DateTime::Format::Strptime->new( pattern => '%y%m%d' );
106 $newdate =$parser->parse_datetime($olddate);
109 return ""; #### some script call format_date more than once --FIX scripts
112 if ( $dateformat eq "us" ) {
113 return $newdate->mdy('/');
116 elsif ( $dateformat eq "metric" ) {
117 return $newdate->dmy('/');
119 elsif ( $dateformat eq "iso" ) {
120 return $newdate->ymd;
124 "Invalid date format: $dateformat. Please change in system preferences";
129 sub format_date_in_iso {
133 if ( !$olddate || $olddate eq "0000-00-00" ) {
137 $parser = DateTime::Format::Strptime->new( pattern => '%d/%m/%Y' );
138 $newdate =$parser->parse_datetime($olddate);
140 $parser = DateTime::Format::Strptime->new( pattern => '%m/%d/%Y' );
141 $newdate =$parser->parse_datetime($olddate);
144 $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
145 $newdate =$parser->parse_datetime($olddate);
148 $parser = DateTime::Format::Strptime->new( pattern => '%y-%m-%d' );
149 $newdate =$parser->parse_datetime($olddate);
152 return $newdate->ymd if $newdate;
155 ## returns 1 if date1>date2 0 if date1==date2 -1 if date1<date2
156 my ($date1,$date2)=@_;
157 my $dt1=DateTime::Format::ISO8601->parse_datetime($date1);
158 my $dt2=DateTime::Format::ISO8601->parse_datetime($date2);
159 my $diff=DateTime->compare( $dt1, $dt2 );
164 my ($date,$amount)=@_;
165 my $dt1=DateTime::Format::ISO8601->parse_datetime($date);
166 $dt1->add( days=>$amount );
169 sub DATE_Add_Duration {
170 ## Similar as above but uses Duration object as amount --used heavily in serials
171 my ($date,$amount)=@_;
172 my $dt1=DateTime::Format::ISO8601->parse_datetime($date);
173 $dt1->add_duration($amount) ;
177 my $dt=DateTime->today;
182 # only send iso dates to this
184 my $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d' );
185 my $newdate =$parser->parse_datetime($date);
193 }elsif ($period=~/week/){
195 }elsif ($period=~/year/){
197 }elsif ($period=~/month/){
200 my $parser=DateTime::Format::Duration->new(pattern => $parse );
201 my $duration=$parser->parse_duration($period);
206 my ($date1,$date2)=@_;
207 my $dt1=DateTime::Format::ISO8601->parse_datetime($date1);
208 my $dt2=DateTime::Format::ISO8601->parse_datetime($date2);
209 my $dur=$dt2->subtract_datetime_absolute($dt1);## in seconds
210 my $days=$dur->seconds/(60*60*24);