From 76bf1b2ad55464d40c98244810166a4b444366b9 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 10 Apr 2007 04:51:48 +0000 Subject: [PATCH] testing for DateTime methods for interval translation and date math. Reformatted some of Session/DateTime for WGBP --- lib/WebGUI/Session/DateTime.pm | 70 ++++++++++++++++++------------- t/Session/DateTime.t | 76 +++++++++++++++++++++++++++++++--- 2 files changed, 112 insertions(+), 34 deletions(-) diff --git a/lib/WebGUI/Session/DateTime.pm b/lib/WebGUI/Session/DateTime.pm index 3f7bff0d8..b5d48d616 100644 --- a/lib/WebGUI/Session/DateTime.pm +++ b/lib/WebGUI/Session/DateTime.pm @@ -674,18 +674,24 @@ sub intervalToSeconds { my $units = shift; if ($units eq "years") { return ($interval*31536000); - } elsif ($units eq "months") { + } + elsif ($units eq "months") { return ($interval*2592000); - } elsif ($units eq "weeks") { - return ($interval*604800); - } elsif ($units eq "days") { - return ($interval*86400); - } elsif ($units eq "hours") { - return ($interval*3600); - } elsif ($units eq "minutes") { - return ($interval*60); - } else { - return $interval; + } + elsif ($units eq "weeks") { + return ($interval*604800); + } + elsif ($units eq "days") { + return ($interval*86400); + } + elsif ($units eq "hours") { + return ($interval*3600); + } + elsif ($units eq "minutes") { + return ($interval*60); + } + else { + return $interval; } } @@ -824,24 +830,30 @@ sub secondsToInterval { if ($seconds >= 31536000) { $interval = round($seconds/31536000); $units = "years"; - } elsif ($seconds >= 2592000) { - $interval = round($seconds/2592000); - $units = "months"; - } elsif ($seconds >= 604800) { - $interval = round($seconds/604800); - $units = "weeks"; - } elsif ($seconds >= 86400) { - $interval = round($seconds/86400); - $units = "days"; - } elsif ($seconds >= 3600) { - $interval = round($seconds/3600); - $units = "hours"; - } elsif ($seconds >= 60) { - $interval = round($seconds/60); - $units = "minutes"; - } else { - $interval = $seconds; - $units = "seconds"; + } + elsif ($seconds >= 2592000) { + $interval = round($seconds/2592000); + $units = "months"; + } + elsif ($seconds >= 604800) { + $interval = round($seconds/604800); + $units = "weeks"; + } + elsif ($seconds >= 86400) { + $interval = round($seconds/86400); + $units = "days"; + } + elsif ($seconds >= 3600) { + $interval = round($seconds/3600); + $units = "hours"; + } + elsif ($seconds >= 60) { + $interval = round($seconds/60); + $units = "minutes"; + } + else { + $interval = $seconds; + $units = "seconds"; } return ($interval, $units); } diff --git a/t/Session/DateTime.t b/t/Session/DateTime.t index 66f1eae3f..a04aa7b7d 100644 --- a/t/Session/DateTime.t +++ b/t/Session/DateTime.t @@ -15,15 +15,12 @@ use lib "$FindBin::Bin/../lib"; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 49; # increment this value for each test you create +use Test::More tests => 69; # increment this value for each test you create my $session = WebGUI::Test->session; my $wgbday = 997966800; my $bdayCopy = $wgbday; -ok($session->datetime->addToDate($wgbday,1,2,3) >= $wgbday+1*60*60*24*365+2*60*60*24*28+3*60*60*24, "addToDate()"); -ok($session->datetime->addToTime($wgbday,1,2,3) >= $wgbday+1*60*60+2*60+3, "addToTime()"); -ok($session->datetime->addToDateTime($wgbday,1,2,3,4,5,6) >= $wgbday+1*60*60*24*365+2*60*60*24*28+3*60*60*24+4*60*60+5*60+6, "addToDateTime()"); my ($start, $end) = $session->datetime->dayStartEnd($wgbday); ok($end-$start >= 60*60*23, "dayStartEnd()"); is($session->datetime->epochToHuman($wgbday,"%y"), "2001", "epochToHuman() - year"); @@ -48,7 +45,6 @@ is($session->datetime->getMonthName(0), undef, "getMonthName returns undef i is($session->datetime->getMonthName(25), undef, "getMonthName returns undef if too high"); is($session->datetime->getSecondsFromEpoch($wgbday), 60*60*8, "getSecondsFromEpoch()"); is($session->datetime->humanToEpoch("2001-08-16 08:00:00"), $wgbday, "humanToEpoch()"); -is($session->datetime->intervalToSeconds(2,"weeks"),60*60*24*14, "intervalToSeconds()"); is(join("-",$session->datetime->localtime($wgbday)),'2001-8-16-8-0-0-228-4-1', "localtime()"); is($session->datetime->monthCount($wgbday,$wgbday+60*60*24*365), 12, "monthCount()"); my ($start, $end) = $session->datetime->monthStartEnd($wgbday); @@ -148,6 +144,76 @@ my $dt_tzs = DateTime::TimeZone::all_names; my $wg_tzs = $session->datetime->getTimeZones(); is (scalar @{ $dt_tzs }, scalar keys %{ $wg_tzs }, 'getTimeZones: correct number of time zones'); +my $timeZoneFormatFlag = 1; +foreach my $timeZone (keys %{ $wg_tzs } ) { + my $tzLabel = $wg_tzs->{$timeZone}; + my $tz; + ($tz = $timeZone) =~ tr/_/ /; + if ($tz ne $tzLabel) { + $timeZoneFormatFlag = 0; + } +} + +ok($timeZoneFormatFlag, 'getTimeZones: All time zones formatted correctly for reference and display'); + +#################################################### +# +# addToDate +# +#################################################### + +ok($session->datetime->addToDate($wgbday,1,2,3) >= $wgbday+1*60*60*24*365+2*60*60*24*28+3*60*60*24, "addToDate()"); +is($session->datetime->addToDate($wgbday), $wgbday, 'addToDate defaults to adding 0'); + +#################################################### +# +# addToTime +# +#################################################### + +ok($session->datetime->addToTime($wgbday,1,2,3) >= $wgbday+1*60*60+2*60+3, "addToTime()"); +is($session->datetime->addToTime($wgbday), $wgbday, 'addToTime defaults to adding 0'); + +#################################################### +# +# addToDateTime +# +#################################################### + +ok($session->datetime->addToDateTime($wgbday,1,2,3,4,5,6) >= $wgbday+1*60*60*24*365+2*60*60*24*28+3*60*60*24+4*60*60+5*60+6, "addToDateTime()"); +is($session->datetime->addToDateTime($wgbday), $wgbday, 'addToDateTime defaults to adding 0'); + +#################################################### +# +# secondsToInverval +# +#################################################### + +is(join(" ",$session->datetime->secondsToInterval(60*60*24*365*2)), "2 years", "secondsToInterval(), years"); +is(join(" ",$session->datetime->secondsToInterval(60*60*24*365*2.4)), "2 years", "secondsToInterval(), years, rounded down"); +is(join(" ",$session->datetime->secondsToInterval(60*60*24*365*2.9)), "3 years", "secondsToInterval(), years, rounded up"); +is(join(" ",$session->datetime->secondsToInterval(60*60*24*363)), "12 months", "secondsToInterval(), months"); +is(join(" ",$session->datetime->secondsToInterval(60*60*24*7*3)), "3 weeks", "secondsToInterval(), weeks"); +is(join(" ",$session->datetime->secondsToInterval(60*60*24*5)), "5 days", "secondsToInterval(), days"); +is(join(" ",$session->datetime->secondsToInterval(60*60*18)), "18 hours", "secondsToInterval(), hours"); +is(join(" ",$session->datetime->secondsToInterval(60*27)), "27 minutes", "secondsToInterval(), minutes"); +is(join(" ",$session->datetime->secondsToInterval(59)), "59 seconds", "secondsToInterval(), seconds"); + +#################################################### +# +# intervalToSeconds +# +#################################################### + +is($session->datetime->intervalToSeconds(40), 40, "intervalToSeconds() seconds as default"); +is($session->datetime->intervalToSeconds(59, 'seconds'), 59, "intervalToSeconds() seconds"); +is($session->datetime->intervalToSeconds(3, 'minutes'), 60*3, "intervalToSeconds() minutes"); +is($session->datetime->intervalToSeconds(2, 'hours'), 60*60*2, "intervalToSeconds() hours"); +is($session->datetime->intervalToSeconds(1, 'days'), 60*60*24, "intervalToSeconds() days"); +is($session->datetime->intervalToSeconds(2, 'weeks'), 60*60*24*14, "intervalToSeconds() weeks"); +is($session->datetime->intervalToSeconds(5, 'months'), 60*60*24*30*5, "intervalToSeconds() months"); +is($session->datetime->intervalToSeconds(7, 'years'), 60*60*24*365*7, "intervalToSeconds() years"); + END { foreach my $account ($buster, $dude) { (defined $account and ref $account eq 'WebGUI::User') and $account->delete;