Operating System - HP-UX
1754385 Members
3158 Online
108813 Solutions
New Discussion юеВ

Script to calculate duration

 
SOLVED
Go to solution
Ermin Borovac
Honored Contributor

Re: Script to calculate duration

Oops, need to specially adjust 12pm and 12am. Attached is the modified script.
Hein van den Heuvel
Honored Contributor

Re: Script to calculate duration


The perl below shouls be close... but you would need to verify the AM/PM handling.
As a european I never managed to figure that one out. for PM you add 12 right? 10:00 PM is really 22:00.
And 12:30 pm = 24:30 right?
Hmmm, how can that be, a day has only 24 hours!?
Is this 5-Jan 12:30PM really 6-Jan 00:30?

:-) :-)

--------------------------- try this ----


use Time::Local;

sub my_seconds {
# Jan 2 2005 7:45:57:206PM
my ($time)=@_;
my $t, $hour, $mon;
if ($time =~ /(\w+)\s+(\d+)\s+(\d+) (\d+):(\d+):(\d+):(\d+)(AM|PM)/) {
$hour = $4;
if ($8 eq "PM") { if ($hour == 12) { $hour=0 } else {$hour += 12}}
$mon = index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)/3;
$t = timelocal($6,$5,$hour,$2,$mon,$3-1900) + $7/1000;
}
return $t;
}

while (<>) {
chop;
($t1,$t2) = split (/\|/);
printf ("%s -- %s -- %9.3f\n", $t1, $t2, my_seconds($t2) - my_seconds($t1));
}


Stephen Keane
Honored Contributor

Re: Script to calculate duration

12:30 AM is 30 minutes past midnight
12:30 PM is 30 minutes past noon

So on a 24-hour clock

12:30 AM is 00:30

12:30 PM is 12:30


H.Merijn Brand (procura
Honored Contributor

Re: Script to calculate duration

Use what's appropriate for it. There's agood tool for everything:

# echo "Jan 2 2005 7:45:57:206PM|Jan 2 2005 8:20:24:583PM" | perl -MDate::Manip -lne'print DateCalc ((map { ParseDate ($_) } split m/\|/ => $_),\$err)'
+0:0:0:0:0:34:27


Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
H.Merijn Brand (procura
Honored Contributor

Re: Script to calculate duration

lt09:/home/merijn 116 > cat xx.txt
Jan 5 2005 12:39:48:890AM|Jan 5 2005 1:15:17:173AM
Jan 6 2005 11:58:58:303AM|Jan 6 2005 12:16:39:466PM
Jan 9 2005 12:56:18:443PM|Jan 9 2005 1:30:49:986PM
lt09:/home/merijn 117 > perl -MDate::Manip -lpe'$_.="|".DateCalc ((map { ParseDate ($_) } split m/\|/ => $_),\$err)' xx.txt
Jan 5 2005 12:39:48:890AM|Jan 5 2005 1:15:17:173AM|+0:0:0:0:0:35:29
Jan 6 2005 11:58:58:303AM|Jan 6 2005 12:16:39:466PM|+0:0:0:0:0:17:41
Jan 9 2005 12:56:18:443PM|Jan 9 2005 1:30:49:986PM|+0:0:0:0:0:34:31
lt09:/home/merijn 118 >

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Hein van den Heuvel
Honored Contributor

Re: Script to calculate duration


Ouch, in my feeble attempt to crack a joke I managed to actually confuse myself on the AM/PM thing! Thanks Stephen, for treating me kindly.

---------------------------

use Time::Local;

sub my_seconds {
# Jan 2 2005 7:45:57:206PM
my ($time)=@_;
my $t, $hour, $mon;
if ($time =~ /(\w+)\s+(\d+)\s+(\d+) (\d+):(\d+):(\d+):(\d+)(AM|PM)/) {
$hour = $4;
$hour = 0 if ($hour == 12 && $8 eq "AM");
$hour += 12 if ($hour != 12 && $8 eq "PM");
$mon = index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)/3;
$t = timelocal($6,$5,$hour,$2,$mon,$3-1900) + $7/1000;
}
return $t;
}

while (<>) {
chop;
($t1,$t2) = split (/\|/);
printf ("%s -- %s %9.3f\n", $t1, $t2, my_seconds($t2) - my_seconds($t1));
}
Ahmad Munawwar
Frequent Advisor

Re: Script to calculate duration

Hi Guys,

Thanks a lot for the invaluable input.

Finally I got it work..modify lil bit to get :-)

use Time::Local;
#use Time::localtime;

(my $prog = $0) =~ s|.*/||;

my %mnames = (
Jan => 1, Feb => 2, Mar => 3, Apr => 4, May => 5, Jun => 6,
Jul => 7, Aug => 8, Sep => 9, Oct => 10, Nov => 11, Dec => 12,
);

my ($stime, $etime);
while (<>) {
chomp;
next unless /\d+/;
s/\r//;
($stime, $etime) = (split(/\|/, $_))[3,4];
printf "$_|%.3lf\n", unixtime($etime) - unixtime($stime);
}

# Time in: Jan 2 2005 8:19:24:503PM
#

sub unixtime {
my $t = shift;
return
unless $t =~ m/^(...)\s+(\d+)\s+(\d+)\s+(\d+):(\d+):(\d+):(\d\d\d)(..)/;
my $mon = $mnames{$1};
my $day = $2;
my $year = $3;
my $hour = $4;
my $min = $5;
my $sec = $6;
my $mili = $7;
my $am = $8;
$hour += 12 if $am eq 'PM';

return timelocal($sec, $min, $hour, $day, $mon-1, $year - 1900)
+ $mili/1000.0;
}


OK..time for giving the points to you.

/munawar
Mark Ellzey
Valued Contributor

Re: Script to calculate duration

Munawar,

If you are using POSIX shell, you can use the SECONDS shell variable. This is the number of seconds since you logged in. Just do:
START=$SECONDS



FINISH=$SECONDS

ELAPSED=$(expr $FINISH - $START)

Regards,
Mark