use strict; use warnings; use Time::Piece; use Test::More; my $fmt = '%d/%m/%Y'; my $date = Time::Piece->strptime ('21/02/2013', $fmt); my @within = ('12/08/2013'); my @without = ('29/08/2013'); my $mon = 6; plan tests => @within + @without; for my $other (@within) { ok (within_months ($mon, $date, Time::Piece->strptime ($other, $fmt)), "$other is within $mon months of $date"); } for my $other (@without) { ok (! within_months ($mon, $date, Time::Piece->strptime ($other, $fmt)), "$other is not within $mon months of $date"); } sub within_months { my ($mon, @d) = @_; if ($d[0] > $d[1]) { push @d, shift @d }; my $start = $d[0]->add_months ($mon); return $start > $d[1]; }