Room for one more? Here's a generic Time::Piece solution:
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, $fm +t)), "$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]; }
Feel free to expand the @within and @without arrays with more test cases. You can also change $mon to whatever number you require. Time::Piece is in core so there are no extra dependencies needed here.
In reply to Re: More accurate way to calculate Date difference
by hippo
in thread More accurate way to calculate Date difference
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |