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

I have to calculate the time difference of status from 0 to 1. for example If status was down at 2008-04-04 00:00:00 to 2008-04-04 02:10:00 and again if the status was 0 at 2008-04-04 03:00:00 to 2008-04-04 03:10:00 I should calculate as time differnce was 2hrs 10 min and also 10min. I mean to calculate the time when status was 0 for a day

Replies are listed 'Best First'.
Re: Calculate time difference
by dragonchild (Archbishop) on May 12, 2008 at 14:09 UTC
    Learn DateTime, Date::Calc, or Date::Manip. I prefer DateTime.

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Calculate time difference
by grizzley (Chaplain) on May 12, 2008 at 08:50 UTC

    I think this can be achieved with proper db query. But if you need Perl script to do this, use something like this:

    $refvalues= [['1', '2008-04-04 17:30:01'], .... ['1', '2008-04-04 22:10:02']]; sub calcdiff { ($d1, $d2) = @_; return ??? } $is_in_state_0 = 0; $start_date = ''; $end_date = ''; for(@$refvalues) { if($is_in_state_0) { if($$_[0] eq '1') { $end_date = $$_[1]; $is_in_state_0 = 0; print 'status 0 detected between '.$start_date.' and '.$en +d_date.' => '. calcdiff($start_date, $end_date)."\n" } } else { if($$_[0] eq '0') { $is_in_state_0 = 1; $start_date = $$_[1]; } } }

    And here you will find description how to fill calcdiff() function

Re: Calculate time difference
by psini (Deacon) on May 12, 2008 at 08:40 UTC

    Could you state what you really need: I'm assuming that the table shows the status of a system vs time and you are trying to find the length of the periods in which the status had been up or down.

    The approach should be different if you are looking for a single event (i.e. "how long status had been on before going down at xxxx") or you want to chart the up/down length from the entire table or you want to feed this data in another db table