On 8/11/13 1:25 PM, Flavio S. Glock wrote:
http://search.cpan.org/dist/**DateTime-Set/lib/DateTime/**SpanSet.pm<http://search.cpan.org/dist/DateTime-Set/lib/DateTime/SpanSet.pm>
next() and previous() can be used to determine whether a given point
in time falls between, before, or after the segments.
intersects() can tell whether a new segment would intersect existing
segments.
Flavio, thank you for pointing me to that distribution. I have been
experimenting with DateTime::Span and find it helpful, but there's a
problem with DateTime::SpanSet -- or perhaps with my understanding of that
module.
Once again, my original problem:
2013/8/11 James E Keenan<jk...@verizon.net>:
I need to use DateTime functionality and would like to know
if the particular functionality I need already exists on
CPAN.
I need to construct a series of time segments which do not
overlap but which do not need to be contiguous. Each
segment is defined by a start date which is part of the
segment and by an end date which is the first moment in time
after the segment's conclusion.
I can use a start date and an end date to compose a DateTime::Span object
for each segment. (It turns out not to matter so much whether the the
segment is, in DT::S parlance, 'semi-open' or 'closed', i.e., whether I
define the object with the second date with 'before' or with 'end'.)
However, I see that when (a) the end date of one segment is identical to
the start date of the next segment; and (b) I use DateTime::SpanSet to
create a spanset; then those two segments are, in effect, merged -- which
seems counterintuitive. The only way I can get a spanset with the same
number of elements as the spans going in is to make sure that the end date
of one segment is one unit of time less than the start date of the second
segment.
The attached program, bspanset.pl, illustrates this problem. As a
convenience, I have divided the program into 7 sections named with roman
numerals.
In Section I, I create 4 pairs of DateTime objects. The end date of the
first pair is identical to the start date of the second pair. Likewise for
the third and fourth pairs. But there is a gap between the second and
third pairs. The output:
# I:
start: 2012-12-15T00:00:00 end: 2013-01-01T00:00:00
start: 2013-01-01T00:00:00 end: 2013-02-01T00:00:00
start: 2013-03-01T00:00:00 end: 2013-04-01T00:00:00
start: 2013-04-01T00:00:00 end: 2013-04-15T00:00:00
In Section II, I create DateTime::Span objects from each pair. I use
'before' to create semi-open objects.
# II:
start: 2012-12-15T00:00:00 end: 2013-01-01T00:00:00
start: 2013-01-01T00:00:00 end: 2013-02-01T00:00:00
start: 2013-03-01T00:00:00 end: 2013-04-01T00:00:00
start: 2013-04-01T00:00:00 end: 2013-04-15T00:00:00
In Section III, I create a DateTime::SpanSet object from the 4
DateTime::Span objects. But this SpanSet object only contains two
elements. The first and second have been merged, as have the third and
fourth.
# III:
start: 2012-12-15 end: 2013-02-01
start: 2013-03-01 end: 2013-04-15
In Sections IV an V, I repeat II and III, only with 'end' to create closed
objects. I still end up with only two elements in the DateTime::SpanSet
object.
# IV:
start: 2012-12-15T00:00:00 end: 2013-01-01T00:00:00
start: 2013-01-01T00:00:00 end: 2013-02-01T00:00:00
start: 2013-03-01T00:00:00 end: 2013-04-01T00:00:00
start: 2013-04-01T00:00:00 end: 2013-04-15T00:00:00
# V:
start: 2012-12-15 end: 2013-02-01
start: 2013-03-01 end: 2013-04-15
In Section VI, I subtract one second from the latter date in each pair,
then use that to create a closed object.
# VI:
start: 2012-12-15T00:00:00 end: 2012-12-31T23:59:59
start: 2013-01-01T00:00:00 end: 2013-01-31T23:59:59
start: 2013-03-01T00:00:00 end: 2013-03-31T23:59:59
start: 2013-04-01T00:00:00 end: 2013-04-14T23:59:59
In Section VII, I created a DateTime::SpanSet object from the four
DateTime::Span objects. Only now do I get what I intuitively thought I
should get, namely, a SpanSet object with four elements.
# VII:
start: 2012-12-15 end: 2012-12-31
start: 2013-01-01 end: 2013-01-31
start: 2013-03-01 end: 2013-03-31
start: 2013-04-01 end: 2013-04-14
Am I using DateTime::SpanSet correctly?
Thank you very much.
Jim Keenan