There's really no huge problem.  DateTime.pm would just have to skip the
relevant tests if a given formatter wasn't installed.  Since this would be
an optional feature, it'd be okay, I think.

Anyway, patches are still welcome.  I don't really need this, so I don't
think I'll be implementing it myself any time soon ;)

Patches, you say? (a simple test is also included at the bottom) Let me know if this is okay, and I'll submit.

--d


Index: lib/DateTime.pm =================================================================== RCS file: /cvsroot/perl-date-time/modules/DateTime.pm/lib/DateTime.pm,v retrieving revision 1.286 diff -u -r1.286 DateTime.pm --- lib/DateTime.pm 24 Jul 2004 19:30:00 -0000 1.286 +++ lib/DateTime.pm 2 Sep 2004 04:31:03 -0000 @@ -58,7 +58,7 @@ use overload ( 'fallback' => 1, '<=>' => '_compare_overload', 'cmp' => '_compare_overload', - '""' => 'iso8601', + '""' => 'format_datetime', '-' => '_subtract_overload', '+' => '_add_overload', ); @@ -157,6 +157,7 @@ { %$BasicValidate, time_zone => { type => SCALAR | OBJECT, default => 'floating' }, + formatter => { type => SCALAR | OBJECT, optional => 1 } };

 sub new
@@ -194,7 +195,7 @@
         $class->_time_as_seconds( @p{ qw( hour minute second ) } );

     $self->{rd_nanosecs} = $p{nanosecond};
-
+    $self->{formatter} = $p{formatter};
     bless $self, $class;

$self->_normalize_nanoseconds( $self->{local_rd_secs}, $self->{rd_nanosecs} );
@@ -340,6 +341,7 @@
locale => { type => SCALAR | OBJECT, optional => 1 },
language => { type => SCALAR | OBJECT, optional => 1 },
time_zone => { type => SCALAR | OBJECT, optional => 1 },
+ formatter => { type => SCALAR | OBJECT, optional => 1 },
}
);


@@ -378,6 +380,7 @@
},
locale => { type => SCALAR | OBJECT, optional => 1 },
language => { type => SCALAR | OBJECT, optional => 1 },
+ formatter => { type => SCALAR | OBJECT, optional => 1 },
},
);


@@ -481,6 +484,8 @@
                         );
 }

+sub formatter { $_[0]->{formatter} }
+
 sub clone { bless { %{ $_[0] } }, ref $_[0] }

 sub year    { $_[0]->{local_c}{year} }
@@ -612,6 +617,26 @@
     return DateTime->_leap_seconds( $self->{utc_rd_days} );
 }

+sub parse_datetime
+{
+ my $self = shift;
+ if (!$self->{formatter} || ! UNIVERSAL::can($self->{formatter}, 'parse_datetime')) {
+ die "No formatter available (or specified formatter can't perform 'parse_datetime()')";
+ }
+ $self->{formatter}->parse_datetime($_[0]);
+}
+
+sub format_datetime
+{
+ my $self = shift;
+ if (!$self->{formatter} || ! UNIVERSAL::can($self->{formatter}, 'format_datetime')) {
+ # XXX - could warn()
+ return $self->iso8601;
+ } else {
+ return $self->{formatter}->format_datetime($self);
+ }
+}
+
sub hms
{
my ( $self, $sep ) = @_;
@@ -1358,6 +1383,8 @@


 sub set_locale { $_[0]->set( locale => $_[1] ) }

+sub set_formatter { $_[0]->{formatter} = $_[1] }
+
 sub truncate
 {
     my $self = shift;


And here's the test: t/31formatter.t

#!/usr/bin/perl -w

use strict;
BEGIN
{
eval { require DateTime::Format::Strptime };
if ($@) {
require Test::More;
Test::More->import(skip_all => "DateTime::Format::Strptime not installed");
}
require Test::More;
Test::More->import(tests => 6);
}


use DateTime;

my $formatter = DateTime::Format::Strptime->new(
    pattern => '%Y%m%d %T',
    locale  => 'en_US',
);

my $dt = DateTime->new(
    year => 2004,
    month => 9,
    day => 2,
    hour => 13,
    minute => 23,
    second => 34,
    formatter => $formatter
);

is($dt->format_datetime(), '20040902 13:23:34', 'Format datetime');
is($dt->compare($dt->parse_datetime($dt->format_datetime())), 0, 'Parse formatted datetime');


# check stringification (with formatter)
is($dt->format_datetime, "$dt", "Stringification (with formatter)");

# check if the default behavior works
$dt->set_formatter(undef);
is($dt->format_datetime(), $dt->iso8601, 'Default iso8601 works');

# check stringification (default)
is($dt->format_datetime, "$dt", "Stringification (no formatter -> format_datetime)");
is($dt->iso8601, "$dt", "Stringification (no formatter -> iso8601)");




Reply via email to