File: logontime

package info (click to toggle)
hxtools 20231224-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,640 kB
  • sloc: ansic: 4,825; perl: 3,463; sh: 1,629; cpp: 353; makefile: 149
file content (44 lines) | stat: -rwxr-xr-x 998 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/perl
# SPDX-License-Identifier: MIT
#
#	longontime - simple wtmp analyzer
#	written by Jan Engelhardt, 2004-2007

use Getopt::Long;
use strict;

my %Total;
my $ARG_time;

&Getopt::Long::Configure(qw(bundling));
&GetOptions("t" => \$ARG_time);

open(IN, "last -ax |");
while (defined(my $l = <IN>)) {
	chomp $l;
	my($user, $dur) = ($l =~ /^(\w+)\s+.+?\((.*?)\)/);
	if ($user eq "") {
		next;
        }

	my($h, $m) = split(/:/o, $dur);
	$Total{$user} += $h * 60 + $m;
}
close IN;

if ($ARG_time) {
	# Time sort
	foreach my $u (sort { $Total{$b} <=> $Total{$a} } keys %Total) {
		printf "%-8s: %3ud %2uh %2um (%5um)\n",
                      $u, int($Total{$u} / 1440), int($Total{$u} / 60 % 24),
                      int($Total{$u} % 60), $Total{$u};
	}
} else {
	# Alphabetical sort
	foreach my $u (sort keys %Total) {
		printf "%-8s: %3ud %2uh %2um (%5um)\n",
		       $u, int($Total{$u} / 86400),
		       int($Total{$u} / 60 % 86400), int($Total{$u} % 60),
		       $Total{$u};
	}
}