Its really important that date objects masquarade well as strings so you don't have to redesign your whole interface when you introduce them. DateTime currently only stringifies as an ISO8601 date. This is limiting. To get it in any other format you have to drop down to a plain string which sorta defeats the point.
DateTime should have a default_format() method that takes a strftime pattern. This is the format it will use when stringifing.
I'm writing a subclass that does this for my own purposes. Let me know if you think its a good idea and want a patch.
I was just thinking how I would use it, but this would be kind od nice...
use DateTime; use DateTime::Format::Japanese;
my $dt = DateTime->now(formatter => DateTime::Format::Japanese->new);
print "today is $dt!\n"; # internally calls $dt->format_datetime(), which proxies to # formatter object
And similarly,
$dt->parse_datetime($some_datetime_string);
This will allow me to stringify DateTime objects in Japanese :)
We have a relatively standardized interface for formatters, so this sounds like a relatively painless thing to add.
Here's a (badly written) patch that just shows how this might work...
===================================================================
RCS file: /cvsroot/perl-date-time/modules/DateTime.pm/lib/DateTime.pm,v
retrieving revision 1.284
diff -r1.284 DateTime.pm
61c61
< '""' => 'iso8601',
---
> '""' => 'format_datetime',
159a160
> formatter => { type => OBJECT, optional => 1 }
197a199,200
> $self->{formatter} = $p{formatter};
>
339a343
> formatter => { type => OBJECT, optional => 1 },
378a383
> formatter => { type => OBJECT, optional => 1 },
485a491,492
> sub formatter{ $_[0]->{formatter} }
>
625a633,654
> sub parse_datetime
> {
> my $self = shift;
> my $string = shift;
> if (!$self->{formatter} || !UNIVERSAL::can($self->{formatter}, 'parse_datetime')) {
> die "Not formatter available (or formatter can't do parse_datetime())";
> }
>
> $self->{formatter}->parse_datetime($string);
> }
>
> sub format_datetime
> {
> my $self = shift;
>
> if (!$self->{formatter} || !UNIVERSAL::can($self->{formatter}, 'format_datetime')) {
> return $self->iso8601;
> } else {
> return $self->{formatter}->format_datetime($self);
> }
> }
>
1389a1419,1426
> sub set_formatter
> {
> my $self = shift;
> my $formatter = shift;
>
> $self->{formatter} = $formatter;
> }
>
--d
