Filename | /usr/share/koha/lib/Koha/DateUtils.pm |
Statements | Executed 30 statements in 1.34ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 825µs | 5.71ms | BEGIN@23 | Koha::DateUtils::
1 | 1 | 1 | 71µs | 156µs | BEGIN@21 | Koha::DateUtils::
1 | 1 | 1 | 19µs | 85µs | BEGIN@21.7 | Koha::DateUtils::
1 | 1 | 1 | 18µs | 22µs | BEGIN@19 | Koha::DateUtils::
1 | 1 | 1 | 16µs | 89µs | BEGIN@26 | Koha::DateUtils::
1 | 1 | 1 | 15µs | 18µs | BEGIN@24 | Koha::DateUtils::
1 | 1 | 1 | 13µs | 36µs | BEGIN@27 | Koha::DateUtils::
1 | 1 | 1 | 13µs | 13µs | BEGIN@22 | Koha::DateUtils::
1 | 1 | 1 | 11µs | 24µs | BEGIN@20 | Koha::DateUtils::
0 | 0 | 0 | 0s | 0s | dt_from_string | Koha::DateUtils::
0 | 0 | 0 | 0s | 0s | format_sqldatetime | Koha::DateUtils::
0 | 0 | 0 | 0s | 0s | format_sqlduedatetime | Koha::DateUtils::
0 | 0 | 0 | 0s | 0s | output_pref | Koha::DateUtils::
0 | 0 | 0 | 0s | 0s | output_pref_due | Koha::DateUtils::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Koha::DateUtils; | ||||
2 | |||||
3 | # Copyright (c) 2011 PTFS-Europe Ltd. | ||||
4 | # This file is part of Koha. | ||||
5 | # | ||||
6 | # Koha is free software; you can redistribute it and/or modify it under the | ||||
7 | # terms of the GNU General Public License as published by the Free Software | ||||
8 | # Foundation; either version 2 of the License, or (at your option) any later | ||||
9 | # version. | ||||
10 | # | ||||
11 | # Koha is distributed in the hope that it will be useful, but WITHOUT ANY | ||||
12 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||||
13 | # A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||||
14 | # | ||||
15 | # You should have received a copy of the GNU General Public License along with | ||||
16 | # Koha; if not, write to the Free Software Foundation, Inc., | ||||
17 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
18 | |||||
19 | 3 | 34µs | 2 | 26µs | # spent 22µs (18+4) within Koha::DateUtils::BEGIN@19 which was called:
# once (18µs+4µs) by C4::Letters::BEGIN@32 at line 19 # spent 22µs making 1 call to Koha::DateUtils::BEGIN@19
# spent 4µs making 1 call to strict::import |
20 | 3 | 26µs | 2 | 37µs | # spent 24µs (11+13) within Koha::DateUtils::BEGIN@20 which was called:
# once (11µs+13µs) by C4::Letters::BEGIN@32 at line 20 # spent 24µs making 1 call to Koha::DateUtils::BEGIN@20
# spent 13µs making 1 call to warnings::import |
21 | 6 | 114µs | 3 | 308µs | use 5.010; # spent 156µs making 1 call to Koha::DateUtils::BEGIN@21
# spent 85µs making 1 call to Koha::DateUtils::BEGIN@21.7
# spent 66µs making 1 call to feature::import |
22 | 3 | 31µs | 1 | 13µs | # spent 13µs within Koha::DateUtils::BEGIN@22 which was called:
# once (13µs+0s) by C4::Letters::BEGIN@32 at line 22 # spent 13µs making 1 call to Koha::DateUtils::BEGIN@22 |
23 | 3 | 142µs | 1 | 5.71ms | # spent 5.71ms (825µs+4.88) within Koha::DateUtils::BEGIN@23 which was called:
# once (825µs+4.88ms) by C4::Letters::BEGIN@32 at line 23 # spent 5.71ms making 1 call to Koha::DateUtils::BEGIN@23 |
24 | 3 | 33µs | 2 | 22µs | # spent 18µs (15+3) within Koha::DateUtils::BEGIN@24 which was called:
# once (15µs+3µs) by C4::Letters::BEGIN@32 at line 24 # spent 18µs making 1 call to Koha::DateUtils::BEGIN@24
# spent 3µs making 1 call to C4::Context::import |
25 | |||||
26 | 3 | 37µs | 2 | 163µs | # spent 89µs (16+74) within Koha::DateUtils::BEGIN@26 which was called:
# once (16µs+74µs) by C4::Letters::BEGIN@32 at line 26 # spent 89µs making 1 call to Koha::DateUtils::BEGIN@26
# spent 74µs making 1 call to base::import |
27 | 4 | 911µs | 3 | 78µs | # spent 36µs (13+23) within Koha::DateUtils::BEGIN@27 which was called:
# once (13µs+23µs) by C4::Letters::BEGIN@32 at line 27 # spent 36µs making 1 call to Koha::DateUtils::BEGIN@27
# spent 23µs making 1 call to version::import
# spent 18µs making 1 call to version::__ANON__[version.pm:47] |
28 | |||||
29 | 1 | 2µs | our @EXPORT = ( | ||
30 | qw( dt_from_string output_pref format_sqldatetime output_pref_due format_sqlduedatetime) | ||||
31 | ); | ||||
32 | |||||
33 | =head1 DateUtils | ||||
34 | |||||
- - | |||||
45 | =head2 dt_ftom_string | ||||
46 | |||||
- - | |||||
54 | sub dt_from_string { | ||||
55 | my ( $date_string, $date_format, $tz ) = @_; | ||||
56 | if ( !$tz ) { | ||||
57 | $tz = C4::Context->tz; | ||||
58 | } | ||||
59 | if ( !$date_format ) { | ||||
60 | $date_format = C4::Context->preference('dateformat'); | ||||
61 | } | ||||
62 | if ($date_string) { | ||||
63 | if ( ref($date_string) eq 'DateTime' ) { # already a dt return it | ||||
64 | return $date_string; | ||||
65 | } | ||||
66 | |||||
67 | if ( $date_format eq 'metric' ) { | ||||
68 | $date_string =~ s#-#/#g; | ||||
69 | $date_string =~ s/^00/01/; # system allows the 0th of the month | ||||
70 | $date_string =~ s#^(\d{1,2})/(\d{1,2})#$2/$1#; | ||||
71 | } else { | ||||
72 | if ( $date_format eq 'iso' ) { | ||||
73 | $date_string =~ s/-00/-01/; | ||||
74 | if ( $date_string =~ m/^0000-0/ ) { | ||||
75 | return; # invalid date in db | ||||
76 | } | ||||
77 | } elsif ( $date_format eq 'us' ) { | ||||
78 | $date_string =~ s#-#/#g; | ||||
79 | $date_string =~ s[/00/][/01/]; | ||||
80 | } elsif ( $date_format eq 'sql' ) { | ||||
81 | $date_string =~ | ||||
82 | s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/; | ||||
83 | return if ($date_string =~ /^0000-00-00/); | ||||
84 | $date_string =~ s/00T/01T/; | ||||
85 | } | ||||
86 | } | ||||
87 | return DateTime::Format::DateParse->parse_datetime( $date_string, | ||||
88 | $tz->name() ); | ||||
89 | } | ||||
90 | return DateTime->now( time_zone => $tz ); | ||||
91 | |||||
92 | } | ||||
93 | |||||
94 | =head2 output_pref | ||||
95 | |||||
- - | |||||
111 | sub output_pref { | ||||
112 | my $dt = shift; | ||||
113 | my $force_pref = shift; # if testing we want to override Context | ||||
114 | my $force_time = shift; | ||||
115 | my $dateonly = shift || 0; # if you don't want the hours and minutes | ||||
116 | |||||
117 | return unless defined $dt; | ||||
118 | |||||
119 | $dt->set_time_zone( C4::Context->tz ); | ||||
120 | |||||
121 | my $pref = | ||||
122 | defined $force_pref ? $force_pref : C4::Context->preference('dateformat'); | ||||
123 | |||||
124 | my $time_format = $force_time || C4::Context->preference('TimeFormat'); | ||||
125 | my $time = ( $time_format eq '12hr' ) ? '%I:%M %p' : '%H:%M'; | ||||
126 | |||||
127 | given ($pref) { | ||||
128 | when (/^iso/) { | ||||
129 | return $dateonly | ||||
130 | ? $dt->strftime("%Y-%m-%d") | ||||
131 | : $dt->strftime("%Y-%m-%d $time"); | ||||
132 | } | ||||
133 | when (/^metric/) { | ||||
134 | return $dateonly | ||||
135 | ? $dt->strftime("%d/%m/%Y") | ||||
136 | : $dt->strftime("%d/%m/%Y $time"); | ||||
137 | } | ||||
138 | when (/^us/) { | ||||
139 | |||||
140 | return $dateonly | ||||
141 | ? $dt->strftime("%m/%d/%Y") | ||||
142 | : $dt->strftime("%m/%d/%Y $time"); | ||||
143 | } | ||||
144 | default { | ||||
145 | return $dateonly | ||||
146 | ? $dt->strftime("%Y-%m-%d") | ||||
147 | : $dt->strftime("%Y-%m-%d $time"); | ||||
148 | } | ||||
149 | |||||
150 | } | ||||
151 | return; | ||||
152 | } | ||||
153 | |||||
154 | =head2 output_pref_due | ||||
155 | |||||
- - | |||||
168 | sub output_pref_due { | ||||
169 | my $disp_str = output_pref(@_); | ||||
170 | $disp_str =~ s/ 23:59//; | ||||
171 | return $disp_str; | ||||
172 | } | ||||
173 | |||||
174 | =head2 format_sqldatetime | ||||
175 | |||||
- - | |||||
183 | sub format_sqldatetime { | ||||
184 | my $str = shift; | ||||
185 | my $force_pref = shift; # if testing we want to override Context | ||||
186 | my $force_time = shift; | ||||
187 | my $dateonly = shift; | ||||
188 | |||||
189 | if ( defined $str && $str =~ m/^\d{4}-\d{2}-\d{2}/ ) { | ||||
190 | my $dt = dt_from_string( $str, 'sql' ); | ||||
191 | return q{} unless $dt; | ||||
192 | $dt->truncate( to => 'minute' ); | ||||
193 | return output_pref( $dt, $force_pref, $force_time, $dateonly ); | ||||
194 | } | ||||
195 | return q{}; | ||||
196 | } | ||||
197 | |||||
198 | =head2 format_sqlduedatetime | ||||
199 | |||||
- - | |||||
207 | sub format_sqlduedatetime { | ||||
208 | my $str = shift; | ||||
209 | my $force_pref = shift; # if testing we want to override Context | ||||
210 | my $force_time = shift; | ||||
211 | my $dateonly = shift; | ||||
212 | |||||
213 | if ( defined $str && $str =~ m/^\d{4}-\d{2}-\d{2}/ ) { | ||||
214 | my $dt = dt_from_string( $str, 'sql' ); | ||||
215 | $dt->truncate( to => 'minute' ); | ||||
216 | return output_pref_due( $dt, $force_pref, $force_time, $dateonly ); | ||||
217 | } | ||||
218 | return q{}; | ||||
219 | } | ||||
220 | |||||
221 | 1 | 5µs | 1; |