Home > Archive > PERL Beginners > January 2006 > getting a time diff from strings
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
getting a time diff from strings
|
|
| Robert 2006-01-10, 4:01 am |
| 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).
Robert
| |
| Todd W 2006-01-10, 4:01 am |
|
""Robert"" <sigzero@gmail.com> wrote in message
news:20051229021856.12068.qmail@lists.develooper.com...
> 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).
>
> Robert
>
I would make some demands that the full date get put in the log, otherwise
when a session spans a change of dates your reports are going to be
incomplete.
Otherwise here is how I would get started:
use warnings;
use strict;
use Time::Piece;
my $login = Time::Piece->strptime('13:50:01', '%T');
my $logout = Time::Piece->strptime('14:14:35', '%T');
# $diff is a Time::Seconds object
my $diff = $logout - $login;
print('Logged in for: ', $diff->seconds, " seconds\n");
Enjoy,
trwww
| |
| Timothy Johnson 2006-01-10, 4:01 am |
|
Check out the Time::Local module.
-----Original Message-----
From: Robert [mailto:sigzero@gmail.com]=20
Sent: Wednesday, December 28, 2005 6:19 PM
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=20
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=20
the math with).
Robert=20
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Gerard Robin 2006-01-10, 4:01 am |
| On Wed, Dec 28, 2005 at 09:18:55PM -0500, Robert wrote:
>From: Robert <sigzero@gmail.com>
>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=20
>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=
=20
>since it is a string (that I assume I have to convert to "real" time to do=
=20
>the math with).
#!/usr/bin/perl
#timelogged.pl
use warnings;
use strict;
use POSIX;
use constant Hs =3D> 3600;
use constant Hmn =3D> 60;
my $login =3D '13:50:01';
my $logout =3D '14:14:35';
my @Login =3D split ':', $login;
my @Logout =3D split ':', $logout;
my $sumin =3D $Login[0]*Hs + $Login[1]*Hmn + $Login[2];
my $sumout =3D $Logout[0]*Hs + $Logout[1]*Hmn + $Logout[2];
my $diff =3D $sumout - $sumin;
my $hours =3D floor($diff/Hs);
my $rest1 =3D $diff % Hs;
my $mn =3D floor($rest1/Hmn);
my $sec =3D $rest1 % Hmn;
print "Time spends : $hours h $mn mn $sec sec\n";
__END__
hth if you don't have the Time::Piece module=20
You can make subroutines ....
=20
--=20
G=E9rard
| |
| Robert Hicks 2006-01-10, 4:01 am |
| Thanks!
Robert
|
|
|
|
|