diff options
-rw-r--r-- | perllib/Utils.pm | 33 | ||||
-rw-r--r-- | t/utils.t | 8 |
2 files changed, 38 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); @@ -91,10 +91,18 @@ is Utils::prettify_dt($dt, 1), $dt->strftime("%H:%M, %e %b %Y"); $dt = DateTime->now->subtract(days => 400); is Utils::prettify_dt($dt), $dt->strftime("%H:%M, %a %e %B %Y"); +is Utils::prettify_duration(12*5*7*86400+3600+60+1, 'year'), '1 year'; +is Utils::prettify_duration(25*5*7*86400+3600+60+1, 'year'), '2 years'; +is Utils::prettify_duration(5*7*86400+3600+60+1, 'month'), '1 month'; is Utils::prettify_duration(7*86400+3600+60+1, 'week'), '1 week'; is Utils::prettify_duration(86400+3600+60+1, 'day'), '1 day'; is Utils::prettify_duration(86400+3600+60+1, 'hour'), '1 day, 1 hour'; is Utils::prettify_duration(86400+3600+60+1, 'minute'), '1 day, 1 hour, 1 minute'; is Utils::prettify_duration(20, 'minute'), 'less than a minute'; +# prettify_duration should choose a $nearest sensibly if it's not given +is Utils::prettify_duration(12*5*7*86400+3600+60+1), '1 year'; +is Utils::prettify_duration(7*86400+3600+60+1), '1 week'; +is Utils::prettify_duration(14*86400+3600+60+1), '2 weeks'; +is Utils::prettify_duration(1800), '30 minutes'; done_testing(); |