Tom Heady > DateTime-Format-Flexible > DateTime::Format::Flexible

Download:
DateTime-Format-Flexible-0.21.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  2
Open  0
View/Report Bugs
Module Version: 0.21   Source  

NAME ^

DateTime::Format::Flexible - DateTime::Format::Flexible - Flexibly parse strings and turn them into DateTime objects.

SYNOPSIS ^

  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

DESCRIPTION ^

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.

USAGE ^

This module uses DateTime::Format::Builder under the covers.

parse_datetime

Give it a string and it attempts to parse it and return a DateTime object.

If it cannot it will throw an exception.

 my $dt = DateTime::Format::Flexible->parse_datetime( $date );

 my $dt = DateTime::Format::Flexible->parse_datetime(
     $date,
     strip    => [qr{\.\z}],                  # optional, remove a trailing period
     tz_map   => {EDT => 'America/New_York'}, # optional, map the EDT timezone to America/New_York
     lang     => ['es'],                      # optional, only parse using spanish
     european => 1                            # optional, catch some cases of DD-MM-YY
 );

base

gets/sets the base DateTime for incomplete dates. Requires a valid DateTime object as an argument when setting.

example:

 DateTime::Format::Flexible->base( DateTime->new( year => 2009, month => 6, day => 22 ) );
 my $dt = DateTime::Format::Flexible->parse_datetime( '23:59' );
 # $dt is now 2009-06-22T23:59:00

build

an alias for parse_datetime

Example formats

A small list of supported formats:

YYYYMMDDTHHMMSS
YYYYMMDDTHHMM
YYYYMMDDTHH
YYYYMMDD
YYYYMM
MM-DD-YYYY
MM-D-YYYY
MM-DD-YY
M-DD-YY
YYYY/DD/MM
YYYY/M/DD
YYYY/MM/D
M-D
MM-D
M-D-Y
Month D, YYYY
Mon D, YYYY
Mon D, YYYY HH:MM:SS
...

there are 9000+ variations that are detected correctly in the test files (see t/data/* for most of them). If you can think of any that I do not cover, please let me know.

NOTES ^

As of version 0.11 you will get a DateTime::Infinite::Future object if the passed in date is 'infinity' and a DateTime::Infinite::Past object if the passed in date is '-infinity'. If you are expecting these types of strings, you might want to check for 'is_infinite()' from the object returned.

example:

 my $dt = DateTime::Format::Flexible->parse_datetime( 'infinity' );
 if ( $dt->is_infinite )
 {
      # you have a Infinite object.
 }

The DateTime website http://datetime.perl.org/?Modules as of february 2010 lists this module under 'Confusing' and recommends the use of DateTime::Format::Natural.

Unfortunately I do not agree. DateTime::Format::Natural 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.

BUGS/LIMITATIONS ^

You cannot use a 1 or 2 digit year as the first field unless the year is > 31:

 YY-MM-DD # not supported if YY is <= 31
 Y-MM-DD  # not supported

It gets confused with MM-DD-YY

AUTHOR ^

Tom Heady <cpan@punch.net>

COPYRIGHT & LICENSE ^

Copyright 2007-2011 Tom Heady.

This program is free software; you can redistribute it and/or modify it under the terms of either:

SEE ALSO ^

DateTime::Format::Builder, DateTime::Timezone, DateTime::Format::Natural

syntax highlighting: