From 56773eefc1c3d77718d263172e25232dd2f0e209 Mon Sep 17 00:00:00 2001 From: Graham Knop Date: Thu, 23 Jul 2009 22:04:27 +0000 Subject: [PATCH] fixed #10590: Session::DateTime->secondsToInterval doesn't allow 7 weeks --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Session/DateTime.pm | 46 +++++++++++----------------------- t/Session/DateTime.t | 22 ++++++++-------- 3 files changed, 28 insertions(+), 41 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 173240264..15e1dc799 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -1,4 +1,5 @@ 7.7.16 + - fixed #10590: Session::DateTime->secondsToInterval doesn't allow 7 weeks - Fix template not found diagnostics. - fixed #10665: Navigations with old templates still in 7.7.16 - fixed #10662: Manage Clipboard / delete item diff --git a/lib/WebGUI/Session/DateTime.pm b/lib/WebGUI/Session/DateTime.pm index 5e07d401e..cd7274429 100644 --- a/lib/WebGUI/Session/DateTime.pm +++ b/lib/WebGUI/Session/DateTime.pm @@ -821,39 +821,23 @@ The number of seconds in the interval. =cut sub secondsToInterval { - my $self = shift; - my $seconds = shift; + my $self = shift; + my $seconds = shift; my $i18n = WebGUI::International->new($self->session, 'WebGUI'); - my ($interval, $units); - if ($seconds >= 31536000) { - $interval = round($seconds/31536000); - $units = $i18n->get("703"); - } - elsif ($seconds >= 2592000) { - $interval = round($seconds/2592000); - $units = $i18n->get("702"); - } - elsif ($seconds >= 604800) { - $interval = round($seconds/604800); - $units = $i18n->get("701"); - } - elsif ($seconds >= 86400) { - $interval = round($seconds/86400); - $units = $i18n->get("700"); + my %units = ( + 31536000 => "703", # years + 2592000 => "702", # months + 604800 => "701", # weeks + 86400 => "700", # days + 3600 => "706", # hours + 60 => "705", # minutes + ); + for my $unit (sort { $b <=> $a } keys %units) { + if ($seconds % $unit == 0) { + return ($seconds / $unit, $i18n->get($units{$unit})); + } } - elsif ($seconds >= 3600) { - $interval = round($seconds/3600); - $units = $i18n->get("706"); - } - elsif ($seconds >= 60) { - $interval = round($seconds/60); - $units = $i18n->get("705"); - } - else { - $interval = $seconds; - $units = $i18n->get("704"); - } - return ($interval, $units); + return ($seconds, $i18n->get("704")); # seconds } #------------------------------------------------------------------- diff --git a/t/Session/DateTime.t b/t/Session/DateTime.t index 07feab021..5b20b1a25 100644 --- a/t/Session/DateTime.t +++ b/t/Session/DateTime.t @@ -17,7 +17,7 @@ use File::Spec; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 77; # increment this value for each test you create +use Test::More tests => 79; # increment this value for each test you create installBadLocale(); @@ -216,15 +216,17 @@ cmp_ok( # #################################################### -is(join(" ",$dt->secondsToInterval(60*60*24*365*2)), "2 Year(s)", "secondsToInterval(), years"); -is(join(" ",$dt->secondsToInterval(60*60*24*365*2.4)), "2 Year(s)", "secondsToInterval(), years, rounded down"); -is(join(" ",$dt->secondsToInterval(60*60*24*365*2.9)), "3 Year(s)", "secondsToInterval(), years, rounded up"); -is(join(" ",$dt->secondsToInterval(60*60*24*363)), "12 Month(s)", "secondsToInterval(), months"); -is(join(" ",$dt->secondsToInterval(60*60*24*7*3)), "3 Week(s)", "secondsToInterval(), weeks"); -is(join(" ",$dt->secondsToInterval(60*60*24*5)), "5 Day(s)", "secondsToInterval(), days"); -is(join(" ",$dt->secondsToInterval(60*60*18)), "18 Hour(s)", "secondsToInterval(), hours"); -is(join(" ",$dt->secondsToInterval(60*27)), "27 Minute(s)", "secondsToInterval(), minutes"); -is(join(" ",$dt->secondsToInterval(59)), "59 Second(s)", "secondsToInterval(), seconds"); +is(join(" ",$dt->secondsToInterval(60*60*24*365*2)), "2 Year(s)", "secondsToInterval(), years"); +is(join(" ",$dt->secondsToInterval(60*60*24*180)), "6 Month(s)", "secondsToInterval(), months"); +is(join(" ",$dt->secondsToInterval(60*60*24*7*3)), "3 Week(s)", "secondsToInterval(), weeks"); +is(join(" ",$dt->secondsToInterval(60*60*24*5)), "5 Day(s)", "secondsToInterval(), days"); +is(join(" ",$dt->secondsToInterval(60*60*24*8)), "8 Day(s)", "secondsToInterval(), days, longer than a week"); +is(join(" ",$dt->secondsToInterval(60*60*24*363)), "363 Day(s)", "secondsToInterval(), days, longer than a month"); +is(join(" ",$dt->secondsToInterval(60*60*24*365*2.4)), "876 Day(s)", "secondsToInterval(), days, longer than a year"); +is(join(" ",$dt->secondsToInterval(60*60*18)), "18 Hour(s)", "secondsToInterval(), hours"); +is(join(" ",$dt->secondsToInterval(60*60*24*365*2.9)), "25404 Hour(s)", "secondsToInterval(), hours, longer than a year"); +is(join(" ",$dt->secondsToInterval(60*27)), "27 Minute(s)", "secondsToInterval(), minutes"); +is(join(" ",$dt->secondsToInterval(59)), "59 Second(s)", "secondsToInterval(), seconds"); #################################################### #