- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- 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
Forums
Discussions
Discussions
Forums
Discussions
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
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.