diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-12-08 01:34:52 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-12-08 01:34:52 +0100 |
| commit | 3a4f027ec05aa5fdf4098ceb0dab09f69c5e0b8b (patch) | |
| tree | 8a0c5bebff6a40e77bda8b02142d99a7c448545e /search/lib/Common.pm | |
| parent | 340c3080b38518976c5c833399d8e07a7fc561bf (diff) | |
adding perl search index builder
Diffstat (limited to 'search/lib/Common.pm')
| -rw-r--r-- | search/lib/Common.pm | 46 |
1 files changed, 46 insertions, 0 deletions
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; |
