summaryrefslogtreecommitdiff
path: root/lib/VNDB/Func.pm
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2008-12-17 14:02:53 +0100
committerYorhel <git@yorhel.nl>2008-12-17 14:02:53 +0100
commit4a4ea97e411902576d07d5a5bae97c55bda7c7bb (patch)
tree6ebac7b58d19cd3b0720fe33a2be392c8f276e91 /lib/VNDB/Func.pm
parent32771b6b1799be558447fb19a0bcf3a79345f832 (diff)
Moved age() to VNDB::Func
Diffstat (limited to 'lib/VNDB/Func.pm')
-rw-r--r--lib/VNDB/Func.pm17
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/VNDB/Func.pm b/lib/VNDB/Func.pm
index 37bec8be..9abacad0 100644
--- a/lib/VNDB/Func.pm
+++ b/lib/VNDB/Func.pm
@@ -6,7 +6,7 @@ use warnings;
use YAWF ':html';
use Exporter 'import';
use POSIX 'strftime';
-our @EXPORT = qw| shorten date datestr monthstr userstr bb2html gtintype liststat clearfloat cssicon |;
+our @EXPORT = qw| shorten age date datestr monthstr userstr bb2html gtintype liststat clearfloat cssicon |;
# I would've done this as a #define if this was C...
@@ -16,6 +16,21 @@ sub shorten {
}
+# Argument: unix timestamp
+# Returns: age
+sub age {
+ my $a = time-$_[0];
+ return sprintf '%d %s ago',
+ $a > 60*60*24*365*2 ? ( $a/60/60/24/365, 'years' ) :
+ $a > 60*60*24*(365/12)*2 ? ( $a/60/60/24/(365/12), 'months' ) :
+ $a > 60*60*24*7*2 ? ( $a/60/60/24/7, 'weeks' ) :
+ $a > 60*60*24*2 ? ( $a/60/60/24, 'days' ) :
+ $a > 60*60*2 ? ( $a/60/60, 'hours' ) :
+ $a > 60*2 ? ( $a/60, 'min' ) :
+ ( $a, 'sec' );
+}
+
+
# argument: unix timestamp and optional format (compact/full)
# return value: yyyy-mm-dd
# (maybe an idea to use cgit-style ages for recent timestamps)