Operating System - Linux
1827840 Members
1350 Online
109969 Solutions
New Discussion

How to add time intervals in perl

 
SOLVED
Go to solution
Pankaj Yadav_1
Frequent Advisor

How to add time intervals in perl

Hi,

My time intervals are in the form hours:mins:secs and are stored in variables.

I want to know how can we add the two variables?

Please answer me .
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: How to add time intervals in perl

Hi:

Perhaps this will show you:

#!/usr/bin/perl
use strict;
use warnings;
my $t = "1:02:03";
my ($hr, $min, $sec) = split /:/, $t;
my $elapsed = ($hr * 3600) + ($min * 60) + $sec;
print "elapsed time = $elapsed\n";

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: How to add time intervals in perl

Expanding on James's solution
NO POINTS please

#!/usr/contrib/bin/perl
my $t = "1:02:03";
my $t2 = "10:58:59";
my ($hr, $min, $sec) = split /:/, $t;
my ($hr2, $min2, $sec2) = split /:/, $t2;
my $hr3 = $hr + $hr2;
my $min3 = $min + $min2;
my $sec3 = $sec + $sec2;
my $elapsed = ($hr3 * 3600) + ($min3 * 60) + $sec3;
my $hr4 = int $elapsed / 3600;
my $new = $elapsed % 3600 ;
my $min4 = int $new / 60;
my $sec4 = $new % 60 ;
printf "total time = %02d:%02d:%02d\n",$hr4,$min4,$sec4;
Pankaj Yadav_1
Frequent Advisor

Re: How to add time intervals in perl

Hi,

Actually I have two variables like

$a='12:23:44' and $b='1:32:22'

I want to perform
$c=$a+$b;

The result should also be in the form hours:mins:secs

Please answer me !
Peter Nikitka
Honored Contributor

Re: How to add time intervals in perl

Hi,

I'm pretty shure you didn't try Peter G. response - of course you have to set $t and $t2 accordingly instead of $a and $b.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Pankaj Yadav_1
Frequent Advisor

Re: How to add time intervals in perl

Actually my time intervals are stored in an array.
Please tell me how to take each value from the array and add them and store the final result in a variable in the form hours:mins:sec
James R. Ferguson
Acclaimed Contributor

Re: How to add time intervals in perl

Hi (again):

You wrote:

/* begin_quote */

Actually I have two variables like

$a='12:23:44' and $b='1:32:22'

I want to perform
$c=$a+$b;

The result should also be in the form hours:mins:secs

/* end_quote */

You really have the answer if you think about it. Convert both $a and $b into seconds as I suggested; add the resultants ($c); and now 'div' and 'mod' as Peter Gordon did in his post!

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to add time intervals in perl

Hi (again):

OK, so your variables are contained in an array. Consider this example:

# perl -le '@a=qw (1:02:03 2:00:00);$t1=$a[0];$t2=$a[1];print "t1=$t1 t2=$t2"'

Remember that Perl (like any good language) treats things zero-relative. Thus, the first array element is numbered zero, and the second element is element-1.

Regards!

...JRF...
Pankaj Yadav_1
Frequent Advisor

Re: How to add time intervals in perl

I don't know the values neither I know the size of the array since the values present in it is from a SQL query.

I want to add all the values and store the result in another variable.
James R. Ferguson
Acclaimed Contributor

Re: How to add time intervals in perl

Hi:

You wrote, "I don't know the values neither I know the size of the array since the values present in it is from a SQL query.
I want to add all the values and store the result in another variable."

The size of the array doesn't matter. I assume that you know the element numbers where the data occurs.

I have shown you in the abstract how to extract elements from an array or list; split them into pieces; and sum them.

Your question keeps changing. I suggest that you post *exactly* what you have tried to write so that we can attempt to direct you.

Regards!

...JRF...
Pankaj Yadav_1
Frequent Advisor

Re: How to add time intervals in perl

I think I have got the solution.

Thanks a lot for all the pain you all took !

If I get further problems I will post here .
This forum is great since the members are great.

Thanks a lot once again.
Marvin Strong
Honored Contributor
Solution

Re: How to add time intervals in perl

let me make sure I understand.

You don't know which elements of your array contain the values you want to add? But you want to add them and store them as a different variable?

If thats the case you need to figure out what elements they are (print the array) or return the data from your query into a more useful data structure.