Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) 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'); $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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found