We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Perl CGI time calculation

Medium Priority
449 Views
Last Modified: 2013-12-25
Any help would be appreciated.

I am displaying a summary of a user on the system using the 'last' command in the below format.
I am having difficulty with the 'Running Total' column where it keeps track of a running total
of the time logged in.


Login #            Date/Time                        Duration              Running Total
1         Mon Apr 11 18:24 - 18:27          00:03              Days, 0 hours, and 03 minutes
2         Sun Apr 10 18:42 - 21:31          02:49              Days, 2 hours, and 52 minutes
3         Sat Apr 9 14:19 - 14:33            00:13               Days, 3 hours, and  5 minutes
4         Wed Apr 6 21:58 - 22:26           00:28              Days, 3 hours, and 33 minutes
5          Wed Apr 6 20:18 - 21:56          01:38              Days, 4 hours, and 11 minutes
my @list = (`last | less`)

    Tr ( td({-style => 'width: 10%'},("Login #")), td({-style => 'width: 30%'},("Date/Time")),  td({-style => 'width: 10%'},("Duration")), td({-style => 'width: 50%'},("Running Total")) )
);

if ( my @input = grep /^\s*$user\b/, @list)
  {
       for ( my $i = 1; $i <= $#input; $i++ )
        {
 
   $input[$i] =~ /(\S+)\s+pts\/\S+\s+\S+\s+(\S+\s+\S+\s+\d+\s+\d+\:\d+\s+\S*\s+\S*\s*\w*)\(*(\d*)\:*(\d*)\)*/g;

print table
        (
                {-border=>0, width=>"55%"},
 Tr ( td({-style => 'width: 10%'},($i)), td({-style => 'width: 30%'},($2)),  td({-style => 'width: 10%'},($3.":".$4)) ,  td({-style => 'width: 50%'},( $0."Days, ".$0." hours, and ".0." minutes")))
        );
   }

Open in new window

Comment
Watch Question

CERTIFIED EXPERT

Commented:
Can you post the output of your 'last' command?  It does not appear to match what last outputs on my system (or else the "$input[$i] =~ /.../" has issues).

Author

Commented:
Running 'last|less' on my system:

nrisner  pts/0         81.213.156.102   Wed Apr 20 04:59 - 05:03  (00:04)
nrisner  pts/0         81.213.156.102   Wed Apr 20 04:44 - 04:49  (00:05)
bessler  pts/1        cm-204-193-202-2 Wed Apr 20 02:20 - 02:58  (00:37)
bessler  pts/1        cm-204-193-202-2 Wed Apr 20 01:29 - 01:59  (00:29)
kchin    pts/4          ip24-252-49-142. Wed Apr 20 00:36 - 00:39  (00:03)
kkrejci  pts/4           ip72-206-107-124 Wed Apr 20 00:25 - 00:30  (00:04)
nleonard pts/0        ip174-71-94-187. Tue Apr 19 23:59 - 02:37  (02:37)

I am back referencing for user ($1), date/time ($2), and broke up hour($3) & minute ($4)

 $input[$i] =~ /(\S+)\s+pts\/\S+\s+\S+\s+(\S+\s+\S+\s+\d+\s+\d+\:\d+\s+\S*\s+\S*\s*\w*)\(*(\d*)\:*(\d*)\)*/g;
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks again!!
Working perfect so far
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.