From 3a4f027ec05aa5fdf4098ceb0dab09f69c5e0b8b Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 8 Dec 2017 01:34:52 +0100 Subject: adding perl search index builder --- search/lib/Common.pm | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 search/lib/Common.pm (limited to 'search/lib/Common.pm') diff --git a/search/lib/Common.pm b/search/lib/Common.pm new file mode 100644 index 0000000..0797943 --- /dev/null +++ b/search/lib/Common.pm @@ -0,0 +1,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; -- cgit v1.2.3-70-g09d2