Time Delta
Rick Measham
rickm at printaform.com.au
Thu Oct 2 03:08:55 CDT 2003
At 5:07 PM +1000 2/10/03, Scott Penrose wrote:
>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>I am trying to do the age old problem...
>
>* Here is DateTime 1
>* Here is DateTime 2
>* Here is the working week (Mon-Fri, 9-5) (or whatever we define)
>* Tell me the working hours between DateTime 1 and DateTime 2
>
>eg:
>
> DateTime 1 DateTime 2 Difference
>
> 19/8/2003 16:35 20/8/2003 11:15 2:40
>
>or - 2 hours, 40 minutes
>
>What is the simplest, shortest way.
G'day Scott,
I promised a few months back to talk about DateTime sometime soon.
Tell me when and I'll be there. The following *should* solve your
problem, but doesn't. I get a strange error at the end when creating
the intersection. To solve this (and maybe to get a better answer)
I've CC'd this to the DateTime mailing list.
G'day DateTimers,
Any clues on why the intersections don't create properly?
use DateTime;
use DateTime::Span;
use DateTime::Set;
use DateTime::SpanSet;
use Data::Dumper; # For displaying results
$datetime1 = DateTime->new(
year => 2003,
month => 8,
day => 19,
hour => 16,
minute => 35
);
$datetime2 = DateTime->new(
year => 2003,
month => 8,
day => 20,
hour => 11,
minute => 15
);
$span = DateTime::Span->from_datetimes(
start => $datetime1,
end => $datetime2
);
$wd_start = DateTime::Set->from_recurrence(
recurrence => sub {
# Tomorrow at 9 am
$_[0]->truncate( to => 'day' )->add( days => 1, hours => 9 );
# Add two days if it's Saturday
$_[0]->add( days => 2) if $_[0]->day == 6;
},
);
$wd_end = DateTime::Set->from_recurrence(
recurrence => sub {
# Tomorrow at 5pm
$_[0]->truncate( to => 'day' )->add( days => 1, hours => 17 );
# Add two days if it's Saturday
$_[0]->add( days => 2) if $_[0]->day == 6;
},
);
$working_hours = DateTime::SpanSet->from_sets(
start_set => $wd_start,
end_set => $wd_end,
);
$relevent_work_hours = $working_hours->intersection( $span );
print Dumper($relevent_work_hours->duration->deltas);
More information about the Melbourne-pm
mailing list