aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Utils.pm
diff options
context:
space:
mode:
authorMatthew Somerville <matthew@mysociety.org>2011-06-07 17:26:01 +0100
committerMatthew Somerville <matthew@mysociety.org>2011-06-07 17:26:01 +0100
commitb5a218792a7bcf477c930205b6a7c341a90d3efd (patch)
tree0de28f29f7d1210258838ac0f9be4226f4aac627 /perllib/Utils.pm
parent891428159dd27e5648676b7b9b51c5a2fe2f83ed (diff)
Final bits of Page.pm gone.
Diffstat (limited to 'perllib/Utils.pm')
-rw-r--r--perllib/Utils.pm50
1 files changed, 50 insertions, 0 deletions
diff --git a/perllib/Utils.pm b/perllib/Utils.pm
index c267bbea0..bf8ac2805 100644
--- a/perllib/Utils.pm
+++ b/perllib/Utils.pm
@@ -12,6 +12,8 @@
package Utils;
use strict;
+use Encode;
+use POSIX qw(strftime);
use mySociety::DBHandle qw(dbh);
use mySociety::GeoUtil;
use mySociety::Locale;
@@ -206,4 +208,52 @@ sub cleanup_text {
return $input;
}
+sub prettify_epoch {
+ my ($s, $short) = @_;
+ my @s = localtime($s);
+ my $tt = strftime('%H:%M', @s);
+ my @t = localtime();
+ if (strftime('%Y%m%d', @s) eq strftime('%Y%m%d', @t)) {
+ $tt = "$tt " . _('today');
+ } elsif (strftime('%Y %U', @s) eq strftime('%Y %U', @t)) {
+ $tt = "$tt, " . decode_utf8(strftime('%A', @s));
+ } elsif ($short) {
+ $tt = "$tt, " . decode_utf8(strftime('%e %b %Y', @s));
+ } elsif (strftime('%Y', @s) eq strftime('%Y', @t)) {
+ $tt = "$tt, " . decode_utf8(strftime('%A %e %B %Y', @s));
+ } else {
+ $tt = "$tt, " . decode_utf8(strftime('%a %e %B %Y', @s));
+ }
+ return $tt;
+}
+
+# argument is duration in seconds, rounds to the nearest minute
+sub prettify_duration {
+ my ($s, $nearest) = @_;
+ if ($nearest eq 'week') {
+ $s = int(($s+60*60*24*3.5)/60/60/24/7)*60*60*24*7;
+ } elsif ($nearest eq 'day') {
+ $s = int(($s+60*60*12)/60/60/24)*60*60*24;
+ } elsif ($nearest eq 'hour') {
+ $s = int(($s+60*30)/60/60)*60*60;
+ } elsif ($nearest eq 'minute') {
+ $s = int(($s+30)/60)*60;
+ return _('less than a minute') if $s == 0;
+ }
+ my @out = ();
+ _part(\$s, 60*60*24*7, _('%d week'), _('%d weeks'), \@out);
+ _part(\$s, 60*60*24, _('%d day'), _('%d days'), \@out);
+ _part(\$s, 60*60, _('%d hour'), _('%d hours'), \@out);
+ _part(\$s, 60, _('%d minute'), _('%d minutes'), \@out);
+ return join(', ', @out);
+}
+sub _part {
+ my ($s, $m, $w1, $w2, $o) = @_;
+ if ($$s >= $m) {
+ my $i = int($$s / $m);
+ push @$o, sprintf(mySociety::Locale::nget($w1, $w2, $i), $i);
+ $$s -= $i * $m;
+ }
+}
+
1;