summaryrefslogtreecommitdiff
path: root/search/lib/Common.pm
blob: 07979437668bc1d3f84183a3622721031595fdee (plain)
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
45
46
## utility functions
package Common;
use base 'Exporter';
our @EXPORT = qw[is_number is_string show_date get_age trim strip_html];

# promiscuous namespace
sub is_number
    { my ($self, $s) = @_; return $s && $s !~ /\D/; }
sub is_string
    { my ($self, $s) = @_; return length $s && ! length ref $s; }
sub show_date
    { my ($self, $date) = @_; return scalar localtime($date); }
sub get_age
    {
    my ($self, $time) = @_;
	if ($time < 0)
		{ return "old"; }
    my $age = time - $time;
    if ($age < 60)
        { return int($age)."s"; }
    $age /= 60;
    if ($age < 60)
        { return int($age)."m"; }
    my $mins = $age % 60;
    $age /= 60;
    if ($age < 2)
        { return int($age)."h".int($mins)."m"; }
    elsif ($age < 24)
        { return int($age)."h"; }
    $age /= 24;
    if ($age < 10000)
		{ return int($age)."d"; }
	my $powers = qw[k m g t p e z y];
	foreach my $prefix (@$powers)
		{
		$age /= 1000;
    	if ($age < 10000)
			{ return int($age).$prefix."d"; }
		}
    }
sub trim
    { my ($self, $s) = @_; $s =~ s/^\s+//; $s =~ s/\s+$//; return $s; }
sub strip_html
    { my ($self, $s) = @_; $s =~ s/[<>]+/ /g; return $s; }
 
1;