Bugzilla – Attachment 35406 Details for
Bug 13601
Get rid of DateTime::Format::DateParse
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 13601: Make dt_from_string not using DateTime::Format::DateParse
Bug-13601-Make-dtfromstring-not-using-DateTimeForm.patch (text/plain), 5.57 KB, created by
Jonathan Druart
on 2015-01-20 15:12:24 UTC
(
hide
)
Description:
Bug 13601: Make dt_from_string not using DateTime::Format::DateParse
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2015-01-20 15:12:24 UTC
Size:
5.57 KB
patch
obsolete
>From f70bf39d3e5a19c297894cdc2a95fa9783dba825 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@biblibre.com> >Date: Tue, 20 Jan 2015 15:28:56 +0100 >Subject: [PATCH] Bug 13601: Make dt_from_string not using > DateTime::Format::DateParse > >For a couple of reasons, dt_from_string should not use >DateTime::Format::DateParse: >1/ It does not manage date < 1900, certainly caused by l.47 of this >module: > $p{ year } = $year ? $year + 1900 : DateTime->now->year; > >2/ It considers 31/01/2015 as a valid us date, which is not. > >Test plan: >1/ Verify that > prove t/DateUtils.t >returns green >2/ Play with dates in Koha (yes I know, it's vague...) >3/ Try to find a regression with dates >4/ Create a date with year <= 1900 and confirm it works > >QA comment: >Why the sql format switch was: >- $date_string =~ >-s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/; > >From where a date like "yyyymmdd hhmmss" can come? >--- > Koha/DateUtils.pm | 114 +++++++++++++++++++++++++++++++++++++++--------------- > 1 file changed, 82 insertions(+), 32 deletions(-) > >diff --git a/Koha/DateUtils.pm b/Koha/DateUtils.pm >index 4358137..61a733f 100644 >--- a/Koha/DateUtils.pm >+++ b/Koha/DateUtils.pm >@@ -20,7 +20,6 @@ use strict; > use warnings; > use 5.010; > use DateTime; >-use DateTime::Format::DateParse; > use C4::Context; > > use base 'Exporter'; >@@ -54,46 +53,97 @@ to the system preferences. If the date string is empty DateTime->now is returned > sub dt_from_string { > my ( $date_string, $date_format, $tz ) = @_; > >- # FIXME: see bug 13242 => no TZ for dates 'infinite' >- return DateTime::Format::DateParse->parse_datetime($date_string) >- if $date_string and $date_string =~ /^9999-/; >+ return if $date_string and $date_string =~ m|^0000-0|; >+ >+ $tz = C4::Context->tz unless $tz;; >+ >+ return DateTime->now( time_zone => $tz ) unless $date_string; >+ >+ $date_format = C4::Context->preference('dateformat') unless $date_format; > >- if ( !$tz ) { >- $tz = C4::Context->tz; >- } >- if ( !$date_format ) { >- $date_format = C4::Context->preference('dateformat'); >- } > if ($date_string) { > if ( ref($date_string) eq 'DateTime' ) { # already a dt return it > return $date_string; > } > >+ my $regex; > if ( $date_format eq 'metric' ) { >- $date_string =~ s#-#/#g; >- $date_string =~ s/^00/01/; # system allows the 0th of the month >- $date_string =~ s#^(\d{1,2})/(\d{1,2})#$2/$1#; >- } else { >- if ( $date_format eq 'iso' ) { >- $date_string =~ s/-00/-01/; >- if ( $date_string =~ m/^0000-0/ ) { >- return; # invalid date in db >- } >- } elsif ( $date_format eq 'us' ) { >- $date_string =~ s#-#/#g; >- $date_string =~ s[/00/][/01/]; >- } elsif ( $date_format eq 'sql' ) { >- $date_string =~ >-s/(\d{4})(\d{2})(\d{2})\s+(\d{2})(\d{2})(\d{2})/$1-$2-$3T$4:$5:$6/; >- return if ($date_string =~ /^0000-00-00/); >- $date_string =~ s/00T/01T/; >- } >+ # metric format is "dd/mm/yyyy[ hh:mm:ss]" >+ $regex = qr| >+ (?<day>\d{2}) >+ / >+ (?<month>\d{2}) >+ / >+ (?<year>\d{4}) >+ |xms; > } >- return DateTime::Format::DateParse->parse_datetime( $date_string, >- $tz->name() ); >- } >- return DateTime->now( time_zone => $tz ); >+ elsif ( $date_format eq 'us' ) { >+ # us format is "mm/dd/yyyy[ hh:mm:ss]" >+ $regex = qr| >+ (?<month>\d{2}) >+ / >+ (?<day>\d{2}) >+ / >+ (?<year>\d{4}) >+ |xms; >+ } >+ elsif ( $date_format eq 'iso' or $date_format eq 'sql' ) { >+ # iso format is yyyy-dd-mm[ hh:mm:ss]" >+ $regex = qr| >+ (?<year>\d{4}) >+ - >+ (?<month>\d{2}) >+ - >+ (?<day>\d{2}) >+ |xms; >+ } >+ else { >+ die "Invalid dateformat parameter ($date_format)"; >+ } >+ >+ # Add the faculative time part [hh:mm[:ss]] >+ $regex .= qr| >+ ( >+ \s* >+ (?<hour>\d{2}) >+ : >+ (?<minute>\d{2}) >+ ( >+ : >+ (?<second>\d{2}) >+ )? >+ )? >+ |xms; >+ >+ my %dt_params; >+ if ( $date_string =~ $regex ) { >+ %dt_params = ( >+ year => $+{year}, >+ month => $+{month}, >+ day => $+{day}, >+ hour => $+{hour}, >+ minute => $+{minute}, >+ second => $+{second}, >+ ); >+ } >+ else { >+ die "The given date ($date_string) does not match the date format ($date_format)"; >+ } >+ >+ # system allows the 0th of the month >+ $dt_params{day} = '01' if $dt_params{day} eq '00'; > >+ # Set default hh:mm:ss to 00:00:00 >+ $dt_params{hour} = 00 unless defined $dt_params{hour}; >+ $dt_params{minute} = 00 unless defined $dt_params{minute}; >+ $dt_params{second} = 00 unless defined $dt_params{second}; >+ >+ return DateTime->new( >+ %dt_params, >+ # No TZ for dates 'infinite' => see bug 13242 >+ ( $dt_params{year} < 9999 ? ( time_zone => $tz->name ) : () ), >+ ); >+ } > } > > =head2 output_pref >-- >2.1.0
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 13601
:
35405
|
35406
|
35407
|
35408
|
35433
|
36733
|
36734
|
36735
|
36736
|
36737
|
36745
|
36746
|
36747
|
36748
|
36754
|
36755
|
36756
|
36928
|
36933
|
36939
|
37023
|
37087
|
37088
|
37089
|
37090
|
37091
|
37092
|
37093