From: ivan Date: Fri, 9 Apr 2010 08:18:24 +0000 (+0000) Subject: fix date parsing when using international dates (package date edit), RT#8027 X-Git-Tag: root_of_svc_elec_features~307 X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=commitdiff_plain;h=2ab068f449eb97a10e18d20e9dab5ab9faa017e7 fix date parsing when using international dates (package date edit), RT#8027 --- diff --git a/FS/FS/Mason.pm b/FS/FS/Mason.pm index 82e64f245..f65d9728b 100644 --- a/FS/FS/Mason.pm +++ b/FS/FS/Mason.pm @@ -57,12 +57,12 @@ if ( -e $addl_handler_use_file ) { use List::Util qw( max min ); use Data::Dumper; use Date::Format; - use Date::Parse; use Time::Local; use Time::HiRes; use Time::Duration; use DateTime; use DateTime::Format::Strptime; + use FS::Misc::DateTime qw( parse_datetime ); use Lingua::EN::Inflect qw(PL); use Tie::IxHash; use URI; diff --git a/FS/FS/Misc/DateTime.pm b/FS/FS/Misc/DateTime.pm new file mode 100644 index 000000000..a32c15aea --- /dev/null +++ b/FS/FS/Misc/DateTime.pm @@ -0,0 +1,64 @@ +package FS::Misc::DateTime; + +use base qw( Exporter ); +use vars qw( @EXPORT_OK ); +use Carp; +use Date::Parse; +use DateTime::Format::Natural; +use FS::Conf; + +@EXPORT_OK = qw( parse_datetime ); + +=head1 NAME + +FS::Misc::DateTime - Date and time subroutines + +=head1 SYNOPSIS + +use FS::Misc::DateTime qw( parse_datetime ); + +=head1 SUBROUTINES + +=over 4 + +=item parse_datetime STRING + +Parses a date (and possibly time) from the supplied string and returns +the date as an integer UNIX timestamp. + +=cut + +sub parse_datetime { + my $string = shift; + return '' unless $string =~ /\S/; + + my $conf = new FS::Conf; + my $format = $conf->config('date_format') || '%m/%d/%Y'; + + if ( $format eq '%d/%m/%Y' ) { # =~ /\%d.*\%m/ ) { + #$format =~ s/\%//g; + my $parser = DateTime::Format::Natural->new( 'time_zone' => 'local', + #'format'=>'d/m/y',#lc($format) + ); + $dt = $parser->parse_datetime($string); + if ( $parser->success ) { + return $dt->epoch; + } else { + #carp "WARNING: can't parse date: ". $parser->error; + #return ''; + #huh, very common, we still need the "partially" (fully enough for our purposes) parsed date. + $dt->epoch; + } + } else { + return str2time($string); + } + +} + +=back + +=head1 BUGS + +=cut + +1; diff --git a/FS/FS/UI/Web.pm b/FS/FS/UI/Web.pm index c9264a044..821b1916a 100644 --- a/FS/FS/UI/Web.pm +++ b/FS/FS/UI/Web.pm @@ -4,6 +4,7 @@ use strict; use vars qw($DEBUG @ISA @EXPORT_OK $me); use Exporter; use FS::Conf; +use FS::Misc::DateTime qw( parse_datetime ); use FS::Record qw(dbdef); use FS::cust_main; # are sql_balance and sql_date_balance in the right module? @@ -30,7 +31,7 @@ sub parse_beginning_ending { if ( $cgi->param($prefix.'begin') =~ /^(\d+)$/ ) { $beginning = $1; } elsif ( $cgi->param($prefix.'beginning') =~ /^([ 0-9\-\/]{1,64})$/ ) { - $beginning = str2time($1) || 0; + $beginning = parse_datetime($1) || 0; } my $ending = 4294967295; #2^32-1 @@ -38,7 +39,7 @@ sub parse_beginning_ending { $ending = $1 - 1; } elsif ( $cgi->param($prefix.'ending') =~ /^([ 0-9\-\/]{1,64})$/ ) { #probably need an option to turn off the + 86399 - $ending = str2time($1) + 86399; + $ending = parse_datetime($1) + 86399; } ( $beginning, $ending ); diff --git a/FS/FS/cust_main/Import.pm b/FS/FS/cust_main/Import.pm index f477323d1..901ff18ae 100644 --- a/FS/FS/cust_main/Import.pm +++ b/FS/FS/cust_main/Import.pm @@ -5,8 +5,8 @@ use vars qw( $DEBUG $conf ); use Storable qw(thaw); use Data::Dumper; use MIME::Base64; -use Date::Parse; use File::Slurp qw( slurp ); +use FS::Misc::DateTime qw( parse_datetime ); use FS::UID qw( dbh ); use FS::Record qw( qsearchs ); use FS::cust_main; @@ -264,13 +264,13 @@ sub batch_import { if ( $field =~ /^cust_pkg\.(pkgpart|setup|bill|susp|adjourn|expire|cancel)$/ ) { - #$cust_pkg{$1} = str2time( shift @$columns ); + #$cust_pkg{$1} = parse_datetime( shift @$columns ); if ( $1 eq 'pkgpart' ) { $cust_pkg{$1} = shift @columns; } elsif ( $1 eq 'setup' ) { - $billtime = str2time(shift @columns); + $billtime = parse_datetime(shift @columns); } else { - $cust_pkg{$1} = str2time( shift @columns ); + $cust_pkg{$1} = parse_datetime( shift @columns ); } } elsif ( $field =~ /^svc_acct\.(username|_password)$/ ) { diff --git a/FS/MANIFEST b/FS/MANIFEST index b3415178b..900d3a1f5 100644 --- a/FS/MANIFEST +++ b/FS/MANIFEST @@ -503,3 +503,4 @@ FS/part_event/Action/pkg_agent_credit.pm FS/part_event/Action/pkg_agent_credit_pkg.pm FS/part_event/Action/pkg_employee_credit.pm FS/part_event/Action/pkg_employee_credit_pkg.pm +FS/Misc/DateTime.pm diff --git a/httemplate/edit/REAL_cust_pkg.cgi b/httemplate/edit/REAL_cust_pkg.cgi index 29c8ca63f..77ab1fe67 100755 --- a/httemplate/edit/REAL_cust_pkg.cgi +++ b/httemplate/edit/REAL_cust_pkg.cgi @@ -187,9 +187,9 @@ if ( $cgi->param('error') ) { $cust_pkg = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum}); die "No package!" unless $cust_pkg; - foreach my $col (qw( setup last_bill bill adjourn expire )) { + foreach my $col (qw( start_date setup last_bill bill adjourn expire )) { my $value = $cgi->param($col); - $cust_pkg->set( $col, $value ? str2time($value) : '' ); + $cust_pkg->set( $col, $value ? parse_datetime($value) : '' ); } } else { diff --git a/httemplate/edit/cust_pay.cgi b/httemplate/edit/cust_pay.cgi index a6b73b13a..cc4ec605d 100755 --- a/httemplate/edit/cust_pay.cgi +++ b/httemplate/edit/cust_pay.cgi @@ -113,7 +113,7 @@ if ( $cgi->param('error') ) { $paid = $cgi->param('paid'); $payby = $cgi->param('payby'); $payinfo = $cgi->param('payinfo'); - $_date = $cgi->param('_date') ? str2time($cgi->param('_date')) : time; + $_date = $cgi->param('_date') ? parse_datetime($cgi->param('_date')) : time; } elsif ( $cgi->param('custnum') =~ /^(\d+)$/ ) { $link = $cgi->param('popup') ? 'popup' : 'custnum'; $linknum = $1; diff --git a/httemplate/edit/process/REAL_cust_pkg.cgi b/httemplate/edit/process/REAL_cust_pkg.cgi index b5796ebf5..570f0e031 100755 --- a/httemplate/edit/process/REAL_cust_pkg.cgi +++ b/httemplate/edit/process/REAL_cust_pkg.cgi @@ -19,13 +19,8 @@ die "access denied" my $pkgnum = $cgi->param('pkgnum') or die; my $old = qsearchs('cust_pkg',{'pkgnum'=>$pkgnum}); my %hash = $old->hash; -$hash{'start_date'} = $cgi->param('start_date') ? str2time($cgi->param('start_date')) : ''; -$hash{'setup'} = $cgi->param('setup') ? str2time($cgi->param('setup')) : ''; -$hash{'bill'} = $cgi->param('bill') ? str2time($cgi->param('bill')) : ''; -$hash{'last_bill'} = - $cgi->param('last_bill') ? str2time($cgi->param('last_bill')) : ''; -$hash{'adjourn'} = $cgi->param('adjourn') ? str2time($cgi->param('adjourn')) : ''; -$hash{'expire'} = $cgi->param('expire') ? str2time($cgi->param('expire')) : ''; +$hash{$_}= $cgi->param($_) ? parse_datetime($cgi->param($_)) : '' + foreach qw( start_date setup bill last_bill adjourn expire ); my @errors = (); @@ -47,7 +42,6 @@ my $error; if ( @errors ) { $error = join(',', @errors); } else { - warn join(',',%hash); $new = new FS::cust_pkg \%hash; $error = $new->replace($old); } diff --git a/httemplate/edit/process/cust_main.cgi b/httemplate/edit/process/cust_main.cgi index f72ca0a81..1311ba4f3 100755 --- a/httemplate/edit/process/cust_main.cgi +++ b/httemplate/edit/process/cust_main.cgi @@ -98,7 +98,7 @@ foreach my $dfield (qw( birthdate signupdate )) { } else { - $parsed = str2time($value) + $parsed = parse_datetime($value) or $error ||= "Invalid $dfield: $value"; } diff --git a/httemplate/edit/process/cust_pay.cgi b/httemplate/edit/process/cust_pay.cgi index a310c5306..df506c677 100755 --- a/httemplate/edit/process/cust_pay.cgi +++ b/httemplate/edit/process/cust_pay.cgi @@ -39,7 +39,7 @@ $cgi->param('link') =~ /^(custnum|invnum|popup)$/ my $field = my $link = $1; $field = 'custnum' if $field eq 'popup'; -my $_date = str2time($cgi->param('_date')); +my $_date = parse_datetime($cgi->param('_date')); my $new = new FS::cust_pay ( { $field => $linknum, diff --git a/httemplate/edit/process/quick-charge.cgi b/httemplate/edit/process/quick-charge.cgi index 27921b8c3..484f6fcc6 100644 --- a/httemplate/edit/process/quick-charge.cgi +++ b/httemplate/edit/process/quick-charge.cgi @@ -58,7 +58,7 @@ unless ( $error ) { 'bill_now' => scalar($cgi->param('bill_now')), 'invoice_terms' => scalar($cgi->param('invoice_terms')), 'start_date' => ( scalar($cgi->param('start_date')) - ? str2time($cgi->param('start_date')) + ? parse_datetime($cgi->param('start_date')) : '' ), 'no_auto' => scalar($cgi->param('no_auto')), diff --git a/httemplate/edit/process/quick-cust_pkg.cgi b/httemplate/edit/process/quick-cust_pkg.cgi index 202d0a3cf..2fde17f5e 100644 --- a/httemplate/edit/process/quick-cust_pkg.cgi +++ b/httemplate/edit/process/quick-cust_pkg.cgi @@ -54,7 +54,7 @@ my $cust_pkg = new FS::cust_pkg { 'custnum' => $custnum, 'pkgpart' => $pkgpart, 'start_date' => ( scalar($cgi->param('start_date')) - ? str2time($cgi->param('start_date')) + ? parse_datetime($cgi->param('start_date')) : '' ), 'no_auto' => scalar($cgi->param('no_auto')), diff --git a/httemplate/misc/process/cancel_pkg.html b/httemplate/misc/process/cancel_pkg.html index 669af9c87..e17872c06 100755 --- a/httemplate/misc/process/cancel_pkg.html +++ b/httemplate/misc/process/cancel_pkg.html @@ -44,7 +44,7 @@ my $date = time; if ($method eq 'expire' || $method eq 'adjourn'){ #untaint date $date = $cgi->param('date'); - str2time($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date"; + parse_datetime($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date"; $date = $1; $method = ($method eq 'expire') ? 'cancel' : 'suspend'; } diff --git a/httemplate/misc/process/delay_susp_pkg.html b/httemplate/misc/process/delay_susp_pkg.html index c7cc7de7c..8649cc235 100755 --- a/httemplate/misc/process/delay_susp_pkg.html +++ b/httemplate/misc/process/delay_susp_pkg.html @@ -21,7 +21,7 @@ $cgi->param('pkgnum') =~ /^(\d+)$/ or die "Illegal pkgnum"; $pkgnum = $1; #untaint date -str2time($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date"; +parse_datetime($cgi->param('date')) =~ /^(\d+)$/ or die "Illegal date"; my $date = $1; $cust_pkg = qsearchs( 'cust_pkg', {'pkgnum'=>$pkgnum} ); diff --git a/httemplate/search/elements/cust_main_dayranges.html b/httemplate/search/elements/cust_main_dayranges.html index 9e4e86f9c..b3f1a2ef6 100644 --- a/httemplate/search/elements/cust_main_dayranges.html +++ b/httemplate/search/elements/cust_main_dayranges.html @@ -110,7 +110,7 @@ my $range_sub = delete($opt{'range_sub'}); #or die my $offset = 0; if($cgi->param('as_of')) { - $offset = int((time - str2time($cgi->param('as_of'))) / 86400); + $offset = int((time - parse_datetime($cgi->param('as_of'))) / 86400); $opt{'title'} .= ' ('.$cgi->param('as_of').')' if $offset > 0; } diff --git a/httemplate/search/pay_batch.cgi b/httemplate/search/pay_batch.cgi index fb452870f..ebd323e13 100755 --- a/httemplate/search/pay_batch.cgi +++ b/httemplate/search/pay_batch.cgi @@ -97,12 +97,12 @@ my($begin, $end) = ( '', '' ); my @where; if ( $cgi->param('beginning') && $cgi->param('beginning') =~ /^([ 0-9\-\/]{0,10})$/ ) { - $begin = str2time($1); + $begin = parse_datetime($1); push @where, "download >= $begin"; } if ( $cgi->param('ending') && $cgi->param('ending') =~ /^([ 0-9\-\/]{0,10})$/ ) { - $end = str2time($1) + 86399; + $end = parse_datetime($1) + 86399; push @where, "download < $end"; } diff --git a/httemplate/search/report_prepaid_income.cgi b/httemplate/search/report_prepaid_income.cgi index c0e2b807e..bfb699b54 100644 --- a/httemplate/search/report_prepaid_income.cgi +++ b/httemplate/search/report_prepaid_income.cgi @@ -84,7 +84,7 @@ my $actual_label = $legacy ? 'Actual ' : ''; my $time = time; -my $now = $cgi->param('date') && str2time($cgi->param('date')) || $time; +my $now = $cgi->param('date') && parse_datetime($cgi->param('date')) || $time; $now =~ /^(\d+)$/ or die "unparsable date?"; $now = $1; diff --git a/httemplate/search/sqlradius.cgi b/httemplate/search/sqlradius.cgi index 29f360298..cca121179 100644 --- a/httemplate/search/sqlradius.cgi +++ b/httemplate/search/sqlradius.cgi @@ -100,11 +100,11 @@ die "access denied" my( $beginning, $ending ) = ( '', '' ); if ( $cgi->param('stoptime_beginning') && $cgi->param('stoptime_beginning') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) { - $beginning = str2time($1); + $beginning = parse_datetime($1); } if ( $cgi->param('stoptime_ending') && $cgi->param('stoptime_ending') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) { - $ending = str2time($1); # + 86399; + $ending = parse_datetime($1); # + 86399; } if ( $cgi->param('begin') && $cgi->param('begin') =~ /^(\d+)$/ ) { $beginning = $1; @@ -121,11 +121,11 @@ if ( $cgi->param('open_sessions') =~ /^(\d*)$/ ) { my( $starttime_beginning, $starttime_ending ) = ( '', '' ); if ( $cgi->param('starttime_beginning') && $cgi->param('starttime_beginning') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) { - $starttime_beginning = str2time($1); + $starttime_beginning = parse_datetime($1); } if ( $cgi->param('starttime_ending') && $cgi->param('starttime_ending') =~ /^([ 0-9\-\/\:\w]{0,54})$/ ) { - $starttime_ending = str2time($1); # + 86399; + $starttime_ending = parse_datetime($1); # + 86399; } my $cgi_svc_acct = '';