Re: getting a time diff from strings



On Wed, Dec 28, 2005 at 09:18:55PM -0500, Robert wrote:
From: Robert <sigzero@xxxxxxxxx>
To: beginners@xxxxxxxx
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

.



Relevant Pages

  • Re: Verbose functional languages?
    ... whereas a memory leak due to too much laziness in the wrong place can be. ... If you declare your data structure elements as strict, ... I think size matters, for the optimization heuristics. ... strings for symbol names. ...
    (comp.lang.functional)
  • RE: question related to readdir function
    ... perl is not splitting your file name into ... different strings. ... It is just reading all the file names in that directory. ... > use strict; ...
    (perl.beginners)
  • Re: Help - Counting text - Associative Array?
    ... > use strict; ... Unfortunately, the strings are not ... Your code shows me the logic and syntax, and I will study it to make ... in the array of my script it should work, ...
    (comp.lang.perl)
  • Re: Filehandles Referenced with a Variable
    ... > Those are strings, not typeglobs. ... I'm back to where I need to remove the 'use strict'. ... print OUT "$num records will be written $tot times.\n"; ...
    (comp.lang.perl.misc)
  • Re: need help with prog. logic
    ... You should always enable warnings and strict when developing Perl code. ... Dots are not special in strings, so there is no need to backslash them. ... That is testing for numerical equality. ...
    (comp.lang.perl.misc)