From 4a4ea97e411902576d07d5a5bae97c55bda7c7bb Mon Sep 17 00:00:00 2001 From: Yorhel Date: Wed, 17 Dec 2008 14:02:53 +0100 Subject: Moved age() to VNDB::Func --- lib/VNDB/Func.pm | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'lib/VNDB/Func.pm') 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) -- cgit v1.2.3