|
NAMEDateTime::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
DESCRIPTIONIf 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. USAGEThis module uses DateTime::Format::Builder under the covers. parse_datetimeGive 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
);
basegets/sets the base DateTime for incomplete dates. Requires a valid DateTime object as an argument when setting. This defaults to DateTime->now. 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
buildan alias for parse_datetime Example formatsA small list of supported formats:
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. NOTESAs 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.
}
BUGS/LIMITATIONSYou 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 AUTHORTom Heady <cpan@punch.net> COPYRIGHT & LICENSECopyright 2007-2018 Tom Heady. This program is free software; you can redistribute it and/or modify it under the terms of either:
SEE ALSODateTime::Format::Builder, DateTime::Timezone, DateTime::Format::Natural
|