Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Determine if a given DateTime is a member of a DateTime::Set

by vagabonding electron (Curate)
on May 13, 2013 at 13:34 UTC ( [id://1033295]=note: print w/replies, xml ) Need Help??


in reply to Determine if a given DateTime is a member of a DateTime::Set

Completely misunderstood the problem. Please ignore.

My 2 cents.

IMHO: the methods contains, intersects etc. require DateTime::Set as an argument, not a DateTime.

The following code works for me (I have got it here on PM and I am glad that I have a chance to share it further):

#!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; use Datetime::Set; my $strp = DateTime::Format::Strptime->new( pattern => '%Y-%m-%d %T', +); my $datf = qq{2012-01-01 04:00:00}; my $datt = qq{2012-01-02 23:00:00}; my $start = $strp->parse_datetime($datf); my $end = $strp->parse_datetime($datt); my $iter; my $set = DateTime::Set->from_recurrence( after => $start, before => $end, recurrence => sub { return $_[0]->add( hours => 1 )->truncate( to => 'hour' ) }, ); my $datf_plus = $start->add( hours => 10 ); my $set_2 = DateTime::Set->from_datetimes( dates => [ $datf_plus ] ); if ( $set->contains( $set_2 ) ) # or intersects { print "\nYes!\n"; print $set->contains( $set_2 ), "\n"; } else { print "\nNo!\n"; print $set->contains( $set_2 ), "\n"; }

prints "Yes!" and 1.

Update 2:

Misunderstanding, irrelevant. Sorry

Update

After looking at your output again: your DateTime::Set contains five elements only, not the time between. That's why your test 2 fails, since e.g. 2013-05-20 is not in your set of:

FIRST 5 ELEMENTS OF SET 2013-05-13T00:00:00 2013-05-27T00:00:00 2013-06-10T00:00:00 2013-06-24T00:00:00 2013-07-08T00:00:00

What you probably need is DateTime::Span.

#!/usr/bin/perl use strict; use warnings; use DateTime::Span; my $date1 = DateTime->new( year => 2002, month => 3, day => 11 ); my $date2 = DateTime->new( year => 2002, month => 5, day => 12 ); my $span = DateTime::Span->from_datetimes( start => $date1, end => $date2 ); my @timestamps; for my $month ( 1 .. 5) { my $date_x = DateTime->new( year => 2002, month => $month, day => 22 ); push @timestamps, $date_x; } for my $timestamp ( @timestamps ) { if ( $span->contains( $timestamp ) ) { print $timestamp->datetime, ' ', $span->contains( $timestamp ), " Yes!\n"; } else { print $timestamp->datetime, ' ', $span->contains( $timestamp ), " No!\n"; } }

prints:

2002-01-22T00:00:00 0 No! 2002-02-22T00:00:00 0 No! 2002-03-22T00:00:00 1 Yes! 2002-04-22T00:00:00 1 Yes! 2002-05-22T00:00:00 0 No!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1033295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found