On 8/21/07, Kjell-Magne Øierud <[EMAIL PROTECTED]> wrote:
> Dave, thanks for your reply.
>
> * Dave Rolsky:
> > The reason it's omitted is historical, though I can't remember
> > why. I don't plan to change it now, since that'd be a pointless
> > breaking of backwards compatibility.
> >
> > You can change the stringification, however. Search the DateTime.pm
> > docs for "formatter".
>
> The formatter functionality is indeed very useful and I'm already
> using it in the view parts of the systems. But I'm afraid it wont
> entirely solve my problem satisfactory.
>
> To my understanding, the formatter can't be set globally as a
> default. It must be set on every object. To rely on every object to
> have set the formatter-attribute is too fragile in my case.
>
> So if you don't want to change the default behavior or make it
> configurable, I think the most robust solution for me is to make all
> my systems use a module that redefines DateTime::_stringify().
I think there is an alternative which is more immediate and simpler
wrt many issues: create a sub (that acts like a DateTime factory),
wrap the DateTime constructors that matter to you and make sure the
formatter you want is correctly setted. Something like:
# just an example, use the formatter that suits you
use DateTime::Format::Strptime;
my $dt_formatter = DateTime::Format::Strptime->new( pattern => 'whatever' );
sub new_datetime {
my $dt = DateTime->new(@_);
$dt->set_formatter($dt_formatter);
return $dt;
}
(This factory could be made into an object as well.) Now use
everywhere this "new_datetime()" instead -- no need for subclassing
DateTime, fussing with internals, etc.
Cheers,
Adriano Ferreira