On Wed, Dec 28, 2005 at 09:18:55PM -0500, Robert wrote:
From: Robert <[EMAIL PROTECTED]>
To: beginners@perl.org
Subject: getting a time diff from strings


I have a log that I am parsing and I can get the login and logout time string parsed out. It looks like this:

13:50:01    # this is the when the user logged in
14:14:35    # this is when the user logged out

I need to get how much time that is but I am not sure how to go about it since it is a string (that I assume I have to convert to "real" time to do the math with).



#!/usr/bin/perl
#timelogged.pl

use warnings;
use strict;
use POSIX;

use constant  Hs => 3600;
use constant  Hmn => 60;

my $login = '13:50:01';
my $logout = '14:14:35';
my @Login = split ':', $login;
my @Logout = split ':', $logout;

my $sumin = $Login[0]*Hs + $Login[1]*Hmn + $Login[2];
my $sumout = $Logout[0]*Hs + $Logout[1]*Hmn + $Logout[2];

my $diff = $sumout - $sumin;

my $hours = floor($diff/Hs);

my $rest1 = $diff % Hs;
my $mn = floor($rest1/Hmn);
my $sec = $rest1 % Hmn;

print "Time spends : $hours h $mn mn $sec sec\n";

__END__

hth if you don't have the Time::Piece module
You can make subroutines ....

--
Gérard


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to