| Filename | /usr/share/koha/lib/Koha/DateUtils.pm |
| Statements | Executed 30 statements in 1.46ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 1 | 1 | 1 | 608µs | 4.97ms | Koha::DateUtils::BEGIN@23 |
| 1 | 1 | 1 | 82µs | 158µs | Koha::DateUtils::BEGIN@21 |
| 1 | 1 | 1 | 29µs | 36µs | Koha::DateUtils::BEGIN@19 |
| 1 | 1 | 1 | 20µs | 22µs | Koha::DateUtils::BEGIN@22 |
| 1 | 1 | 1 | 17µs | 32µs | Koha::DateUtils::BEGIN@20 |
| 1 | 1 | 1 | 16µs | 77µs | Koha::DateUtils::BEGIN@21.9 |
| 1 | 1 | 1 | 14µs | 99µs | Koha::DateUtils::BEGIN@26 |
| 1 | 1 | 1 | 14µs | 38µs | Koha::DateUtils::BEGIN@27 |
| 1 | 1 | 1 | 12µs | 15µs | Koha::DateUtils::BEGIN@24 |
| 0 | 0 | 0 | 0s | 0s | Koha::DateUtils::dt_from_string |
| 0 | 0 | 0 | 0s | 0s | Koha::DateUtils::format_sqldatetime |
| 0 | 0 | 0 | 0s | 0s | Koha::DateUtils::format_sqlduedatetime |
| 0 | 0 | 0 | 0s | 0s | Koha::DateUtils::output_pref |
| 0 | 0 | 0 | 0s | 0s | Koha::DateUtils::output_pref_due |
| 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 | 45µs | 2 | 43µs | # spent 36µs (29+7) within Koha::DateUtils::BEGIN@19 which was called:
# once (29µs+7µs) by C4::Letters::BEGIN@32 at line 19 # spent 36µs making 1 call to Koha::DateUtils::BEGIN@19
# spent 7µs making 1 call to strict::import |
| 20 | 3 | 53µs | 2 | 46µs | # spent 32µs (17+14) within Koha::DateUtils::BEGIN@20 which was called:
# once (17µs+14µs) by C4::Letters::BEGIN@32 at line 20 # spent 32µs making 1 call to Koha::DateUtils::BEGIN@20
# spent 14µs making 1 call to warnings::import |
| 21 | 6 | 116µs | 3 | 296µs | use 5.010; # spent 158µs making 1 call to Koha::DateUtils::BEGIN@21
# spent 77µs making 1 call to Koha::DateUtils::BEGIN@21.9
# spent 61µs making 1 call to feature::import |
| 22 | 3 | 33µs | 2 | 24µs | # spent 22µs (20+2) within Koha::DateUtils::BEGIN@22 which was called:
# once (20µs+2µs) by C4::Letters::BEGIN@32 at line 22 # spent 22µs making 1 call to Koha::DateUtils::BEGIN@22
# spent 2µs making 1 call to UNIVERSAL::import |
| 23 | 3 | 206µs | 2 | 4.98ms | # spent 4.97ms (608µs+4.37) within Koha::DateUtils::BEGIN@23 which was called:
# once (608µs+4.37ms) by C4::Letters::BEGIN@32 at line 23 # spent 4.97ms making 1 call to Koha::DateUtils::BEGIN@23
# spent 3µs making 1 call to UNIVERSAL::import |
| 24 | 3 | 28µs | 2 | 17µs | # spent 15µs (12+2) within Koha::DateUtils::BEGIN@24 which was called:
# once (12µs+2µs) by C4::Letters::BEGIN@32 at line 24 # spent 15µs making 1 call to Koha::DateUtils::BEGIN@24
# spent 2µs making 1 call to C4::Context::import |
| 25 | |||||
| 26 | 3 | 39µs | 2 | 184µs | # spent 99µs (14+85) within Koha::DateUtils::BEGIN@26 which was called:
# once (14µs+85µs) by C4::Letters::BEGIN@32 at line 26 # spent 99µs making 1 call to Koha::DateUtils::BEGIN@26
# spent 85µs making 1 call to base::import |
| 27 | 4 | 932µs | 3 | 80µs | # spent 38µs (14+24) within Koha::DateUtils::BEGIN@27 which was called:
# once (14µs+24µs) by C4::Letters::BEGIN@32 at line 27 # spent 38µs making 1 call to Koha::DateUtils::BEGIN@27
# spent 24µs making 1 call to version::import
# spent 19µ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; |