Fix Time::Piece to handle objects in overloaded methods correctly

This commit is contained in:
Petr Písař 2018-09-05 16:19:53 +02:00
parent e82b9306ac
commit 8401a631d8
3 changed files with 200 additions and 4 deletions

View File

@ -1612,6 +1612,7 @@ Provides: perl(Time::Local) = 1.25 \
%global gendep_perl_Time_Piece \
Requires: perl(Carp) \
Requires: perl(Exporter) >= 5.57 \
Requires: perl(Scalar::Util) \
Requires: perl(Time::Local) \
Requires: perl(Time::Seconds) \
Requires: perl(XSLoader) \
@ -1619,8 +1620,8 @@ Requires: perl(constant) \
Requires: perl(integer) \
Requires: perl(overload) \
Requires: perl(strict) \
Provides: perl(Time::Piece) = 1.3204 \
Provides: perl(Time::Seconds) = 1.3204 \
Provides: perl(Time::Piece) = 1.33 \
Provides: perl(Time::Seconds) = 1.33 \
%{nil}
%global gendep_perl_Time_Piece_debuginfo \
%{nil}

View File

@ -0,0 +1,189 @@
From 9d890beed61e079102335ef5859d652b4e2c32ac Mon Sep 17 00:00:00 2001
From: Chris 'BinGOs' Williams <chris@bingosnet.co.uk>
Date: Mon, 20 Aug 2018 11:15:20 +0100
Subject: [PATCH] Update Time-Piece to CPAN version 1.33
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
[DELTA]
1.33 2018-08-18
- Allow objects in overloaded methods
Signed-off-by: Petr Písař <ppisar@redhat.com>
---
Porting/Maintainers.pl | 2 +-
cpan/Time-Piece/Piece.pm | 40 ++++++++++++++++++++++++----------------
cpan/Time-Piece/Seconds.pm | 2 +-
cpan/Time-Piece/t/06subclass.t | 15 +++++++++++++++
4 files changed, 41 insertions(+), 18 deletions(-)
diff --git a/Porting/Maintainers.pl b/Porting/Maintainers.pl
index eaf9ed3262..a137ee9483 100755
--- a/Porting/Maintainers.pl
+++ b/Porting/Maintainers.pl
@@ -1208,7 +1208,7 @@ use File::Glob qw(:case);
},
'Time::Piece' => {
- 'DISTRIBUTION' => 'ESAYM/Time-Piece-1.3204.tar.gz',
+ 'DISTRIBUTION' => 'ESAYM/Time-Piece-1.33.tar.gz',
'FILES' => q[cpan/Time-Piece],
'EXCLUDED' => [ qw[reverse_deps.txt] ],
},
diff --git a/cpan/Time-Piece/Piece.pm b/cpan/Time-Piece/Piece.pm
index 8acba86e76..d5624636c6 100644
--- a/cpan/Time-Piece/Piece.pm
+++ b/cpan/Time-Piece/Piece.pm
@@ -6,6 +6,7 @@ use XSLoader ();
use Time::Seconds;
use Carp;
use Time::Local;
+use Scalar::Util qw/ blessed /;
use Exporter ();
@@ -18,7 +19,7 @@ our %EXPORT_TAGS = (
':override' => 'internal',
);
-our $VERSION = '1.3204';
+our $VERSION = '1.33';
XSLoader::load( 'Time::Piece', $VERSION );
@@ -63,13 +64,27 @@ sub gmtime {
$class->_mktime($time, 0);
}
+
+# Check if the supplied param is either a normal array (as returned from
+# localtime in list context) or a Time::Piece-like wrapper around one.
+#
+# We need to differentiate between an array ref that we can interrogate and
+# other blessed objects (like overloaded values).
+sub _is_time_struct {
+ return 1 if ref($_[1]) eq 'ARRAY';
+ return 1 if blessed($_[1]) && $_[1]->isa('Time::Piece');
+
+ return 0;
+}
+
+
sub new {
my $class = shift;
my ($time) = @_;
my $self;
- if (ref($time)) {
+ if ($class->_is_time_struct($time)) {
$self = $time->[c_islocal] ? $class->localtime($time) : $class->gmtime($time);
}
elsif (defined($time)) {
@@ -106,10 +121,9 @@ sub parse {
sub _mktime {
my ($class, $time, $islocal) = @_;
- $class = eval { (ref $class) && (ref $class)->isa('Time::Piece') }
- ? ref $class
- : $class;
- if (ref($time)) {
+ $class = blessed($class) || $class;
+
+ if ($class->_is_time_struct($time)) {
my @new_time = @$time;
my @tm_parts = (@new_time[c_sec .. c_mon], $new_time[c_year]+1900);
$new_time[c_epoch] = $islocal ? timelocal(@tm_parts) : timegm(@tm_parts);
@@ -639,7 +653,8 @@ sub cdate {
sub str_compare {
my ($lhs, $rhs, $reverse) = @_;
- if (UNIVERSAL::isa($rhs, 'Time::Piece')) {
+
+ if (blessed($rhs) && $rhs->isa('Time::Piece')) {
$rhs = "$rhs";
}
return $reverse ? $rhs cmp $lhs->cdate : $lhs->cdate cmp $rhs;
@@ -652,9 +667,6 @@ use overload
sub subtract {
my $time = shift;
my $rhs = shift;
- if (UNIVERSAL::isa($rhs, 'Time::Seconds')) {
- $rhs = $rhs->seconds;
- }
if (shift)
{
@@ -667,7 +679,7 @@ sub subtract {
return $rhs - "$time";
}
- if (UNIVERSAL::isa($rhs, 'Time::Piece')) {
+ if (blessed($rhs) && $rhs->isa('Time::Piece')) {
return Time::Seconds->new($time->epoch - $rhs->epoch);
}
else {
@@ -679,10 +691,6 @@ sub subtract {
sub add {
my $time = shift;
my $rhs = shift;
- if (UNIVERSAL::isa($rhs, 'Time::Seconds')) {
- $rhs = $rhs->seconds;
- }
- croak "Invalid rhs of addition: $rhs" if ref($rhs);
return $time->_mktime(($time->epoch + $rhs), $time->[c_islocal]);
}
@@ -692,7 +700,7 @@ use overload
sub get_epochs {
my ($lhs, $rhs, $reverse) = @_;
- if (!UNIVERSAL::isa($rhs, 'Time::Piece')) {
+ unless (blessed($rhs) && $rhs->isa('Time::Piece')) {
$rhs = $lhs->new($rhs);
}
if ($reverse) {
diff --git a/cpan/Time-Piece/Seconds.pm b/cpan/Time-Piece/Seconds.pm
index 3a56b74485..71a4bd27f2 100644
--- a/cpan/Time-Piece/Seconds.pm
+++ b/cpan/Time-Piece/Seconds.pm
@@ -1,7 +1,7 @@
package Time::Seconds;
use strict;
-our $VERSION = '1.3204';
+our $VERSION = '1.33';
use Exporter 5.57 'import';
diff --git a/cpan/Time-Piece/t/06subclass.t b/cpan/Time-Piece/t/06subclass.t
index d6e4315c8f..a72cfb89ac 100644
--- a/cpan/Time-Piece/t/06subclass.t
+++ b/cpan/Time-Piece/t/06subclass.t
@@ -35,6 +35,21 @@ for my $method (qw(new localtime gmtime)) {
isa_ok($diff, $class, "yesterday via subtraction operator");
}
+{
+ my $g = $class->gmtime;
+ my $l = $class->localtime;
+
+ #via clone
+ my $l_clone = $class->new($l);
+ isa_ok($l_clone, $class, 'custom localtime via clone');
+ cmp_ok("$l_clone", 'eq', "$l", 'Clones match');
+
+ #via clone with gmtime
+ my $g_clone = $class->new($g);
+ isa_ok($g_clone, $class, 'custom gmtime via clone');
+ cmp_ok("$g_clone", 'eq', "$g", 'Clones match');
+}
+
{
# let's verify that we can use gmtime from T::P without the export magic
my $piece = Time::Piece::gmtime;
--
2.14.4

View File

@ -194,6 +194,10 @@ Patch27: perl-5.29.1-Time-HiRes-t-itimer.t-avoid-race-condition.patch
# run, in upstream after 5.29.1
Patch28: perl-5.28.0-Fix-script-run-bug-1-followed-by-Thai-digit.patch
# Fix Time::Piece to handle objects in overloaded methods correctly,
# in upstream after 5.29.1
Patch29: perl-5.29.1-Update-Time-Piece-to-CPAN-version-1.33.patch
# Link XS modules to libperl.so with EU::CBuilder on Linux, bug #960048
Patch200: perl-5.16.3-Link-XS-modules-to-libperl.so-with-EU-CBuilder-on-Li.patch
@ -2630,8 +2634,7 @@ so dates before the system's epoch may not work on all operating systems.
Summary: Time objects from localtime and gmtime
License: (GPL+ or Artistic) and BSD
Epoch: 0
# Real version 1.3204
Version: 1.32.04
Version: 1.33
Requires: %perl_compat
%if %{defined perl_bootstrap}
%gendep_perl_Time_Piece
@ -2774,6 +2777,7 @@ Perl extension for Version Objects
%patch26 -p1
%patch27 -p1
%patch28 -p1
%patch29 -p1
%patch200 -p1
%patch201 -p1
@ -2809,6 +2813,7 @@ perl -x patchlevel.h \
'Fedora Patch26: Fix a buffer overrun in deprecated utf8_to_uvchr()' \
'Fedora Patch27: Fix a time race in Time-HiRes/t/itimer.t test' \
'Fedora Patch28: Fix matching an ASCII digit followed by a non-ASCII digit using a script run' \
'Fedora Patch29: Fix Time::Piece to handle objects in overloaded methods correctly' \
'Fedora Patch200: Link XS modules to libperl.so with EU::CBuilder on Linux' \
'Fedora Patch201: Link XS modules to libperl.so with EU::MM on Linux' \
%{nil}
@ -5102,6 +5107,7 @@ popd
- Fix a buffer overrun in deprecated utf8_to_uvchr()
- Fix a time race in Time-HiRes/t/itimer.t test
- Fix matching an ASCII digit followed by a non-ASCII digit using a script run
- Fix Time::Piece to handle objects in overloaded methods correctly
* Wed Aug 01 2018 Petr Pisar <ppisar@redhat.com> - 4:5.28.0-420
- Fix a file descriptor leak in in-place edits (RT#133314)