Filename | /usr/share/koha/lib/C4/Dates.pm |
Statements | Executed 42 statements in 2.04ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 1.53ms | 2.40ms | BEGIN@29 | C4::Dates::
1 | 1 | 1 | 21µs | 25µs | BEGIN@22 | C4::Dates::
1 | 1 | 1 | 14µs | 14µs | BEGIN@33 | C4::Dates::
1 | 1 | 1 | 12µs | 130µs | BEGIN@26 | C4::Dates::
1 | 1 | 1 | 11µs | 13µs | BEGIN@25 | C4::Dates::
1 | 1 | 1 | 11µs | 2.98ms | BEGIN@28 | C4::Dates::
1 | 1 | 1 | 11µs | 30µs | BEGIN@23 | C4::Dates::
1 | 1 | 1 | 11µs | 30µs | BEGIN@27 | C4::Dates::
1 | 1 | 1 | 11µs | 58µs | BEGIN@24 | C4::Dates::
1 | 1 | 1 | 10µs | 85µs | BEGIN@30 | C4::Dates::
1 | 1 | 1 | 10µs | 39µs | BEGIN@31 | C4::Dates::
1 | 1 | 1 | 9µs | 31µs | BEGIN@39 | C4::Dates::
0 | 0 | 0 | 0s | 0s | DHTMLcalendar | C4::Dates::
0 | 0 | 0 | 0s | 0s | _abbr_to_numeric | C4::Dates::
0 | 0 | 0 | 0s | 0s | _check_date_and_time | C4::Dates::
0 | 0 | 0 | 0s | 0s | _chron_to_hms | C4::Dates::
0 | 0 | 0 | 0s | 0s | _chron_to_ymd | C4::Dates::
0 | 0 | 0 | 0s | 0s | _prefformat | C4::Dates::
0 | 0 | 0 | 0s | 0s | _recognize_format | C4::Dates::
0 | 0 | 0 | 0s | 0s | dmy_map | C4::Dates::
0 | 0 | 0 | 0s | 0s | format | C4::Dates::
0 | 0 | 0 | 0s | 0s | format_date | C4::Dates::
0 | 0 | 0 | 0s | 0s | format_date_in_iso | C4::Dates::
0 | 0 | 0 | 0s | 0s | init | C4::Dates::
0 | 0 | 0 | 0s | 0s | new | C4::Dates::
0 | 0 | 0 | 0s | 0s | output | C4::Dates::
0 | 0 | 0 | 0s | 0s | regexp | C4::Dates::
0 | 0 | 0 | 0s | 0s | reset_prefformat | C4::Dates::
0 | 0 | 0 | 0s | 0s | today | C4::Dates::
0 | 0 | 0 | 0s | 0s | visual | C4::Dates::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package 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 | |||||
22 | 3 | 27µs | 2 | 29µ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 # spent 25µs making 1 call to C4::Dates::BEGIN@22
# spent 4µs making 1 call to strict::import |
23 | 3 | 56µs | 2 | 49µ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 # spent 30µs making 1 call to C4::Dates::BEGIN@23
# spent 19µs making 1 call to warnings::import |
24 | 3 | 30µs | 2 | 106µ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 # spent 58µs making 1 call to C4::Dates::BEGIN@24
# spent 48µs making 1 call to Exporter::import |
25 | 3 | 24µs | 2 | 15µ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 # spent 13µs making 1 call to C4::Dates::BEGIN@25
# spent 2µs making 1 call to C4::Context::import |
26 | 3 | 34µs | 2 | 249µ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 # spent 130µs making 1 call to C4::Dates::BEGIN@26
# spent 118µs making 1 call to Exporter::import |
27 | 3 | 29µs | 2 | 50µ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 # spent 30µs making 1 call to C4::Dates::BEGIN@27
# spent 20µs making 1 call to Exporter::import |
28 | 3 | 50µs | 2 | 5.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 # spent 2.98ms making 1 call to C4::Dates::BEGIN@28
# spent 2.97ms making 1 call to POSIX::import |
29 | 3 | 187µs | 2 | 2.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 # spent 2.40ms making 1 call to C4::Dates::BEGIN@29
# spent 198µs making 1 call to Exporter::import |
30 | 3 | 36µs | 2 | 160µ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 # spent 85µs making 1 call to C4::Dates::BEGIN@30
# spent 75µs making 1 call to vars::import |
31 | 3 | 51µs | 2 | 68µ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 # 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 | ||||
34 | 3 | 15µs | $VERSION = 3.07.00.049; | ||
35 | @ISA = qw(Exporter); | ||||
36 | @EXPORT_OK = qw(format_date_in_iso format_date); | ||||
37 | 1 | 26µs | 1 | 14µs | } # spent 14µs making 1 call to C4::Dates::BEGIN@33 |
38 | |||||
39 | 3 | 1.45ms | 2 | 52µ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 # spent 31µs making 1 call to C4::Dates::BEGIN@39
# spent 22µs making 1 call to vars::import |
40 | |||||
41 | sub _prefformat { | ||||
42 | unless ( defined $prefformat ) { | ||||
43 | $prefformat = C4::Context->preference('dateformat'); | ||||
44 | } | ||||
45 | return $prefformat; | ||||
46 | } | ||||
47 | |||||
48 | sub 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 | |||||
54 | 1 | 4µs | our %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 | ); | ||||
61 | 1 | 2µs | our %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 | |||||
69 | 1 | 2µs | our %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 | |||||
78 | 1 | 2µs | our @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | ||
79 | |||||
80 | 1 | 1µs | our @days = qw(Sun Mon Tue Wed Thu Fri Sat); | ||
81 | |||||
82 | sub 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 | |||||
100 | sub 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 | |||||
123 | sub _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 | |||||
144 | sub _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 | |||||
156 | sub _chron_to_ymd { | ||||
157 | my $chron_ref = shift; | ||||
158 | return ( $chron_ref->[5] + 1900, $chron_ref->[4] + 1, $chron_ref->[3] ); | ||||
159 | } | ||||
160 | |||||
161 | sub _chron_to_hms { | ||||
162 | my $chron_ref = shift; | ||||
163 | return ( $chron_ref->[2], $chron_ref->[1], $chron_ref->[0] ); | ||||
164 | } | ||||
165 | |||||
166 | sub new { | ||||
167 | my $this = shift; | ||||
168 | my $class = ref($this) || $this; | ||||
169 | my $self = {}; | ||||
170 | bless $self, $class; | ||||
171 | return $self->init(@_); | ||||
172 | } | ||||
173 | |||||
174 | sub 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 | |||||
184 | sub 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 | |||||
190 | sub 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 | |||||
197 | sub _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 | |||||
204 | sub DHTMLcalendar ($;$) { # interface to posix_map | ||||
205 | my $class = shift; | ||||
206 | my $format = (@_) ? shift : _prefformat(); | ||||
207 | return $posix_map{$format}; | ||||
208 | } | ||||
209 | |||||
210 | sub 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 | |||||
216 | sub 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 | ||||
226 | sub format_date { | ||||
227 | return __PACKAGE__->new( shift, 'iso' )->output( (@_) ? shift : _prefformat() ); | ||||
228 | } | ||||
229 | |||||
230 | sub format_date_in_iso { | ||||
231 | return __PACKAGE__->new( shift, _prefformat() )->output('iso'); | ||||
232 | } | ||||
233 | |||||
234 | 1 | 10µs | 1; | ||
235 | __END__ |