NAME

Date::Range - deal with a range of dates

SYNOPSIS

use Date::Range;

my $range = Date::Range->new($date1, $date2);

my $earliest = $range->start;
my $latest   = $range->end;
my $days     = $range->length;

if ($range->includes($date3)) { ... }
if ($range->includes($range2)) { ... }

if ($range->overlaps($range2)) {
  my $range3 = $range->overlap($range2);
}

foreach my $date ($range->dates) { ... }

DESCRIPTION

Quite often, when dealing with dates, we don't just want to know information about one particular date, but about a range of dates. For example, we may wish to know whether a given date is in a particular range, or what the overlap is between one range and another. This module lets you ask such questions.

METHODS

new()

my $range = Date::Range->new($date1, $date2);

A range object is instantiated with two dates, which do not need to be in chronological order (we'll sort all that out internally).

These dates must be instances of the Date::Simple class.

start() / end()

my $earliest = $range->start;
my $latest   = $range->end;
my $days     = $range->length;

These methods allow you retrieve the start and end dates of the range, and the number of days in the range.

equals

if ($range1->equals($range2)) { }

This tells you if two ranges are the same - i.e. start and end at the same dates.

includes

if ($range->includes($date3)) { ... }
if ($range->includes($range2)) { ... }

These methods tell you if a given range includes a given date, or a given range.

overlaps / overlap

if ($range->overlaps($range2)) {
  my $range3 = $range->overlap($range2);
}

These methods let you know whether one range overlaps another or not, and access this overlap range.

dates

foreach my $date ($range->dates) { ... }

This returns a list of each date in the range as a Date::Simple object.

BUGS

None known.

AUTHOR

Tony Bowden, <cpan@tmtm.com>, based heavily on Martin Fowler's "Analysis Patterns 2" discussion and code at http://www.martinfowler.com/ap2/range.html

COPYRIGHT

Copyright (C) 2001-2003 Tony Bowden. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.