- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- How to add time intervals in perl
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 11:29 PM
08-03-2006 11:29 PM
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 .
Solved! Go to Solution.
- Tags:
- date arithmetic
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 11:40 PM
08-03-2006 11:40 PM
Re: How to add time intervals in perl
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:07 AM
08-04-2006 12:07 AM
Re: How to add time intervals in perl
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:08 AM
08-04-2006 12:08 AM
Re: How to add time intervals in perl
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 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:21 AM
08-04-2006 12:21 AM
Re: How to add time intervals in perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:24 AM
08-04-2006 12:24 AM
Re: How to add time intervals in perl
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:28 AM
08-04-2006 12:28 AM
Re: How to add time intervals in perl
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:37 AM
08-04-2006 12:37 AM
Re: How to add time intervals in perl
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:45 AM
08-04-2006 12:45 AM
Re: How to add time intervals in perl
I want to add all the values and store the result in another variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:51 AM
08-04-2006 12:51 AM
Re: How to add time intervals in perl
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:55 AM
08-04-2006 12:55 AM
Re: How to add time intervals in perl
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2006 12:56 AM
08-04-2006 12:56 AM
SolutionYou 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.