Skip to content

Commit

Permalink
Merge pull request #10 from jjatria/time-validation
Browse files Browse the repository at this point in the history
Make parameter validation in rand_time more consistent
  • Loading branch information
barefootcoder committed Apr 18, 2018
2 parents e188bfb + 7966e84 commit 6dc8315
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/Data/Random.pm
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,15 @@ sub rand_time {
( $min_hour, $min_min, $min_sec ) = ( $hour, $min, $sec );
}
else {
( $min_hour, $min_min, $min_sec ) = split ( /\:/, $options{'min'} );

cluck('minimum time is not in valid time format HH:MM:SS') && return
if ( $min_hour > 23 ) || ( $min_hour < 0 );
cluck('minimum time is not in valid time format HH:MM:SS') && return
if ( $min_min > 59 ) || ( $min_min < 0 );
cluck('minimum time is not in valid time format HH:MM:SS') && return
if ( $min_sec > 59 ) || ( $min_sec < 0 );
eval {
my $min = Time::Piece->strptime( $options{min}, '%T' );
( $min_hour, $min_min, $min_sec )
= ( $min->hour, $min->min, $min->sec );
};
if ($@) {
cluck 'minimum time is not in valid time format HH:MM:SS';
return;
}
}
}
else {
Expand All @@ -339,14 +340,15 @@ sub rand_time {
( $max_hour, $max_min, $max_sec ) = ( $hour, $min, $sec );
}
else {
( $max_hour, $max_min, $max_sec ) = split ( /\:/, $options{'max'} );

cluck('maximum time is not in valid time format HH:MM:SS') && return
if ( $max_hour > 23 ) || ( $max_hour < 0 );
cluck('maximum time is not in valid time format HH:MM:SS') && return
if ( $max_min > 59 ) || ( $max_min < 0 );
cluck('maximum time is not in valid time format HH:MM:SS') && return
if ( $max_sec > 59 ) || ( $max_sec < 0 );
eval {
my $max = Time::Piece->strptime( $options{max}, '%T' );
( $max_hour, $max_min, $max_sec )
= ( $max->hour, $max->min, $max->sec );
};
if ($@) {
cluck 'maximum time is not in valid time format HH:MM:SS';
return;
}
}
}
else {
Expand Down

0 comments on commit 6dc8315

Please sign in to comment.