NAME
DateTime::SpanSet - set of DateTime spans
SYNOPSIS
$spanset = DateTime::SpanSet->from_spans( spans => [ $dt_span, $dt_span ] );
$set = $spanset->union( $set2 ); # like "OR", "insert", "both"
$set = $spanset->complement( $set2 ); # like "delete", "remove"
$set = $spanset->intersection( $set2 ); # like "AND", "while"
$set = $spanset->complement; # like "NOT", "negate", "invert"
if ( $spanset->intersects( $set2 ) ) { ... # like "touches", "interferes"
if ( $spanset->contains( $set2 ) ) { ... # like "is-fully-inside"
# data extraction
$date = $spanset->min; # first date of the set
$date = $spanset->max; # last date of the set
$iter = $spanset->iterator;
while ( $dt = $iter->next ) {
# $dt is a DateTime::Span
print $dt->start->ymd; # first date of span
print $dt->end->ymd; # last date of span
};
DESCRIPTION
DateTime::SpanSet is a class that represents sets of datetime spans. An example would be a recurring meeting that occurs from 13:00-15:00 every Friday.
METHODS
from_spans
Creates a new span set from one or more
DateTime::Span
objects.$spanset = DateTime::SpanSet->from_spans( spans => [ $dt_span ] );
from_set_and_duration
Creates a new span set from one or more
DateTime::Set
objects and a duration.The duration can be a
DateTime::Duration
object, or the parameters to create a newDateTime::Duration
object, such as "days", "months", etc.$spanset = DateTime::SpanSet->from_set_and_duration ( set => $dt_set, days => 1 );
from_sets
Creates a new span set from two
DateTime::Set
objects.One set defines the starting dates, and the other defines the end dates.
$spanset = DateTime::SpanSet->from_sets ( start_set => $dt_set1, end_set => $dt_set2 );
The spans have the starting date
closed
, and the end dateopen
, like in[$dt1, $dt2)
.If an end date comes without a starting date before it, then it defines a span like
(-inf, $dt)
.If a starting date comes without an end date after it, then it defines a span like
[$dt, inf)
.empty_set
Creates a new empty set.
clone
This object method returns a replica of the given object.
set_time_zone( $tz )
This method accepts either a time zone object or a string that can be passed as the "name" parameter to
DateTime::TimeZone->new()
. If the new time zone's offset is different from the old time zone, then the local time is adjusted accordingly.If the old time zone was a floating time zone, then no adjustments to the local time are made, except to account for leap seconds. If the new time zone is floating, then the UTC time is adjusted in order to leave the local time untouched.
The method returns a new object.
min / max
First or last dates in the set. These methods may return
undef
if the set is empty. It is also possible that these methods may return a scalar containing infinity or negative infinity.duration
The total size of the set, as a
DateTime::Duration
object, or as a scalar containing infinity.Also available as
size()
.span
The total span of the set, as a
DateTime::Span
object.previous / next
my $span = $set->next( $dt ); my $span = $set->previous( $dt );
These methods are used to find a set member relative to a given datetime or span.
The return value may be
undef
if there is no matching span in the set.as_list
Returns a list of
DateTime::Span
objects.my @dt = $set->as_list( span => $span );
Just as with the
iterator()
method, theas_list()
method can be limited by a span.If a set is specified as a recurrence and has no fixed begin and end datetimes, then
as_list
will returnundef
unless you limit it with a span. Please note that this is explicitly not an empty list, since an empty list is a valid return value for empty sets!union / intersection / complement
Set operations may be performed not only with
DateTime::SpanSet
objects, but also withDateTime
,DateTime::Set
andDateTime::Span
objects. These set operations always return aDateTime::SpanSet
object.$set = $spanset->union( $set2 ); # like "OR", "insert", "both" $set = $spanset->complement( $set2 ); # like "delete", "remove" $set = $spanset->intersection( $set2 ); # like "AND", "while" $set = $spanset->complement; # like "NOT", "negate", "invert"
intersects / contains
These set functions return a boolean value.
if ( $spanset->intersects( $set2 ) ) { ... # like "touches", "interferes" if ( $spanset->contains( $dt ) ) { ... # like "is-fully-inside"
These methods can accept a
DateTime
,DateTime::Set
,DateTime::Span
, orDateTime::SpanSet
object as an argument.iterator / next / previous
This method can be used to iterate over the spans in a set.
$iter = $spanset->iterator; while ( $dt = $iter->next ) { # $dt is a DateTime::Span print $dt->min->ymd; # first date of span print $dt->max->ymd; # last date of span }
The boundaries of the iterator can be limited by passing it a
span
parameter. This should be aDateTime::Span
object which delimits the iterator's boundaries. Optionally, instead of passing an object, you can pass any parameters that would work for one of theDateTime::Span
class's constructors, and an object will be created for you.Obviously, if the span you specify does is not restricted both at the start and end, then your iterator may iterate forever, depending on the nature of your set. User beware!
The
next()
orprevious()
methods will returnundef
when there are no more spans in the iterator.start_set
end_set
These methods do the inverse of the
from_sets
method:start_set
retrieves a DateTime::Set with the start datetime of each span.end_set
retrieves a DateTime::Set with the end datetime of each span.iterate
Experimental method - subject to change.
This function apply a callback subroutine to all elements of a set and returns the resulting set.
The parameter
$_[0]
to the callback subroutine is aDateTime::Span
object.[TODO - fix example] sub callback { $_[0]->add( hours => 1 ); } # $set2 elements are one hour after $set elements, and # $set is unchanged $set2 = $set->iterate( \&callback );
If the callback returns
undef
, the datetime is removed from the set:sub remove_sundays { $_[0] unless $_[0]->start->day_of_week == 7; }
The callback can be used to postpone or anticipate events which collide with datetimes in another set:
[TODO - fix example] sub after_holiday { $_[0]->add( days => 1 ) while $holidays->contains( $_[0] ); }
The callback return value is expected to be within the span of the
previous
and thenext
element in the original set.For example: given the set
[ 2001, 2010, 2015 ]
, the callback result for the value2010
is expected to be within the span[ 2001 .. 2015 ]
.The callback subroutine may not be called immediately. Don't count on subroutine side-effects. For example, a
print
inside the subroutine may happen later than you expect.
SUPPORT
Support is offered through the datetime@perl.org
mailing list.
Please report bugs using rt.cpan.org
AUTHOR
Flavio Soibelmann Glock <fglock@pucrs.br>
The API was developed together with Dave Rolsky and the DateTime Community.
COPYRIGHT
Copyright (c) 2003 Flavio Soibelmann Glock. All rights reserved. This program is free software; you can distribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
SEE ALSO
Set::Infinite
For details on the Perl DateTime Suite project please see http://datetime.perl.org.