Dave,

I took over the maintainer role for DT::F:Pg.

Want to try out this patch?
I'll release it if it works (I personally don't use infinite datetime on Pg, so it would be better if you check it out...)

--d

Dave Rolsky wrote:
Is there any particular reason this isn't supported other than lack of tuits? Pg can return "infinity" or "-infinity" for a datetime, so it'd be nice to turn that into the appropriate DateTime::Infinite object (and vice versa).

Claus, I can make the changes myself if you'd like.


-dave

/*===================================================
VegGuide.Org                        www.BookIRead.com
Your guide to all that's veg.       My book blog
===================================================*/


Index: Changes
===================================================================
RCS file: /cvsroot/perl-date-time/modules/DateTime-Format-Pg/Changes,v
retrieving revision 1.7
diff -d -u -r1.7 Changes
--- Changes     16 Mar 2005 16:13:18 -0000      1.7
+++ Changes     1 Sep 2005 23:17:24 -0000
@@ -1,5 +1,9 @@
 Revision history for Perl extension DateTime::Format::Pg.
 
+0.09  Fri Sep 02 09:00:00 2005
+    - support infinite, -infinite for parse_timestamp(tz) and format_datetime,
+      format_timestamp
+
 0.08  Wed Mar 16 16:00:00 2005
        - fixed parse_time (John Siracusa, bug #8516)
        - format_interval was missing (CEESHEK, bug #11898)
Index: lib/DateTime/Format/Pg.pm
===================================================================
RCS file: 
/cvsroot/perl-date-time/modules/DateTime-Format-Pg/lib/DateTime/Format/Pg.pm,v
retrieving revision 1.13
diff -d -u -r1.13 Pg.pm
--- lib/DateTime/Format/Pg.pm   16 Mar 2005 16:13:18 -0000      1.13
+++ lib/DateTime/Format/Pg.pm   1 Sep 2005 23:17:25 -0000
@@ -12,7 +12,7 @@
 use DateTime::TimeZone::UTC;
 use DateTime::TimeZone::Floating;
 
-$VERSION = '0.08';
+$VERSION = '0.09';
 $VERSION = eval $VERSION;
 
 our @ISA = ('DateTime::Format::Builder');
@@ -145,6 +145,26 @@
   return $self->new();
 }
 
+sub _create_infinity
+{
+    my $self = shift;
+    my %p    = @_;
+
+    if ($p{sign}) {
+        return DateTime::Infinite::Past->new;
+    } else {
+        return DateTime::Infinite::Future->new;
+    }
+}
+
+# infinite datetimes
+my $pg_infinity =
+{
+  regex => qr/^(-)?infinity$/,
+  params => [ qw(sign) ],
+  constructor => \&_create_infinity,
+};
+
 # Dates (without time zone)
 #
 # see EncodeDateOnly() in
@@ -356,12 +376,12 @@
     parse_timetz       => [ $pg_timeonly, ],
     parse_timestamptz  => [ $pg_datetime_iso, $pg_datetime_pg_eu,
                              $pg_datetime_pg_us, $pg_datetime_sql,
-                            $pg_datetime_german, ],
+                            $pg_datetime_german, $pg_infinity ],
     parse_datetime     => [ $pg_datetime_iso, $pg_datetime_pg_eu,
                             $pg_datetime_pg_us, $pg_datetime_sql,
                             $pg_datetime_german,
                             $pg_dateonly_iso, $pg_dateonly_german,
-                            $pg_dateonly_sql, $pg_timeonly, ],
+                            $pg_dateonly_sql, $pg_timeonly, $pg_infinity],
   }
 );
 
@@ -754,7 +774,9 @@
 sub format_timestamp
 {
   my ($self,$dt,%param) = @_;
-  if($dt->year()<=0) {
+  if($dt->is_infinite) {
+    return $dt->isa('DateTime::Infinite::Future') ? 'infinite' : '-infinite';
+  } elsif($dt->year()<=0) {
     return sprintf('%04d-%02d-%02d %s BC',
       1-$dt->year(),
       $dt->month(),
@@ -785,7 +807,9 @@
 sub format_timestamptz
 {
   my ($self,$dt,%param) = @_;
-  if($dt->year()<=0) {
+  if($dt->is_infinite) {
+    return $dt->isa('DateTime::Infinite::Future') ? 'infinite' : '-infinite';
+  } elsif($dt->year()<=0) {
     return sprintf('%04d-%02d-%02d',
       1-$dt->year(),
       $dt->month(),
Index: t/format_datetime.t
===================================================================
RCS file: 
/cvsroot/perl-date-time/modules/DateTime-Format-Pg/t/format_datetime.t,v
retrieving revision 1.3
diff -d -u -r1.3 format_datetime.t
--- t/format_datetime.t 20 Jun 2004 08:46:05 -0000      1.3
+++ t/format_datetime.t 1 Sep 2005 23:17:25 -0000
@@ -1,5 +1,5 @@
 # $Id: format_datetime.t,v 1.3 2004/06/20 08:46:05 cfaerber Exp $
-use Test::More tests => 7;
+use Test::More tests => 11;
 use DateTime 0.10;
 use DateTime::TimeZone;
 use DateTime::Format::Pg 0.02;
@@ -75,3 +75,24 @@
   my $dt = DateTime->new( %{$tests{$result}} );
   is( DateTime::Format::Pg->format_datetime($dt), $result );
 }
+
+is(
+    DateTime::Format::Pg->format_datetime(DateTime::Infinite::Future->new),
+    'infinite'
+);
+
+is(
+    DateTime::Format::Pg->format_timestamp(DateTime::Infinite::Future->new),
+    'infinite'
+);
+
+is(
+    DateTime::Format::Pg->format_datetime(DateTime::Infinite::Past->new),
+    '-infinite'
+);
+
+is(
+    DateTime::Format::Pg->format_timestamp(DateTime::Infinite::Past->new),
+    '-infinite'
+);
+

Reply via email to