Skip to content
This repository has been archived by the owner on Oct 15, 2022. It is now read-only.

Commit

Permalink
Ensure start and end dates are different
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiltyDolphin committed Jul 20, 2016
1 parent 1899241 commit 8347689
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/DDG/Goodie/RandomDate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ handle query => sub {
return if $range_text && $range_type eq 'none';
my ($min_date, $max_date) = parse_range($range_type, $lang->locale, $range_text);
return if $range_text && !(defined $min_date && defined $max_date);
my $random_date = get_random_date($lang->locale, $min_date, $max_date);
my $random_date = get_random_date(
$lang->locale, $min_date, $max_date
) or return;
my ($formatted, $min_date_formatted, $max_date_formatted) = map {
format_date($format, $_, $force_cldr);
} ($random_date, $min_date, $max_date);
Expand Down Expand Up @@ -144,6 +146,7 @@ my $MIN_DATE = -62_167_219_200;
sub get_random_date {
my ($locale, $min_date, $max_date) = @_;
my $range = abs($max_date->epoch - $min_date->epoch);
return if $range == 0;
my $rand_num = int(rand($range));
my $rand_epoch = $min_date->epoch + $rand_num;
return DateTime->from_epoch(
Expand Down
1 change: 1 addition & 0 deletions t/RandomDate.t
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ ddg_goodie_test(
'random date between 2005-06-10 and 2006-06-11' => build_range_test(
'Date', 'Jun 10, 2005', 'Jun 11, 2006',
),
'random date between 2005-06-10 and 2005-06-10' => undef,
'random date between' => undef,
'random date between now and bar' => undef,
'random date between now' => undef,
Expand Down

0 comments on commit 8347689

Please sign in to comment.