http://qs321.pair.com?node_id=1033231

eibwen has asked for the wisdom of the Perl Monks concerning the following question:

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?