← Index
NYTProf Performance Profile   « block view • line view • sub view »
For /usr/share/koha/opac/cgi-bin/opac/opac-search.pl
  Run on Tue Oct 15 17:10:45 2013
Reported on Tue Oct 15 17:11:22 2013

Filename/usr/share/koha/lib/C4/Dates.pm
StatementsExecuted 42 statements in 2.04ms
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
1111.53ms2.40msC4::Dates::::BEGIN@29C4::Dates::BEGIN@29
11121µs25µsC4::Dates::::BEGIN@22C4::Dates::BEGIN@22
11114µs14µsC4::Dates::::BEGIN@33C4::Dates::BEGIN@33
11112µs130µsC4::Dates::::BEGIN@26C4::Dates::BEGIN@26
11111µs13µsC4::Dates::::BEGIN@25C4::Dates::BEGIN@25
11111µs2.98msC4::Dates::::BEGIN@28C4::Dates::BEGIN@28
11111µs30µsC4::Dates::::BEGIN@23C4::Dates::BEGIN@23
11111µs30µsC4::Dates::::BEGIN@27C4::Dates::BEGIN@27
11111µs58µsC4::Dates::::BEGIN@24C4::Dates::BEGIN@24
11110µs85µsC4::Dates::::BEGIN@30C4::Dates::BEGIN@30
11110µs39µsC4::Dates::::BEGIN@31C4::Dates::BEGIN@31
1119µs31µsC4::Dates::::BEGIN@39C4::Dates::BEGIN@39
0000s0sC4::Dates::::DHTMLcalendarC4::Dates::DHTMLcalendar
0000s0sC4::Dates::::_abbr_to_numericC4::Dates::_abbr_to_numeric
0000s0sC4::Dates::::_check_date_and_timeC4::Dates::_check_date_and_time
0000s0sC4::Dates::::_chron_to_hmsC4::Dates::_chron_to_hms
0000s0sC4::Dates::::_chron_to_ymdC4::Dates::_chron_to_ymd
0000s0sC4::Dates::::_prefformatC4::Dates::_prefformat
0000s0sC4::Dates::::_recognize_formatC4::Dates::_recognize_format
0000s0sC4::Dates::::dmy_mapC4::Dates::dmy_map
0000s0sC4::Dates::::formatC4::Dates::format
0000s0sC4::Dates::::format_dateC4::Dates::format_date
0000s0sC4::Dates::::format_date_in_isoC4::Dates::format_date_in_iso
0000s0sC4::Dates::::initC4::Dates::init
0000s0sC4::Dates::::newC4::Dates::new
0000s0sC4::Dates::::outputC4::Dates::output
0000s0sC4::Dates::::regexpC4::Dates::regexp
0000s0sC4::Dates::::reset_prefformatC4::Dates::reset_prefformat
0000s0sC4::Dates::::todayC4::Dates::today
0000s0sC4::Dates::::visualC4::Dates::visual
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package C4::Dates;
2
3# Copyright 2007 Liblime
4# Parts Copyright ACPL 2011
5# Parts Copyright Catalyst IT 2012
6#
7# This file is part of Koha.
8#
9# Koha is free software; you can redistribute it and/or modify it under the
10# terms of the GNU General Public License as published by the Free Software
11# Foundation; either version 2 of the License, or (at your option) any later
12# version.
13#
14# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with Koha; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22327µs229µs
# spent 25µs (21+4) within C4::Dates::BEGIN@22 which was called: # once (21µs+4µs) by C4::Output::BEGIN@34 at line 22
use strict;
# spent 25µs making 1 call to C4::Dates::BEGIN@22 # spent 4µs making 1 call to strict::import
23356µs249µs
# spent 30µs (11+19) within C4::Dates::BEGIN@23 which was called: # once (11µs+19µs) by C4::Output::BEGIN@34 at line 23
use warnings;
# spent 30µs making 1 call to C4::Dates::BEGIN@23 # spent 19µs making 1 call to warnings::import
24330µs2106µs
# spent 58µs (11+48) within C4::Dates::BEGIN@24 which was called: # once (11µs+48µs) by C4::Output::BEGIN@34 at line 24
use Carp;
# spent 58µs making 1 call to C4::Dates::BEGIN@24 # spent 48µs making 1 call to Exporter::import
25324µs215µs
# spent 13µs (11+2) within C4::Dates::BEGIN@25 which was called: # once (11µs+2µs) by C4::Output::BEGIN@34 at line 25
use C4::Context;
# spent 13µs making 1 call to C4::Dates::BEGIN@25 # spent 2µs making 1 call to C4::Context::import
26334µs2249µs
# spent 130µs (12+118) within C4::Dates::BEGIN@26 which was called: # once (12µs+118µs) by C4::Output::BEGIN@34 at line 26
use C4::Debug;
# spent 130µs making 1 call to C4::Dates::BEGIN@26 # spent 118µs making 1 call to Exporter::import
27329µs250µs
# spent 30µs (11+20) within C4::Dates::BEGIN@27 which was called: # once (11µs+20µs) by C4::Output::BEGIN@34 at line 27
use Exporter;
# spent 30µs making 1 call to C4::Dates::BEGIN@27 # spent 20µs making 1 call to Exporter::import
28350µs25.95ms
# spent 2.98ms (11µs+2.97) within C4::Dates::BEGIN@28 which was called: # once (11µs+2.97ms) by C4::Output::BEGIN@34 at line 28
use POSIX qw(strftime);
# spent 2.98ms making 1 call to C4::Dates::BEGIN@28 # spent 2.97ms making 1 call to POSIX::import
293187µs22.59ms
# spent 2.40ms (1.53+863µs) within C4::Dates::BEGIN@29 which was called: # once (1.53ms+863µs) by C4::Output::BEGIN@34 at line 29
use Date::Calc qw(check_date check_time);
# spent 2.40ms making 1 call to C4::Dates::BEGIN@29 # spent 198µs making 1 call to Exporter::import
30336µs2160µs
# spent 85µs (10+75) within C4::Dates::BEGIN@30 which was called: # once (10µs+75µs) by C4::Output::BEGIN@34 at line 30
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
# spent 85µs making 1 call to C4::Dates::BEGIN@30 # spent 75µs making 1 call to vars::import
31351µs268µs
# spent 39µs (10+29) within C4::Dates::BEGIN@31 which was called: # once (10µs+29µs) by C4::Output::BEGIN@34 at line 31
use vars qw($debug $cgi_debug);
# spent 39µs making 1 call to C4::Dates::BEGIN@31 # spent 29µs making 1 call to vars::import
32
33
# spent 14µs within C4::Dates::BEGIN@33 which was called: # once (14µs+0s) by C4::Output::BEGIN@34 at line 37
BEGIN {
34315µs $VERSION = 3.07.00.049;
35 @ISA = qw(Exporter);
36 @EXPORT_OK = qw(format_date_in_iso format_date);
37126µs114µs}
# spent 14µs making 1 call to C4::Dates::BEGIN@33
38
3931.45ms252µs
# spent 31µs (9+22) within C4::Dates::BEGIN@39 which was called: # once (9µs+22µs) by C4::Output::BEGIN@34 at line 39
use vars qw($prefformat);
# spent 31µs making 1 call to C4::Dates::BEGIN@39 # spent 22µs making 1 call to vars::import
40
41sub _prefformat {
42 unless ( defined $prefformat ) {
43 $prefformat = C4::Context->preference('dateformat');
44 }
45 return $prefformat;
46}
47
48sub reset_prefformat { # subroutine to clear the prefformat, called when we change it
49 if (defined $prefformat){
50 $prefformat = C4::Context->preference('dateformat');
51 }
52}
53
5414µsour %format_map = (
55 iso => 'yyyy-mm-dd', # plus " HH:MM:SS"
56 metric => 'dd/mm/yyyy', # plus " HH:MM:SS"
57 us => 'mm/dd/yyyy', # plus " HH:MM:SS"
58 sql => 'yyyymmdd HHMMSS',
59 rfc822 => 'a, dd b y HH:MM:SS z ',
60);
6112µsour %posix_map = (
62 iso => '%Y-%m-%d', # or %F, "Full Date"
63 metric => '%d/%m/%Y',
64 us => '%m/%d/%Y',
65 sql => '%Y%m%d %H%M%S',
66 rfc822 => '%a, %d %b %Y %H:%M:%S %z',
67);
68
6912µsour %dmy_subs = ( # strings to eval (after using regular expression returned by regexp below)
70 # make arrays for POSIX::strftime()
71 iso => '[(($6||0),($5||0),($4||0),$3, $2 - 1, $1 - 1900)]',
72 metric => '[(($6||0),($5||0),($4||0),$1, $2 - 1, $3 - 1900)]',
73 us => '[(($6||0),($5||0),($4||0),$2, $1 - 1, $3 - 1900)]',
74 sql => '[(($6||0),($5||0),($4||0),$3, $2 - 1, $1 - 1900)]',
75 rfc822 => '[($7, $6, $5, $2, $3, $4 - 1900, $8)]',
76);
77
7812µsour @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
79
8011µsour @days = qw(Sun Mon Tue Wed Thu Fri Sat);
81
82sub regexp ($;$) {
83 my $self = shift;
84 my $delim = qr/:?\:|\/|-/; # "non memory" cluster: no backreference
85 my $format = (@_) ? _recognize_format(shift) : ( $self->{'dateformat'} || _prefformat() );
86
87 # Extra layer of checking $self->{'dateformat'}.
88 # Why? Because it is assumed you might want to check regexp against an *instantiated* Dates object as a
89 # way of saying "does this string match *whatever* format that Dates object is?"
90
91 ( $format eq 'sql' )
92 and return qr/^(\d{4})(\d{1,2})(\d{1,2})(?:\s{4}(\d{2})(\d{2})(\d{2}))?/;
93 ( $format eq 'iso' )
94 and return qr/^(\d{4})$delim(\d{1,2})$delim(\d{1,2})(?:(?:\s{1}|T)(\d{2})\:?(\d{2})\:?(\d{2}))?Z?/;
95 ( $format eq 'rfc822' )
96 and return qr/^([a-zA-Z]{3}),\s{1}(\d{1,2})\s{1}([a-zA-Z]{3})\s{1}(\d{4})\s{1}(\d{1,2})\:(\d{1,2})\:(\d{1,2})\s{1}(([\-|\+]\d{4})|([A-Z]{3}))/;
97 return qr/^(\d{1,2})$delim(\d{1,2})$delim(\d{4})(?:\s{1}(\d{1,2})\:?(\d{1,2})\:?(\d{1,2}))?/; # everything else
98}
99
100sub dmy_map ($$) {
101 my $self = shift;
102 my $val = shift or return undef;
103 my $dformat = $self->{'dateformat'} or return undef;
104 my $re = $self->regexp();
105 my $xsub = $dmy_subs{$dformat};
106 $debug and print STDERR "xsub: $xsub \n";
107 if ( $val =~ /$re/ ) {
108 my $aref = eval $xsub;
109 if ($dformat eq 'rfc822') {
110 $aref = _abbr_to_numeric($aref, $dformat);
111 pop(@{$aref}); #pop off tz offset because we are not setup to handle tz conversions just yet
112 }
113 _check_date_and_time($aref);
114 push @{$aref}, (-1,-1,1); # for some reason unknown to me, setting isdst to -1 or undef causes strftime to fail to return the tz offset which is required in RFC822 format -chris_n
115 return @{$aref};
116 }
117
118 # $debug and
119 carp "Illegal Date '$val' does not match '$dformat' format: " . $self->visual();
120 return 0;
121}
122
123sub _abbr_to_numeric {
124 my $aref = shift;
125 my $dformat = shift;
126 my ($month_abbr, $day_abbr) = ($aref->[4], $aref->[3]) if $dformat eq 'rfc822';
127
128 for( my $i = 0; $i < scalar(@months); $i++ ) {
129 if ( $months[$i] =~ /$month_abbr/ ) {
130 $aref->[4] = $i-1;
131 last;
132 }
133 };
134
135 for( my $i = 0; $i < scalar(@days); $i++ ) {
136 if ( $days[$i] =~ /$day_abbr/ ) {
137 $aref->[3] = $i;
138 last;
139 }
140 };
141 return $aref;
142}
143
144sub _check_date_and_time {
145 my $chron_ref = shift;
146 my ( $year, $month, $day ) = _chron_to_ymd($chron_ref);
147 unless ( check_date( $year, $month, $day ) ) {
148 carp "Illegal date specified (year = $year, month = $month, day = $day)";
149 }
150 my ( $hour, $minute, $second ) = _chron_to_hms($chron_ref);
151 unless ( check_time( $hour, $minute, $second ) ) {
152 carp "Illegal time specified (hour = $hour, minute = $minute, second = $second)";
153 }
154}
155
156sub _chron_to_ymd {
157 my $chron_ref = shift;
158 return ( $chron_ref->[5] + 1900, $chron_ref->[4] + 1, $chron_ref->[3] );
159}
160
161sub _chron_to_hms {
162 my $chron_ref = shift;
163 return ( $chron_ref->[2], $chron_ref->[1], $chron_ref->[0] );
164}
165
166sub new {
167 my $this = shift;
168 my $class = ref($this) || $this;
169 my $self = {};
170 bless $self, $class;
171 return $self->init(@_);
172}
173
174sub init ($;$$) {
175 my $self = shift;
176 my $dformat;
177 $self->{'dateformat'} = $dformat = ( scalar(@_) >= 2 ) ? $_[1] : _prefformat();
178 ( $format_map{$dformat} ) or croak "Invalid date format '$dformat' from " . ( ( scalar(@_) >= 2 ) ? 'argument' : 'system preferences' );
179 $self->{'dmy_arrayref'} = [ ( (@_) ? $self->dmy_map(shift) : localtime ) ];
180 if ($debug && $debug > 1) { warn "(during init) \@\$self->{'dmy_arrayref'}: " . join( ' ', @{ $self->{'dmy_arrayref'} } ) . "\n"; }
181 return $self;
182}
183
184sub output ($;$) {
185 my $self = shift;
186 my $newformat = (@_) ? _recognize_format(shift) : _prefformat();
187 return ( eval { POSIX::strftime( $posix_map{$newformat}, @{ $self->{'dmy_arrayref'} } ) } || undef );
188}
189
190sub today ($;$) { # NOTE: sets date value to today (and returns it in the requested or current format)
191 my $class = shift;
192 $class = ref($class) || $class;
193 my $format = (@_) ? _recognize_format(shift) : _prefformat();
194 return $class->new()->output($format);
195}
196
197sub _recognize_format($) {
198 my $incoming = shift;
199 ( $incoming eq 'syspref' ) and return _prefformat();
200 ( scalar grep ( /^$incoming$/, keys %format_map ) == 1 ) or croak "The format you asked for ('$incoming') is unrecognized.";
201 return $incoming;
202}
203
204sub DHTMLcalendar ($;$) { # interface to posix_map
205 my $class = shift;
206 my $format = (@_) ? shift : _prefformat();
207 return $posix_map{$format};
208}
209
210sub format { # get or set dateformat: iso, metric, us, etc.
211 my $self = shift;
212 (@_) or return $self->{'dateformat'};
213 $self->{'dateformat'} = _recognize_format(shift);
214}
215
216sub visual {
217 my $self = shift;
218 if (@_) {
219 return $format_map{ _recognize_format(shift) };
220 }
221 $self eq __PACKAGE__ and return $format_map{ _prefformat() };
222 return $format_map{ eval { $self->{'dateformat'} } || _prefformat() };
223}
224
225# like the functions from the old C4::Date.pm
226sub format_date {
227 return __PACKAGE__->new( shift, 'iso' )->output( (@_) ? shift : _prefformat() );
228}
229
230sub format_date_in_iso {
231 return __PACKAGE__->new( shift, _prefformat() )->output('iso');
232}
233
234110µs1;
235__END__