summaryrefslogtreecommitdiff
path: root/bucky2/lib/Common.pm
diff options
context:
space:
mode:
authorJules Laplace <carbon@melanarchy.org>2013-08-02 17:23:25 -0500
committerJules Laplace <carbon@melanarchy.org>2013-08-02 17:23:25 -0500
commite76b691e78e273226cba9284cb8cd22a423319ed (patch)
treea58d22f69869fe2bf3885f81bdda4952f87ff6d7 /bucky2/lib/Common.pm
parent753f60c7d4769fa72d3b910e491f37db6f130898 (diff)
bucky2
Diffstat (limited to 'bucky2/lib/Common.pm')
-rw-r--r--bucky2/lib/Common.pm46
1 files changed, 46 insertions, 0 deletions
diff --git a/bucky2/lib/Common.pm b/bucky2/lib/Common.pm
new file mode 100644
index 0000000..0797943
--- /dev/null
+++ b/bucky2/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;