good chemistry is complicated,and a little bit messy -LW PerlMonks

### comment on

 Need Help??

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');
is(\$biweekly->contains(\$date), 0, \$date->ymd . ' is *NOT* the bi-weekl
+y set');
is(\$biweekly->contains(\$date), 1, \$date->ymd . ' *IS* the bi-weekly se
+t');
is(\$biweekly->contains(\$date), 0, \$date->ymd . ' is *NOT* the bi-weekl
+y set');
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?

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

• Are you posting in the right place? Check out Where do I post X? to know for sure.
• Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
• Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
• Want more info? How to link or How to display code and escape characters are good places to start.

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2023-03-22 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (60 votes). Check out past polls.

Notices?