This any use?

perl -MDateTime::Format=Mail -wle 'print DateTime->now->format_mail'
Sat, 01 Nov 2003 06:34:12 -0000

% perl -MDateTime::Format=HTTP -wle 'print DateTime->now->format_http'
Sat, 01 Nov 2003 06:34:23 GMT

% perl -MDateTime::Format=ICal -wle 'print DateTime->now->format_ical'
20031101T063430Z

% perl -MDateTime::Format=Pg -wle 'print DateTime->now->format_pg'
2003-11-01 06:34:35+0000

% perl -MDateTime::Format=W3CDTF,Pg -wle 'print DateTime->now->$_ for qw( 
format_w3cdtf format_pg )'
2003-11-01T06:35:04Z
2003-11-01 06:35:04+0000



cheers,
-- 
Iain.


package DateTime::Format;
use strict;
use warnings;
use vars qw( $VERSION );
$VERSION = '0.24';

sub import
{
    my $class = shift;
    my $target = 'DateTime';

    if (@_)
    {
        my @formats = @_;
        for my $leaf ( @formats )
        {
            my $module = "DateTime::Format::$leaf";
            eval "use $module";
            die $@ if $@;
            $class->insert_method(
                $target, "parse_\L$leaf", $class->make_parser( $module ) );
            $class->insert_method(
                $target, "format_\L$leaf", $class->make_formatter( $module ) );
        }
    }

    return 1;
}

sub insert_method
{
    my ($self, $target, $label, $code) = @_;
    no strict 'refs';
    no warnings 'redefine';
    *{$target."::".$label} = $code;
}

sub make_parser
{
    my ($self, $module) = @_;
    return sub {
        my ($self, $string) = @_;
        $module->parse_datetime( $string );
    }
}

sub make_formatter
{
    my ($self, $module) = @_;
    return sub {
        my ($self, $dt) = @_;
        $dt = $self unless defined $dt;
        $module->format_datetime( $dt );
    }
}

1;

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to