John Siracusa schreef:
> All the various DateTime::Format::* modules are nice, but I've been
> thinking that it would be even nicer if DateTime had some sort of
> rudimentary parsing built in.
I agree with this; the DateTime constructor tends to get a little
verbose. However, I don't quite agree with your implementation ideas.
> 1/20/2002 1:02 p.m. (okay, maybe a Euro-mode for dd/mm/yyyy :)
A parser that can parse this format correctly should not be called
Simple. As you say, it has to have a US and a Euro mode at least; the
default DateTime parser should be simple enough to need no
configuration.
> IOW, DateTime::Format::Simple should handle at least 90% of the dates a
> user is likely to encounter (or a person is likely to enter).
If you want a parser that can handle 90% of all dates, call it
DateTime::Format::HideouslyComplex.
> I know I'm already
> tired of picking and loading DateTime::Format::* classes
The answer to this would be to have a Format class which can do
everything, not to have it included in DateTime. You can't have a parser
that magically parses everything, without heavy configuration for
amiguous cases.
IMHO, the reason a parser is needed in DateTime is the verbosity of the
constructor. To define a date I always need 2 lines, which screws up the
formatting/indenting of the program. A shorter contructor would be nice.
My suggestion would be to have a small, very restrictive parse_datetime
method, which is more-or-less the reverse of the datetime() method.
I suggest:
sub parse_datetime {
my ($self, $date) = @_;
if ($date ~= /^(-?\d{4}) # year
(\D?) # optional date separator
(\d\d) # month
\2 # date sep again
(\d\d) # day
[T ]?
(\d\d) # hour
(\D?) # optional time separator
(\d\d) # minute
\6 # time sep again
(\d\d) # second
$/x) {
my ($y, $m, $d, $h, $min, $s) = ($1, $3, $4, $5, $7, $8);
return DateTime->new(year => $y, month => $m, day => $d,
hour => $d, minute => $min, second => $s);
}
return;
}
This would make it possible to create dates in a compact but
understandable way:
$dt = DateTime->parse_datetime( "2003-07-13T12:38:00" );
I wouldn't use this method for user input, but only to create fixed
dates in my programs.
Eugene