# Time Delta

Flavio S. Glock fglock at pucrs.br
Thu Oct 2 07:35:35 CDT 2003

```Rick Measham wrote:
>
> At 5:07 PM +1000 2/10/03, Scott Penrose wrote:
>
> >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 DateTimers,
> Any clues on why the intersections don't create properly?

Rick:

Creating a complex recurrence function is hard.
It is much easier to use DT::Event::Recurrence:

use DateTime;
use DateTime::Span;
use DateTime::Set;
use DateTime::SpanSet;
use DateTime::Event::Recurrence;

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::Event::Recurrence->weekly(
days => [ 1,2,3,4,5 ],
hours => [ 9 ],
);

\$wd_end = DateTime::Event::Recurrence->weekly(
days => [ 1,2,3,4,5 ],
hours => [ 17 ],
);

\$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);

----
\$VAR1 = 'months';
\$VAR2 = 0;
\$VAR3 = 'days';
\$VAR4 = 0;
\$VAR5 = 'minutes';
\$VAR6 = 160;
\$VAR7 = 'seconds';
\$VAR8 = 0;
\$VAR9 = 'nanoseconds';
\$VAR10 = 0;

- Flavio S. Glock

```