aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/Utils.pm
diff options
context:
space:
mode:
authorDave Arter <davea@mysociety.org>2017-02-15 13:41:38 +0000
committerDave Arter <davea@mysociety.org>2017-02-15 13:41:38 +0000
commit54af489f0fe985dfc433f0b8a3ab226a470a6023 (patch)
tree29a0a89f65016cd0fc4900ea7491da67ddd2389a /perllib/Utils.pm
parente198a5b8ba63fb1bae68132d2a81fd6cd4ecf69a (diff)
parent8e6f6a1818b4f998d48f157387d2314bb8c86f8a (diff)
Merge branch 'issues/forcouncils/51-close-old-reports'
Diffstat (limited to 'perllib/Utils.pm')
-rw-r--r--perllib/Utils.pm33
1 files changed, 30 insertions, 3 deletions
diff --git a/perllib/Utils.pm b/perllib/Utils.pm
index 7dd2a3f39..6ba20e9d3 100644
--- a/perllib/Utils.pm
+++ b/perllib/Utils.pm
@@ -99,7 +99,7 @@ sub truncate_coordinate {
Strip leading and trailing white space from a string. Also reduces all
white space to a single space.
-Trim
+Trim
=cut
@@ -195,7 +195,28 @@ sub prettify_dt {
# argument is duration in seconds, rounds to the nearest minute
sub prettify_duration {
my ($s, $nearest) = @_;
- if ($nearest eq 'week') {
+
+ unless ( defined $nearest ) {
+ if ($s < 3600) {
+ $nearest = 'minute';
+ } elsif ($s < 3600*24) {
+ $nearest = 'hour';
+ } elsif ($s < 3600*24*7) {
+ $nearest = 'day';
+ } elsif ($s < 3600*24*7*4) {
+ $nearest = 'week';
+ } elsif ($s < 3600*24*7*4*12) {
+ $nearest = 'month';
+ } else {
+ $nearest = 'year';
+ }
+ }
+
+ if ($nearest eq 'year') {
+ $s = int(($s+60*60*24*3.5)/60/60/24/7/4/12)*60*60*24*7*4*12;
+ } elsif ($nearest eq 'month') {
+ $s = int(($s+60*60*24*3.5)/60/60/24/7/4)*60*60*24*7*4;
+ } elsif ($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;
@@ -206,6 +227,8 @@ sub prettify_duration {
return _('less than a minute') if $s == 0;
}
my @out = ();
+ _part(\$s, 60*60*24*7*4*12, \@out);
+ _part(\$s, 60*60*24*7*4, \@out);
_part(\$s, 60*60*24*7, \@out);
_part(\$s, 60*60*24, \@out);
_part(\$s, 60*60, \@out);
@@ -217,7 +240,11 @@ sub _part {
if ($$s >= $m) {
my $i = int($$s / $m);
my $str;
- if ($m == 60*60*24*7) {
+ if ($m == 60*60*24*7*4*12) {
+ $str = mySociety::Locale::nget("%d year", "%d years", $i);
+ } elsif ($m == 60*60*24*7*4) {
+ $str = mySociety::Locale::nget("%d month", "%d months", $i);
+ } elsif ($m == 60*60*24*7) {
$str = mySociety::Locale::nget("%d week", "%d weeks", $i);
} elsif ($m == 60*60*24) {
$str = mySociety::Locale::nget("%d day", "%d days", $i);