summaryrefslogtreecommitdiff
path: root/FS
diff options
context:
space:
mode:
authorivan <ivan>2010-04-09 08:18:24 +0000
committerivan <ivan>2010-04-09 08:18:24 +0000
commit2ab068f449eb97a10e18d20e9dab5ab9faa017e7 (patch)
treef1464912b3f3072cbb4290cfa2a4a26e32165676 /FS
parent4125a128eaa40c751c89a69c2f9888463a78c6f3 (diff)
fix date parsing when using international dates (package date edit), RT#8027
Diffstat (limited to 'FS')
-rw-r--r--FS/FS/Mason.pm2
-rw-r--r--FS/FS/Misc/DateTime.pm64
-rw-r--r--FS/FS/UI/Web.pm5
-rw-r--r--FS/FS/cust_main/Import.pm8
-rw-r--r--FS/MANIFEST1
5 files changed, 73 insertions, 7 deletions
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