WITH date_validate fra AS builtin 'perl5' function<<EOF
# this is the valid ts format string
my $ts_format_str = "";
my @mdays = (0,31,28,31,30,31,30,31,31,30,31,30,31);
# This maps column name to an index value used
# by addamark::setInto() function
my $col_map = {
"BAD_TS_FLAG" => 1,
"BAD_RECORD_FLAG" => 2,
};
$SMART_DEBUG = 1;
sub smart_debug { addamark::dbgPrint($_[0]) if ( $SMART_DEBUG); }
sub date_validate {
# We take 3 args , the start,end and acct_delay strings
my ($start_ts_str, $end_ts_str, $acct_delay_str) = @_;
# begin by starting in successful state
my $bad_record_flag = 0;
my $bad_ts_flag = 0;
# first lets figure out the BAD_RECORD_FLAG by checking
# the integrity of $acct_delay_str
$bad_record_flag = 1 unless ( $acct_delay_str =~ /^\d+$/ );
smart_debug("After ACCT_DELAY_CHECK: bad_record_flag is: $bad_record_flag");
# now we check our ts strings
$bad_ts_flag = 1 unless ( valid_date($start_ts_str) );
smart_debug("After START_TS_STR CHECK: bad_ts_flag is: $bad_ts_flag");
$bad_ts_flag = 1 unless ( valid_date($end_ts_str) );
smart_debug("After END_TS_STR CHECK: bad_ts_flag is: $bad_ts_flag");
addamark::setInto($col_map->{"BAD_TS_FLAG"},$bad_ts_flag);
addamark::setInto($col_map->{"BAD_RECORD_FLAG"}, $bad_record_flag);
}
sub valid_date {
my ($day, $month, $year, $hour, $min,$sec) = $_[0] =~ m/^(\d{2})(\d{2})(\d{4})(\d{2})(\d{2})(\d{2})$/;
my $nyear = (localtime())[5] + 1900;
smart_debug("in valid date: $day-$month-$year $hour:$min:$sec");
##### Year range is 10 years on either side of current year
##### Also exits if m// returned nothing
return if $year < $nyear - 10 || $year > $nyear + 10;
return if $month < 1 || $month > 12;
##### Leap year conditions
if ($month == 2) {
if ($year % 4 != 0) { $mdays[2] = 28; }
elsif ($year % 400 == 0) { $mdays[2] = 29; }
elsif ($year % 100 == 0) { $mdays[2] = 28; }
else { $mdays[2] = 29; }
}
return if $day < 1 || $day > $mdays[$month];
return 1;
}