- FORMATS HANDLED
- SEE ALSO
- COPYRIGHT AND LICENSE
my $parser = Date::Extract->new(); my $dt = $parser->extract($arbitrary_text) or die "No date found."; return $dt->ymd;
Date::Extract - extract probable dates from strings
There are already a few modules for getting a date out of a string. DateTime::Format::Natural should be your first choice. There's also Time::ParseDate which fits many formats. Finally, you can coerce Date::Manip to do your bidding.
But I needed something that will take an arbitrary block of text, search it for something that looks like a date string, and extract it. This module fills this niche. By design it will produce few false positives. This means it will not catch nearly everything that looks like a date string. So if you have the string "do homework for class 2019" it won't return a DateTime object with the year set to 2019. This is what your users would probably expect.
Choose what format the extracted date(s) will be. The default is "DateTime", which will return DateTime object(s). Other option include "verbatim" (return the original text), "epoch" (return Unix timestamp), or "combined" (return hashref containing these keys "verbatim", "DateTime", "pos" [position of date string in the text]).
Only relevant when C,format> is set to "DateTime".
Forces a particular time zone to be set (this actually matters, as "tomorrow" on Monday at 11 PM means something different than "tomorrow" on Tuesday at 1 AM).
By default it will use the "floating" time zone. See the documentation for DateTime.
This controls both the input time zone and output time zone.
This argument decides what happens when an ambiguous date appears in the input. For example, "Friday" may refer to any number of Fridays. The valid options for this argument are:
Prefer the nearest date. This is the default.
Prefer the closest future date.
Prefer the closest past date. NOT YET SUPPORTED.
If the text has multiple possible dates, then this argument determines which date will be returned. By default it's 'first'.
Returns the first date found in the string.
Returns the final date found in the string.
Returns the date found in the string that chronologically precedes any other date in the string.
Returns the date found in the string that chronologically follows any other date in the string.
Returns all dates found in the string, in the order they were found in the string.
Returns all dates found in the string, in chronological order.
Takes an arbitrary amount of text and extracts one or more dates from it. The return value will be zero or more dates, which by default are DateTime objects (but can be customized with the
format argument). If called in scalar context, only one will be returned, even if the
returns argument specifies multiple possible return values.
See the documentation of
new for the configuration of this method. Any arguments passed into this method will trump those from the constructor.
You may reuse a parser for multiple calls to
You do not need to have an instantiated
Date::Extract object to call this method. Just
Date::Extract->extract($foo) will work.
today; tomorrow; yesterday
last Friday; next Monday; previous Sat
November 13th, 1986; Nov 13, 1986
13 November 1986; 13 Nov 1986
November 13th; Nov 13
13 Nov; 13th November
This module is intentionally very simple. Surprises are not welcome here.
Steven Haryanto <email@example.com>
This software is copyright (c) 2014 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.