DateTime::Format::Flexible - DateTime::Format::Flexible - Flexibly parse strings and turn them into DateTime objects.
use DateTime::Format::Flexible; my $dt = DateTime::Format::Flexible->parse_datetime( 'January 8, 1999' ); # $dt = a DateTime object set at 1999-01-08T00:00:00
If you have ever had to use a program that made you type in the date a certain way and thought "Why can't the computer just figure out what date I wanted?", this module is for you.
DateTime::Format::Flexible attempts to take any string you give it and parse it into a DateTime object.
The test file tests 2500+ variations of date/time strings. If you can think of any that I do not cover, please let me know.
This module uses DateTime::Format::Builder under the covers.
build and parse_datetime do the same thing. Give it a string and it attempts to parse it and return a DateTime object.
If it can't it will throw an exception.
my $dt = DateTime::Format::Flexible->build( $date ); my $dt = DateTime::Format::Flexible->parse_datetime( $date );
A small list of supported formats:
- Month D, YYYY
- Mon D, YYYY
- Mon D, YYYY HH:MM:SS
there are 2500+ variations that are detected correctly in the test files (see t/data/tests.txt for most of them).
The DateTime website http://datetime.perl.org/?Modules as of march 2008 lists this module under 'Confusing' and recommends the use of DateTime::Format::Natural.
Unfortunately I do not agree. DateTime::Format::Natural currently fails more than 2000 of my parsing tests. DateTime::Format::Flexible supports different types of date/time strings than DateTime::Format::Natural. I think there is utility in that can be found in both of them.
The whole goal of DateTime::Format::Flexible is to accept just about any crazy date/time string that a user might care to enter. DateTime::Format::Natural seems to be a little stricter in what it can parse.
You cannot use a 1 or 2 digit year as the first field:
YY-MM-DD # not supported Y-MM-DD # not supported
It would get confused with MM-DD-YY
It also prefers the US format of MM-DD over the European DD-MM.
It also does not support timezones.
Tom Heady CPAN ID: thinc Punch, Inc. email@example.com http://www.punch.net/
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.