Time Delta

Timothy S. Nelson wayland at smartchat.net.au
Fri Oct 3 10:43:48 CDT 2003


On Thu, 2 Oct 2003, Flavio S. Glock wrote:

> 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

	I've been working on an extension to Time::Piece which I call 
Time::Period.  Then you can subtract one Time::Period from another, and it 
gives a Time::Seconds which is the length of the first time period minus the 
length of the *overlapping* second time period.  

	It'd be nice if we had an easy way to shoot this problem in the head 
:).  

> > >
> > >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
> 

---------------------------------------------------------------------
| Name: Tim Nelson                 | Because the Creator is,        |
| E-mail: wayland at smartchat.net.au | I am                           |
---------------------------------------------------------------------

----BEGIN GEEK CODE BLOCK----
Version 3.12
GCS d+ s:- a- C++>++++$ U++ P++ L++ E- W+++ N+ w>--- V- Y+>++ 
PGP->++ R !tv b++ DI++++ D+ G e++>++++ h! y-
-----END GEEK CODE BLOCK-----





More information about the Melbourne-pm mailing list