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;
--- /dev/null
+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;
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?
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
$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 );
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;
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)$/ ) {
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
$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 {
$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;
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 = ();
if ( @errors ) {
$error = join(',', @errors);
} else {
- warn join(',',%hash);
$new = new FS::cust_pkg \%hash;
$error = $new->replace($old);
}
} else {
- $parsed = str2time($value)
+ $parsed = parse_datetime($value)
or $error ||= "Invalid $dfield: $value";
}
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,
'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'))
: ''
),
'pkg' => scalar($cgi->param('pkg')),
'custnum' => $custnum,
'pkgpart' => $pkgpart,
'start_date' => ( scalar($cgi->param('start_date'))
- ? str2time($cgi->param('start_date'))
+ ? parse_datetime($cgi->param('start_date'))
: ''
),
'refnum' => $refnum,
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';
}
$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} );
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;
}
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";
}
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;
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;
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 = '';