Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

given time1a and b how much is in time2a and b

by blueberryCoffee (Scribe)
on Mar 30, 2005 at 22:21 UTC ( #443622=perlquestion: print w/replies, xml ) Need Help??

blueberryCoffee has asked for the wisdom of the Perl Monks concerning the following question:

Hello all wise monks, I am making a time tracking program. Currently I record the start time and end time (with perl's time function) of a project as I work on it each day. Are there any functions anyone knows of that I can use to show how much time was spent within a range?

I guess I am envisioning something like
$secs += secondsInRange($projectStart, $projectEnd, "1/12/2005", "1/18/2005")
Or
$secs += secondsInRange($time1a, $time1b, $time2a, $time2b)

Thanks in advance,

Replies are listed 'Best First'.
Re: given time1a and b how much is in time2a and b
by tlm (Prior) on Mar 30, 2005 at 22:35 UTC

    Sounds like interval arithmetic:

    use List::Util qw( min max ); sub overlap { my @intervals = @_[0, 1]; my $left = max( map $_->[ 0 ], @intervals ); my $right = min( map $_->[ 1 ], @intervals ); return max( 0, $right - $left ); }
    Just turn the 3rd and 4th args to seconds relative to the epoch, and use the above function, with the intervals passed as array refs [ $time1a, $time1b ], [ $time2a, $time2b ].

    the lowliest monk

Re: given time1a and b how much is in time2a and b
by fglock (Vicar) on Mar 31, 2005 at 03:47 UTC

    DateTime::SpanSet can do this for you:

    use DateTime::SpanSet; my $dt1 = DateTime->new( year => 2005, month => 1, day => 1 ); my $dt2 = DateTime->new( year => 2005, month => 1, day => 4 ); my $dt3 = DateTime->new( year => 2005, month => 1, day => 7 ); my $dt4 = DateTime->new( year => 2005, month => 1, day => 9 ); my $dt_spanset = DateTime::SpanSet->from_spans( spans => [ DateTime::Span->from_datetimes( start => $dt1, end => $dt2 ), DateTime::Span->from_datetimes( start => $dt3, end => $dt4 ), ] ); print "duration: @{[ $dt_spanset->duration->deltas ]}\n"; # duration: months 0 days 5 minutes 0 seconds 0 nanoseconds 0

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2023-01-27 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?