1Date::Extract(3) User Contributed Perl Documentation Date::Extract(3)
2
3
4
6 Date::Extract - extract probable dates from strings
7
9 my $parser = Date::Extract->new();
10 my $dt = $parser->extract($arbitrary_text)
11 or die "No date found.";
12 return $dt->ymd;
13
15 There are already a few modules for getting a date out of a string.
16 DateTime::Format::Natural should be your first choice. There's also
17 Time::ParseDate which fits many formats. Finally, you can coerce
18 Date::Manip to do your bidding.
19
20 But I needed something that will take an arbitrary block of text,
21 search it for something that looks like a date string, and extract it.
22 This module fills this niche. By design it will produce few false
23 positives. This means it will not catch nearly everything that looks
24 like a date string. So if you have the string "do homework for class
25 2019" it won't return a DateTime object with the year set to 2019. This
26 is what your users would probably expect.
27
29 new PARAMHASH => "Date::Extract"
30 arguments
31
32 format
33 Choose what format the extracted date(s) will be. The default is
34 "DateTime", which will return DateTime object(s). Other option
35 include "verbatim" (return the original text), or "epoch" (return
36 Unix timestamp).
37
38 time_zone
39 Only relevant when "format" is set to "DateTime".
40
41 Forces a particular time zone to be set (this actually matters, as
42 "tomorrow" on Monday at 11 PM means something different than
43 "tomorrow" on Tuesday at 1 AM).
44
45 By default it will use the "floating" time zone. See the
46 documentation for DateTime.
47
48 This controls both the input time zone and output time zone.
49
50 prefers
51 This argument decides what happens when an ambiguous date appears
52 in the input. For example, "Friday" may refer to any number of
53 Fridays. The valid options for this argument are:
54
55 nearest
56 Prefer the nearest date. This is the default.
57
58 future
59 Prefer the closest future date.
60
61 past
62 Prefer the closest past date. NOT YET SUPPORTED.
63
64 returns
65 If the text has multiple possible dates, then this argument
66 determines which date will be returned. By default it's 'first'.
67
68 first
69 Returns the first date found in the string.
70
71 last
72 Returns the final date found in the string.
73
74 earliest
75 Returns the date found in the string that chronologically
76 precedes any other date in the string.
77
78 latest
79 Returns the date found in the string that chronologically
80 follows any other date in the string.
81
82 all Returns all dates found in the string, in the order they were
83 found in the string.
84
85 all_cron
86 Returns all dates found in the string, in chronological order.
87
88 extract text, ARGS => dates
89 Takes an arbitrary amount of text and extracts one or more dates from
90 it. The return value will be zero or more dates, which by default are
91 DateTime objects (but can be customized with the "format" argument). If
92 called in scalar context, only one will be returned, even if the
93 "returns" argument specifies multiple possible return values.
94
95 See the documentation of "new" for the configuration of this method.
96 Any arguments passed into this method will trump those from the
97 constructor.
98
99 You may reuse a parser for multiple calls to "extract".
100
101 You do not need to have an instantiated "Date::Extract" object to call
102 this method. Just "Date::Extract->extract($foo)" will work.
103
105 · today; tomorrow; yesterday
106
107 · last Friday; next Monday; previous Sat
108
109 · Monday; Mon
110
111 · November 13th, 1986; Nov 13, 1986
112
113 · 13 November 1986; 13 Nov 1986
114
115 · November 13th; Nov 13
116
117 · 13 Nov; 13th November
118
119 · 1986/11/13; 1986-11-13
120
121 · 11-13-86; 11/13/1986
122
124 This module is intentionally very simple. Surprises are not welcome
125 here.
126
128 DateTime::Format::Natural, Time::ParseDate, Date::Manip
129
131 Shawn M Moore, "<sartak at bestpractical dot com>"
132
134 Thanks to Steven Schubiger for writing the fine
135 DateTime::Format::Natural. We still use it, but it doesn't quite fill
136 all the particular needs we have.
137
139 Copyright 2007-2009 Best Practical Solutions.
140
141 This program is free software; you can redistribute it and/or modify it
142 under the same terms as Perl itself.
143
144
145
146perl v5.32.0 2020-07-28 Date::Extract(3)