Skip Menu |

This queue is for tickets about the Time-Piece CPAN distribution.

Report information
The Basics
Id: 21255
Status: resolved
Priority: 0/
Queue: Time-Piece

People
Owner: Nobody in particular
Requestors: derek [...] ximbiot.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 1.08
Fixed in: (no value)



Subject: NOTDATE - DATE should stringify DATE and let Perl handle things.
This is broken in 1.08, but that version is still missing from the list. The subtract function currently dies when SWAP is set. SWAP is set when perl encounters an expression like NOTDATE - DATE, where Perl doesn't have a subtraction function overloaded for NOTDATE and DATE is a Time::Piece object. This seems to go against the Perl philosophy. If the `-' operator had not been overloaded in Time::Piece, Perl would have automatically stringified the DATE, then numified that string, and then attempted to subtract that from NOTDATE. This is important in the context of any subclass of Time::Piece which attempts to change the stringify function to something that makes more sense as a number. If Time::Piece allowed Perl to handle the subtraction with the stringified object, then the subclass would not need to override Time::Piece's subtraction function to enable subtraction, assuming there were not other attributes of the subclass that needed to be preserved across subtraction. For an example of preserving Perl's standard stringify behavior in the event that SWAP is set in the subtraction function, please see the Time::Piece::Adaptive module on CPAN.
Here's the patch. It was basically a one-liner, but I included a big comment, a line in the Changes file, and a test for t/06subclass.t.
Index: Changes =================================================================== RCS file: /cvsroot/Time-Piece/Changes,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 Changes --- Changes 8 Sep 2006 17:14:13 -0000 1.1.1.1 +++ Changes 8 Sep 2006 17:42:57 -0000 @@ -1,6 +1,10 @@ Time::Piece Changes +1.12 + - Subtract now stringifies times subtracted from unrecognized objects + (bug #21255). + 1.11 - Skip %V test on Win32 Index: Piece.pm =================================================================== RCS file: /cvsroot/Time-Piece/Piece.pm,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 Piece.pm --- Piece.pm 8 Sep 2006 17:14:13 -0000 1.1.1.1 +++ Piece.pm 8 Sep 2006 17:42:57 -0000 @@ -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); Index: t/06subclass.t =================================================================== RCS file: /cvsroot/Time-Piece/t/06subclass.t,v retrieving revision 1.1.1.1 diff -u -p -r1.1.1.1 06subclass.t --- t/06subclass.t 8 Sep 2006 17:14:13 -0000 1.1.1.1 +++ t/06subclass.t 8 Sep 2006 17:42:57 -0000 @@ -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"); + } +}
From: RGARCIA [...] cpan.org
I've applied this patch to the version of Time::Piece bundled with bleadperl as change #29417.
Marking resolved.