Re: Difference in times (in minutes)
by Velaki (Chaplain) on Dec 13, 2004 at 17:06 UTC
|
Even if you extract the hours, minutes, and seconds; you'll still need to convert the hours (you might need to "borrow" 1 from the hours), and convert the seconds to fractional minutes.
In this case, I would simply convert the string to second, subtract, and convert back.
If you are timing something, though, you may wish to look into the various timing modules, or (poor man's method) capture the value of time() at different points in the program, and convert the difference to minutes at the end.
Hope that helped,
-v
"Perl. There is no substitute."
| [reply] [d/l] |
Re: Difference in times (in minutes)
by davorg (Chancellor) on Dec 13, 2004 at 17:07 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
my $end_time = "16:54:28";
my $start_time = "16:54:23";
my @start = split /:/, $start_time;
my @end = split /:/, $end_time;
my @diff;
$diff[$_] = $end[$_] - $start[$_] for 0 .. $#start;
if ($diff[2] < 0) {
$diff[1]--;
$diff[2] += 60;
}
if ($diff[1] < 0) {
$diff[0]--;
$diff[1] += 60;
}
print "@diff\n";
This ends with @diff containing the difference in hours, minutes and seconds. You can format that in any way you want.
--
< http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] [d/l] |
Re: Difference in times (in minutes)
by zejames (Hermit) on Dec 13, 2004 at 20:52 UTC
|
use DateTime;
$dt1 = DateTime->new(year => 2004,
hour => 16,
minute => 54,
second => 23);
$dt2 = DateTime->new(year => 2004,
hour => 16,
minute => 54,
second => 28);
# Beware : $diff is a DateTime::Duration object
# (which is logical if one think about it...)
$diff = $dt2 - $dt1;
print $diff->hours, ":", $diff->minutes, ":", $diff->seconds, "\n";
HTH
| [reply] [d/l] |
Re: Difference in times (in minutes)
by insensate (Hermit) on Dec 13, 2004 at 17:56 UTC
|
use Date::Calc qw(Delta_DHMS);
$start_time = "16:54:23";
$end_time = "16:54:28";
@random=(2001,1,1); #Any value to allay the ymd filler
push @end, @random, split/:/, $end_time;
push @start, @random, split/:/, $start_time;
@diff=Delta_DHMS(@start,@end);
print join":",@diff[1..3];
| [reply] [d/l] |
Re: Difference in times (in minutes)
by Joost (Canon) on Dec 13, 2004 at 17:11 UTC
|
What is the answer of that sum in minutes? 0 ? 1 ? 0.0something?
Are you sure you never have to handle saving-time transitions or leap-seconds, or just plain 00:10 :01 - 23:48:30 ? If you need to handle any of it, you'd probably be better off using full dates and a good date module (like DateTime)
| [reply] |
Re: Difference in times (in minutes)
by TomDLux (Vicar) on Dec 13, 2004 at 17:51 UTC
|
Concert to seconds and subtract.
It's a one-liner (or two or three) if you don't count
the module you use.
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] |
Re: Difference in times (in minutes)
by ysth (Canon) on Dec 13, 2004 at 21:36 UTC
|
Not clear on what you mean by "in minutes"...round up or down or use
floating point? Or did you actually mean seconds? Anyway, this does
floating point, in what I hope is an educational (or even amusing)
way:
$end_time = shift || "16:54:28";
$start_time = shift || "16:54:23";
if (($end_time =~ /(\d+)/)[0] < ($start_time =~ /(\d+)/)[0]
|| ($end_time =~ /(\d+)/)[0] == ($start_time =~ /(\d+)/)[0]
&& (($end_time =~ /(\d+)/g)[1] < ($start_time =~ /(\d+)/g)[1]
|| ($end_time =~ /(\d+)/g)[1] == ($start_time =~ /(\d+)/g)[1]
&& ($end_time =~ /(\d+)/g)[2] < ($start_time =~ /(\d+)/g)[2])) {
$end_time =~ s/(\d+)/24+$1/e;
}
use List::Util "sum";
$mindiff = sum(map( 60**(2-$_)*(($end_time =~ /(\d+)/g)[$_]-($start_ti
+me =~ /(\d+)/g)[$_]), 0..2 ))/60;
print $mindiff, "minutes (", $mindiff*60, " seconds)\n";
But if these are really local times, you are going to have trouble over dst boundaries (and some numbers will be ambiguous). | [reply] [d/l] |
|
|
Thanks for all the responses.
Only on PM one can get such a wonderful support.
| [reply] |