coli ~ # perl t.pl
Sat Apr 8 14:09:05 2006
Sat Apr 8 14:09:10 2006
Elapsed: 0:0:5
but on another machine:
D:\TESTS>perl 3
Sat Apr 8 18:10:21 2006
Sat Apr 8 18:10:26 2006
Elapsed: 3:0:5
Obviously, this is just conversion of time onto your local timezone settings (mine is GMT+3, yours is GMT+1? CET?)
| [reply] [d/l] [select] |
yep, I'm i GMT+1, so i'll buy your explanation here
Tnx! -- allan
update:
my ($h,$m,$s) = (gmtime($t2-$t1))[2,1,0];
will DWIM -- allan
| [reply] [d/l] |
| [reply] |
Testing out your code, this is what happens. You subtracting the number of seconds from epoc for $t1 from the number of seconds from epoc for $2.
$t1 = 1000
$t2 = 1005
$sec_diff = $t2 - $t1;
print "Time elapsed $sec_diff\n";
When you call localtime it returning a date time format for the seconds for epoc based upon the time for WHERE YOU ARE!
print "GMT time is " . gmtime($sec_diff);
# Thu Jan 1 00:00:02 1970
print "Local time is " . localtime($sec_diff);
# Thu Jan 1 19:00:02 1970 - for me at least.
Note: I realized someone else had mentioned the gmtime but I just wanted to explain it further.
| [reply] [d/l] [select] |
<i have tried something very rudimentary hope u can refine and work on it let me know if you have something better
#!/usr/lib/perl -w
use strict;
use warnings;
my $t1 = time();
print "$t1 \n";
sleep 5;
my $t2 = time(); ;
print ($t2) ;
my $diff = $t2 - $t1;
my $mDiff = int($diff / 60);
my $sDiff = sprintf("%02d", $diff - 60 * $mDiff);
print "diff = $diff -> $mDiff\:$sDiff\n";
| [reply] [d/l] |
Thanks rjsaulakh
See my update above for another fix of the issue.
Best regards,
allan
| [reply] |
The problem is that when you subtract $t1 from $t2 you wind up with a number, that is incidentially both the duration that passed, in seconds, *and* a valid epoch number representing a certain date, in this case 5 seconds after the "epoch", which is January 1st, 1970 GMT on UNIX systems. An epoch date is the number of seconds that passed since the epoch (essentially also a form of a duration, but with a well defined base value).
localtime accepts an epoch number representing a date and then calculates that date, converts it to the local time zone, and provides the separate fields (something like the Jan 1, 1970, 1:00:05, that is 5 seconds after the epoch, converted to your timezone). Even if you use gmtime this would still not make sense, because you are talking about a duration, and not a date.
Use DateTime to subtract the two dates, play with duration sanely, and all that, or use simple modular arithmetics:
my ( $h, $m, $s ) = do { use integer; ( ($tdelta / (60 * 60)) % 24, ($
+tdelta / 60) % 60, $tdelta % 60 ) };
but note that this conversion is lossy.
You can also use Time::Duration::Object, which is convenient for formatting durations for users as '2 hours, 5 minutes' or '2 minutes, 10 seconds', that is - rounded to a certain number of units.
| [reply] [d/l] [select] |