- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: Perl - calculate time difference
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
Discussions
Discussions
Forums
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
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-25-2005 08:25 AM
тАО08-25-2005 08:25 AM
Perl - calculate time difference
I am doing the following:
my $time1 = substr($data[3],9,6);
my $time2 = substr($data[4],9,6);
my $timediff = $time2 - $time1;
if($timediff < 0)
{
$timediff += 60*60*24;
}
But still I am getting time difference in negative.
Please help.
Thanks,
Anand
- Tags:
- date arithmetic
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-25-2005 08:27 AM
тАО08-25-2005 08:27 AM
Re: Perl - calculate time difference
http://hpux.ws/merijn/caljd-2.2.pl
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-25-2005 08:43 AM
тАО08-25-2005 08:43 AM
Re: Perl - calculate time difference
#!/usr/bin/perl -w
use strict;
use Time::Local;
my $t1 = "08252005194248";
my $t2 = "08252005194249";
sub to_seconds
{
use integer;
my $x = $_[0];
my $mo = substr($x,0,2);
my $day = substr($x,2,2);
my $year = substr($x,4,4);
my $hour = substr($x,8,2);
my $minute = substr($x,10,2);
my $second = substr($x,12,2);
my $t = timelocal($second,$minute,$hour,$day,$mo - 1,$year - 1900);
return($t);
}
my $diff = to_seconds($t2) - to_seconds($t1);
printf("Diff = %d seconds\n",$diff);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-25-2005 08:52 AM
тАО08-25-2005 08:52 AM
Re: Perl - calculate time difference
Are you running with 'use warnings' or at least '-w' ???
Looking at your code in isolation, this would lead to a negative computation if you data[4] wasn't defined.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-25-2005 01:36 PM
тАО08-25-2005 01:36 PM
Re: Perl - calculate time difference
---------
use Time::Local;
sub seconds {
($mo,$d,$y,$h,$mi,$s) = unpack 'a2a2a4a2a2a2', $_[0];
return timelocal($s,$mi,$h,$d,$mo - 1,$y - 1900);
}
$t1 = seconds (shift @ARGV);
$t2 = seconds (shift @ARGV);
print $t1 - $t2;
------
$ perl tmp.p 08252005194248 08252005184248
3600
$ perl tmp.p 08252005194248 08242005194248
86400
Hein.