From 64640003afc16a1cab27dc1b43e519a14282e4c3 Mon Sep 17 00:00:00 2001 From: Luke Robinson Date: Tue, 17 Nov 2009 15:28:52 +0000 Subject: [PATCH] ConvertUTCToTZ.pm macro and test Conflicts: lib/WebGUI/Macro/ConvertUTCToTZ.pm --- lib/WebGUI/Macro/ConvertUTCToTZ.pm | 24 +++++++------- t/Macro/ConvertUTCToTZ.t | 53 ++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 t/Macro/ConvertUTCToTZ.t diff --git a/lib/WebGUI/Macro/ConvertUTCToTZ.pm b/lib/WebGUI/Macro/ConvertUTCToTZ.pm index add651399..4098980c4 100644 --- a/lib/WebGUI/Macro/ConvertUTCToTZ.pm +++ b/lib/WebGUI/Macro/ConvertUTCToTZ.pm @@ -73,33 +73,31 @@ sub process { $toTZ ||= $uTZ; $format ||= $uFormat; - # remove all whitespace including newlines - $date =~ s/\s//msg; - - # Additional date delimiters accepted for edge cases - my ( $year, $month, $day ) = split /[\/\-\.]/, $date; + my ( $year, $month, $day ); + if ($date) { + ( $year, $month, $day ) = split /[\/\-\.]/, $date; + $date =~ s/\s//msg; # remove all whitespace including newlines + } my $dt = WebGUI::DateTime->now; - unless ( length($year) ) { + unless ( $year ) { $year = $dt->year; } - unless ( length($month) ) { + unless ( $month ) { $month = $dt->month; } - unless ( length($day) ) { + unless ( $day ) { $day = $dt->day; } my $formatter = DateTime::Format::Strptime->new( pattern => $format ); - # Macro calls also seem to include any spaces between commas - $time =~ s/^\s+//msg; - my ( $hour, $minute, $second ); - if ( length($time) ) { + if ( $time ) { + $time =~ s/^\s+//msg; # remove all whitespace including newlines ( $hour, $minute, $second ) = split /\:/, $time; } my $dtOut = DateTime->new( @@ -114,7 +112,7 @@ sub process { # If no time component, we use the date as provided with no conversion # Without a time to convert between, there is no point to altering the date - if ( length($time) ) { + if ( $time ) { $dtOut->set_time_zone($toTZ); } $dtOut->set_formatter($formatter); diff --git a/t/Macro/ConvertUTCToTZ.t b/t/Macro/ConvertUTCToTZ.t new file mode 100644 index 000000000..e126885a0 --- /dev/null +++ b/t/Macro/ConvertUTCToTZ.t @@ -0,0 +1,53 @@ +#------------------------------------------------------------------- +# WebGUI is Copyright 2001-2008 Plain Black Corporation. +#------------------------------------------------------------------- +# Please read the legal notices (docs/legal.txt) and the license +# (docs/license.txt) that came with this distribution before using +# this software. +#------------------------------------------------------------------- +# http://www.plainblack.com info@plainblack.com +#------------------------------------------------------------------- + +use FindBin; +use strict; +use lib "$FindBin::Bin/../lib"; + +use WebGUI::Test; +use WebGUI::Session; +use Test::More; +use DateTime; + +my $session = WebGUI::Test->session; + +my $numTests = 5+1; +plan tests => $numTests; + +my $macro = 'WebGUI::Macro::ConvertUTCToTZ'; +my $loaded = use_ok($macro); +my $formatter = '%Y-%m-%d'; + +SKIP: { + skip "Unable to load $macro", $numTests-1 unless $loaded; + + my $today = DateTime->now(); + $today->set_time_zone('UTC'); + my $yesterday = $today->clone; + $yesterday->subtract( days => 1 ); + $yesterday->set_formatter($formatter); + + my $out1 = WebGUI::Macro::ConvertUTCToTZ::process($session); + like( $out1, qr/\d{2}\/\d{2}\/\d{2}\/\d{2}\/\d{2}/, 'No parameters passed, check pattern'); + + my $out2 = WebGUI::Macro::ConvertUTCToTZ::process($session, 'UTC', $formatter, $today->ymd); + is( $out2, $today->ymd, 'UTC, date only'); + + my $out3 = WebGUI::Macro::ConvertUTCToTZ::process($session, 'UTC', $formatter, $today->ymd, '02:30:00'); + is( $out3, $today->ymd, 'UTC, date and time'); + + my $out4 = WebGUI::Macro::ConvertUTCToTZ::process($session, 'America/Chicago', $formatter, $today->ymd, '02:30:00'); + is( $out4, $yesterday->ymd, 'Chicago, date and a.m. time'); + + my $out5 = WebGUI::Macro::ConvertUTCToTZ::process($session, 'America/Chicago', $formatter, $today->ymd, '14:30:00'); + is( $out5, $today->ymd, 'Chicago, date and p.m. time'); +} +