From: Rafael Garcia-Suarez Date: Wed, 29 Nov 2006 12:08:35 +0000 (+0000) Subject: Patch by Derek Price to Time::Piece for CPAN bug #21255: X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e3db0d8102d6a5093c49f123e33bf8525414eff9;p=p5sagit%2Fp5-mst-13.2.git Patch by Derek Price to Time::Piece for CPAN bug #21255: NOTDATE - DATE should stringify DATE and let Perl handle things p4raw-id: //depot/perl@29417 --- diff --git a/ext/Time/Piece/Piece.pm b/ext/Time/Piece/Piece.pm index a3c4bfa..59b9976 100644 --- a/ext/Time/Piece/Piece.pm +++ b/ext/Time/Piece/Piece.pm @@ -22,7 +22,7 @@ our %EXPORT_TAGS = ( ':override' => 'internal', ); -our $VERSION = '1.11_01'; +our $VERSION = '1.11_02'; bootstrap Time::Piece $VERSION; @@ -540,7 +540,17 @@ sub subtract { if (UNIVERSAL::isa($rhs, 'Time::Seconds')) { $rhs = $rhs->seconds; } - die "Can't subtract a date from something!" if shift; + + if (shift) + { + # SWAPED is set (so someone tried an expression like NOTDATE - DATE). + # Imitate Perl's standard behavior and return the result as if the + # string $time resolves to was subtracted from NOTDATE. This way, + # classes which override this one and which have a stringify function + # that resolves to something that looks more like a number don't need + # to override this function. + return $rhs - "$time"; + } if (UNIVERSAL::isa($rhs, 'Time::Piece')) { return Time::Seconds->new($time->epoch - $rhs->epoch); diff --git a/ext/Time/Piece/t/06subclass.t b/ext/Time/Piece/t/06subclass.t index 0a729d6..dce097a 100644 --- a/ext/Time/Piece/t/06subclass.t +++ b/ext/Time/Piece/t/06subclass.t @@ -45,3 +45,22 @@ for my $method (qw(new localtime gmtime)) { use base qw(Time::Piece); # this package is identical, but will be ->isa('Time::Piece::Twin'); } + +{ + my $class = "Time::Piece::NumString"; + my $piece = $class->strptime ("2006", "%Y"); + is (2007 - $piece, 1, + "subtract attempts stringify for unrecognized objects."); +} + +## Below is a package which only changes the stringify function. +{ + package Time::Piece::NumString; + use base qw(Time::Piece); + use overload '""' => \&_stringify; + sub _stringify + { + my $self = shift; + return $self->strftime ("%Y"); + } +}