ConvertUTCToTZ.pm macro and test

Conflicts:

	lib/WebGUI/Macro/ConvertUTCToTZ.pm
This commit is contained in:
Luke Robinson 2009-11-17 15:28:52 +00:00 committed by Colin Kuskie
parent 53f3683c21
commit 64640003af
2 changed files with 64 additions and 13 deletions

View file

@ -73,33 +73,31 @@ sub process {
$toTZ ||= $uTZ; $toTZ ||= $uTZ;
$format ||= $uFormat; $format ||= $uFormat;
# remove all whitespace including newlines my ( $year, $month, $day );
$date =~ s/\s//msg; if ($date) {
( $year, $month, $day ) = split /[\/\-\.]/, $date;
# Additional date delimiters accepted for edge cases $date =~ s/\s//msg; # remove all whitespace including newlines
my ( $year, $month, $day ) = split /[\/\-\.]/, $date; }
my $dt = WebGUI::DateTime->now; my $dt = WebGUI::DateTime->now;
unless ( length($year) ) { unless ( $year ) {
$year = $dt->year; $year = $dt->year;
} }
unless ( length($month) ) { unless ( $month ) {
$month = $dt->month; $month = $dt->month;
} }
unless ( length($day) ) { unless ( $day ) {
$day = $dt->day; $day = $dt->day;
} }
my $formatter = DateTime::Format::Strptime->new( pattern => $format ); 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 ); my ( $hour, $minute, $second );
if ( length($time) ) { if ( $time ) {
$time =~ s/^\s+//msg; # remove all whitespace including newlines
( $hour, $minute, $second ) = split /\:/, $time; ( $hour, $minute, $second ) = split /\:/, $time;
} }
my $dtOut = DateTime->new( my $dtOut = DateTime->new(
@ -114,7 +112,7 @@ sub process {
# If no time component, we use the date as provided with no conversion # 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 # 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_time_zone($toTZ);
} }
$dtOut->set_formatter($formatter); $dtOut->set_formatter($formatter);

53
t/Macro/ConvertUTCToTZ.t Normal file
View file

@ -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');
}