Objective: Determine if a given day is an occurrence of a bi-weekly event.

Approach: Given the complexities of dates, opted to use a date module, hence DateTime::Set.

Code:

#!/usr/bin/perl -w use strict; use diagnostics; use DateTime; use DateTime::Set; use Test::More; my $biweekly = DateTime::Set->from_recurrence( 'recurrence' => sub { return $_[0]->truncate('to' => 'day')->add('days' => 1 +4) }, 'start' => DateTime->today() ); print 'FIRST 5 ELEMENTS OF SET', $/; my $iterator = $biweekly->iterator; for (0..4) { my $date = $iterator->next; print $date->datetime, $/; } print $/; print 'TESTING whether Date is in set...', $/; my $date = DateTime->today(); is($biweekly->contains($date), 1, $date->ymd . ' *IS* in the bi-weekly + set'); $date->add('days' => 7); is($biweekly->contains($date), 0, $date->ymd . ' is *NOT* the bi-weekl +y set'); $date->add('days' => 7); is($biweekly->contains($date), 1, $date->ymd . ' *IS* the bi-weekly se +t'); $date->add('days' => 7); is($biweekly->contains($date), 0, $date->ymd . ' is *NOT* the bi-weekl +y set'); $date->add('days' => 7); is($biweekly->contains($date), 1, $date->ymd . ' *IS* the bi-weekly se +t'); # ... done_testing();

Output:

FIRST 5 ELEMENTS OF SET 2013-05-12T00:00:00 2013-05-26T00:00:00 2013-06-09T00:00:00 2013-06-23T00:00:00 2013-07-07T00:00:00 TESTING whether Date is in set... ok 1 - 2013-05-12 *IS* in the bi-weekly set not ok 2 - 2013-05-19 is *NOT* the bi-weekly set # Failed test '2013-05-19 is *NOT* the bi-weekly set' # at ./off-friday line 28. # got: '1' # expected: '0' ok 3 - 2013-05-26 *IS* the bi-weekly set not ok 4 - 2013-06-02 is *NOT* the bi-weekly set # Failed test '2013-06-02 is *NOT* the bi-weekly set' # at ./off-friday line 32. # got: '1' # expected: '0' ok 5 - 2013-06-09 *IS* the bi-weekly set 1..5 # Looks like you failed 2 tests of 5.

NOTE: get the same results with closest, contains, current, intersects methods.

CLARIFICATION UPDATE: I have working code using a different method. The question here is not how to accomplish the objective, but rather why didn't this particular method work as expected?

EXPLICITLY:
(a) WHY DID THE TESTS FAIL?
(b) HOW TO DO THIS PROPERLY WITH DateTime::Set?


In reply to Determine if a given DateTime is a member of a DateTime::Set by eibwen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.