in reply to DateTime::Span intersection inconsistencies

If a one-hour event starts at 12:00, the event is over by 13:00. The test is wrong.
  • Comment on Re: DateTime::Span intersection inconsistencies

Replies are listed 'Best First'.
Re^2: DateTime::Span intersection inconsistencies
by mikeman (Acolyte) on Jun 30, 2011 at 17:15 UTC

    Which test? The first test where there is an intersection, or the second test where this is no intersection? Note that the end time and duration of each span is the same.

    As I wrote in my update, I now understand why I get those results, but they are still inconsistent. An event that ends at 13:00 either intersects with an event that starts at 13:00, or it doesn't -- surely it can't do both.

      Which test?

      I only investigated the failing test, #5.

      Note that the end time and duration of each span is the same.

      That could indicate a problem with test #2 or the possibility for confusion in reading the code for test #2.

      There's no problem with test #2 because the docs clearly show one should use before instead of end if you want test #2 to be consistent with test #5.


      Fixed test:

      use strict; use warnings; use DateTime; use DateTime::Span; use Test::More tests => 6; my $dt1 = DateTime->new( day => 1, month => 1, year => 2011, hour => 12, minute => 0, second => 0, ); my $dt2 = $dt1->clone->add( hours => 1 ); my $duration = DateTime::Duration->new( hours => 1 ); { my $span = DateTime::Span->from_datetimes( start => $dt1, before => $dt2, ); diag $span->start->datetime(); diag $span->end->datetime(); ok( $span->intersects( $dt1 ), 'Span intersects $dt1' ); ok( !$span->intersects( $dt2 ), 'Span doesn\'t intersects $dt2' ); is( DateTime::Duration->compare( $span->duration, $duration ), 0, 'Duration is 1 hour' ); } { my $span = DateTime::Span->from_datetime_and_duration( start => $dt1, duration => $duration, ); diag $span->start->datetime(); diag $span->end->datetime(); ok( $span->intersects( $dt1 ), 'Span intersects $dt1' ); ok( !$span->intersects( $dt2 ), 'Span doesn\'t intersects $dt2' ); is( DateTime::Duration->compare( $span->duration, $duration ), 0, 'Duration is 1 hour' ); } 1;
      1..6 # 2011-01-01T12:00:00 # 2011-01-01T13:00:00 ok 1 - Span intersects $dt1 ok 2 - Span doesn't intersects $dt2 ok 3 - Duration is 1 hour # 2011-01-01T12:00:00 # 2011-01-01T13:00:00 ok 4 - Span intersects $dt1 ok 5 - Span doesn't intersects $dt2 ok 6 - Duration is 1 hour

      PS - I used ok instead of is because intersects is documented to return a boolean, not zero or one.

        There's no problem with test #2 because the docs clearly show one should use before instead of end if you want test #2 to be consistent with test #5.

        Agreed. The inconsistency arises when looking only at the times, and ignoring the open or closed state of the spans -- that was my mistake.

        The problem arose when creating recurring start and end sets, and turning them into spans with DateTime::SpanSet. Its docs don't mention that the from_set_and_duration method creates open spans, so that's where my confusion began! Alas, its from_sets method also creates open spans.

        Now that I know, it's not really a problem. But I'm still curious to know why open spans are created by default -- there doesn't appear to be a way to change this, and perhaps for good reason.