From aa7a90738a3aeb4b55a4249d20af92c7f8d3f5f0 Mon Sep 17 00:00:00 2001 From: Joe Atzberger Date: Thu, 20 Dec 2007 12:22:41 -0600 Subject: [PATCH] Dates.pm - IMPORTANT fix for dmy_array (part of obejct, not class) Signed-off-by: Chris Cormack Signed-off-by: Joshua Ferraro --- C4/Dates.pm | 12 +++++------- t/Dates.t | 38 ++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/C4/Dates.pm b/C4/Dates.pm index 15d14d14a3..b33b0a0d1d 100644 --- a/C4/Dates.pm +++ b/C4/Dates.pm @@ -29,8 +29,6 @@ $VERSION = 0.03; my $prefformat = C4::Context->preference('dateformat'); my $debug = $ENV{'DEBUG'} || 0; -our @dmy_array = (); - our %format_map = ( iso => 'yyyy-mm-dd', metric => 'dd/mm/yyyy', @@ -74,7 +72,8 @@ sub dmy_map ($$) { my $aref = eval $xsub; return @{$aref}; } - $debug and carp "Illegal Date '$val' does not match $dformat format: $re\n"; + # $debug and + carp "Illegal Date '$val' does not match '$dformat' format: " . $self->visual() . "\n"; return 0; } @@ -91,15 +90,14 @@ sub init ($;$$) { $self->{'dateformat'} = $dformat = (scalar(@_) >= 2) ? $_[1] : $prefformat; ($format_map{$dformat}) or croak "Invalid date format '$dformat' from " . ((scalar(@_) >= 2) ? 'argument' : 'system preferences'); - # scalar(@self::dmy_array) and croak "\$self is " . ref($self) . "\n\@self::dmy_array already populated: @self::dmy_array"; - @self::dmy_array = ((@_) ? $self->dmy_map(shift) : localtime); - $debug and print STDERR "(during init) \@self::dmy_array = (@self::dmy_array)\n"; #debug + $self->{'dmy_arrayref'} = [((@_) ? $self->dmy_map(shift) : localtime )] ; + $debug and print STDERR "(during init) \@\$self->{'dmy_arrayref'}: " . join(' ',@{$self->{'dmy_arrayref'}}) . "\n"; return $self; } sub output ($;$) { my $self = shift; my $newformat = (@_) ? _recognize_format(shift) : $prefformat; - return (eval {POSIX::strftime($posix_map{$newformat}, @self::dmy_array)} || undef); + return (eval {POSIX::strftime($posix_map{$newformat}, @{$self->{'dmy_arrayref'}})} || undef); } sub today ($;$) { # NOTE: sets date value to today (and returns it in the requested or current format) my $class = shift; diff --git a/t/Dates.t b/t/Dates.t index 303e582cd3..5a3c5ecd86 100755 --- a/t/Dates.t +++ b/t/Dates.t @@ -3,11 +3,17 @@ use strict; use warnings; -use Test::More tests => 91; +use Test::More tests => 92; BEGIN { use_ok('C4::Dates', qw(format_date format_date_in_iso)); } +sub describe ($$) { + my $front = sprintf("%-25s", shift); + my $tail = shift || 'FAILED'; + return "$front : $tail"; +} + my %thash = ( iso => ['2001-01-01','1989-09-21'], metric => ["01-01-2001",'21-09-1989'], @@ -33,29 +39,37 @@ print "\n"; foreach (@formats) { my $pre = sprintf '(%-6s)', $_; ok($date = C4::Dates->new(), "$pre Date Creation : new()"); - ok($_ eq ($format = $date->format($_)), "$pre format($_) : $format" ); - ok($format = $date->visual(), "$pre visual() : $format" ); - ok($today = $date->output(), "$pre output() : $today" ); - ok($today = $date->today(), "$pre object->today : $today" ); + ok($_ eq ($format = $date->format($_)), "$pre format($_) : " . ($format|| 'FAILED') ); + ok($format = $date->visual(), "$pre visual() : " . ($format|| 'FAILED') ); + ok($today = $date->output(), "$pre output() : " . ($today || 'FAILED') ); + ok($today = $date->today(), "$pre object->today : " . ($today || 'FAILED') ); print "\n"; } -diag "\nTesting with inputs:\n"; +diag "\nTesting with valid inputs:\n"; foreach $format (@formats) { my $pre = sprintf '(%-6s)', $format; foreach my $testval (@{$thash{ $format }}) { - ok($date = C4::Dates->new($testval,$format), "$pre Date Creation : new('$testval','$format')"); - ok($re = $date->regexp, "$pre has regexp()" ); - ok($val = $date->output(), "$pre output() : $val" ); + ok($date = C4::Dates->new($testval,$format), "$pre Date Creation : new('$testval','$format')"); + ok($re = $date->regexp, "$pre has regexp()" ); + ok($val = $date->output(), describe("$pre output()", $val) ); foreach (grep {!/$format/} @formats) { - ok($today = $date->output($_), "$pre output(" . sprintf("%8s","'$_'") . "): $today"); + ok($today = $date->output($_), describe(sprintf("$pre output(%8s)","'$_'"), $today) ); } - ok($today = $date->today(), "$pre object->today : $today" ); + ok($today = $date->today(), describe("$pre object->today", $today) ); # ok($today == ($today = C4::Dates->today()), "$pre CLASS ->today : $today" ); - ok($val = $date->output(), "$pre output() : $val" ); + ok($val = $date->output(), describe("$pre output()", $val) ); # ok($format eq ($format = $date->format()), "$pre format() : $format" ); print "\n"; } } +diag "\nTesting object independence from class\n"; +my $in1 = '12/25/1952'; # us +my $in2 = '13/01/2001'; # metric +my $d1 = C4::Dates->new($in1, 'us'); +my $d2 = C4::Dates->new($in2, 'metric'); +my $out1 = $d1->output('iso'); +my $out2 = $d2->output('iso'); +ok($out1 ne $out2, "subsequent constructors get different dataspace ($out1 != $out2)"); diag "done.\n"; -- 2.39.5