diff options
author | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2006-11-29 12:08:35 +0000 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2006-11-29 12:08:35 +0000 |
commit | e3db0d8102d6a5093c49f123e33bf8525414eff9 (patch) | |
tree | 8d29486c403fb55afd7cc2f1a6735cedab7dd3d6 /ext | |
parent | 9d3968b272fe0ca6f9e4110df961b705d4665965 (diff) | |
download | perl-e3db0d8102d6a5093c49f123e33bf8525414eff9.tar.gz |
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
Diffstat (limited to 'ext')
-rw-r--r-- | ext/Time/Piece/Piece.pm | 14 | ||||
-rw-r--r-- | ext/Time/Piece/t/06subclass.t | 19 |
2 files changed, 31 insertions, 2 deletions
diff --git a/ext/Time/Piece/Piece.pm b/ext/Time/Piece/Piece.pm index a3c4bfa31b..59b9976bc2 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 0a729d6c82..dce097a29b 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"); + } +} |