/usr/share/perl5/Date/Extract.pm is in libdate-extract-perl 0.06-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 | package Date::Extract;
use strict;
use warnings;
use DateTime::Format::Natural;
use List::Util 'reduce';
use parent 'Class::Data::Inheritable';
our $VERSION = '0.06';
__PACKAGE__->mk_classdata($_) for qw/scalar_downgrade handlers regex/;
sub _croak {
require Carp;
Carp::croak @_;
}
sub new {
my $class = shift;
my %args = (
format => 'DateTime',
returns => 'first',
prefers => 'nearest',
time_zone => 'floating',
@_,
);
if ($args{format} ne 'DateTime'
&& $args{format} ne 'verbatim'
&& $args{format} ne 'epoch') {
_croak "Invalid `format` passed to constructor: expected `DateTime', `verbatim', `epoch'.";
}
if ($args{returns} ne 'first'
&& $args{returns} ne 'last'
&& $args{returns} ne 'earliest'
&& $args{returns} ne 'latest'
&& $args{returns} ne 'all'
&& $args{returns} ne 'all_cron') {
_croak "Invalid `returns` passed to constructor: expected `first', `last', `earliest', `latest', `all', or `all_cron'.";
}
if ($args{prefers} ne 'nearest'
&& $args{prefers} ne 'past'
&& $args{prefers} ne 'future') {
_croak "Invalid `prefers` passed to constructor: expected `nearest', `past', or `future'.";
}
my $self = bless \%args, ref($class) || $class;
return $self;
}
# This method will combine the arguments of parser->new and extract. Modify the
# "to" hash directly.
sub _combine_args {
shift;
my $from = shift;
my $to = shift;
$to->{format} ||= $from->{format};
$to->{prefers} ||= $from->{prefers};
$to->{returns} ||= $from->{returns};
$to->{time_zone} ||= $from->{time_zone};
}
sub extract {
my $self = shift;
my $text = shift;
my %args = @_;
# using extract as a class method
$self = $self->new
if !ref($self);
# combine the arguments of parser->new and this
$self->_combine_args($self, \%args);
# when in scalar context, downgrade
$args{returns} = $self->_downgrade($args{returns})
unless wantarray;
# do the work
my @ret = $self->_extract($text, %args);
# munge the output to match the desired return type
return $self->_handle($args{returns}, @ret);
}
# build the giant regex used for parsing. it has to be a single regex, so that
# the order of matches is correct.
sub _build_regex {
my $self = shift;
my $relative = '(?:today|tomorrow|yesterday)';
my $long_weekday = '(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)';
my $short_weekday = '(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)';
my $weekday = "(?:$long_weekday|$short_weekday)";
my $relative_weekday = "(?:(?:next|previous|last)\\s*$weekday)";
my $long_month = '(?:January|February|March|April|May|June|July|August|September|October|November|December)';
my $short_month = '(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)';
my $month = "(?:$long_month|$short_month)";
# 1 - 31
my $cardinal_monthday = "(?:[1-9]|[12][0-9]|3[01])";
my $monthday = "(?:$cardinal_monthday(?:st|nd|rd|th)?)";
my $day_month = "(?:$monthday\\s*$month)";
my $month_day = "(?:$month\\s*$monthday)";
my $day_month_year = "(?:(?:$day_month|$month_day)\\s*,?\\s*\\d\\d\\d\\d)";
my $yyyymmdd = "(?:\\d\\d\\d\\d[-/]\\d\\d[-/]\\d\\d)";
my $ddmmyy = "(?:\\d\\d[-/]\\d\\d[-/]\\d\\d)";
my $ddmmyyyy = "(?:\\d\\d[-/]\\d\\d[-/]\\d\\d\\d\\d)";
my $other = $self->_build_more_regex;
$other = "|$other"
if $other;
my $regex = qr{
\b(
$relative # today
| $relative_weekday # last Friday
| $weekday # Monday
| $day_month_year # November 13th, 1986
| $day_month # November 13th
| $month_day # 13 Nov
| $yyyymmdd # 1986/11/13
| $ddmmyy # 11-13-86
| $ddmmyyyy # 11-13-1986
$other # anything from the subclass
)\b
}ix;
$self->regex($regex);
}
# this is to be used in subclasses for adding more stuff to the regex
# for example, to add support for $foo_bar and $baz_quux, return
# "$foo_bar|$baz_quux"
sub _build_more_regex { '' }
# build the list->scalar downgrade types
sub _build_scalar_downgrade {
my $self = shift;
$self->scalar_downgrade({
all => 'first',
all_cron => 'earliest',
});
}
# build the handlers that munge the list of dates to the desired order
sub _build_handlers {
my $self = shift;
$self->handlers({
all_cron => sub {
sort { DateTime->compare_ignore_floating($a, $b) } @_
},
all => sub { @_ },
earliest => sub { reduce { $a < $b ? $a : $b } @_ },
latest => sub { reduce { $a > $b ? $a : $b } @_ },
first => sub { $_[0] },
last => sub { $_[-1] },
});
}
# actually perform the scalar downgrade
sub _downgrade {
my $self = shift;
my $returns = shift;
my $downgrades = $self->scalar_downgrade || $self->_build_scalar_downgrade;
return $downgrades->{$returns} || $returns;
}
sub _handle {
my $self = shift;
my $returns = shift;
my $handlers = $self->handlers || $self->_build_handlers;
my $handler = $handlers->{$returns};
return defined $handler ? $handler->(@_) : @_
}
sub _extract {
my $self = shift;
my $text = shift;
my %args = @_;
my $regex = $self->regex || $self->_build_regex;
my @gleaned = $text =~ /$regex/g;
return @gleaned if $self->{format} eq 'verbatim';
my %dtfn_args;
$dtfn_args{prefer_future} = 1
if $args{prefers} && $args{prefers} eq 'future';
$dtfn_args{time_zone} = $args{time_zone};
my $parser = DateTime::Format::Natural->new(%dtfn_args);
my @ret;
for (@gleaned) {
my $dt = $parser->parse_datetime($_);
push @ret, $dt->set_time_zone($args{time_zone})
if $parser->success;
}
if ($self->{format} eq 'epoch') {
return map { $_->epoch } @ret;
}
return @ret;
}
1;
__END__
=head1 NAME
Date::Extract - extract probable dates from strings
=head1 SYNOPSIS
my $parser = Date::Extract->new();
my $dt = $parser->extract($arbitrary_text)
or die "No date found.";
return $dt->ymd;
=head1 MOTIVATION
There are already a few modules for getting a date out of a string.
L<DateTime::Format::Natural> should be your first choice. There's also
L<Time::ParseDate> which fits many formats. Finally, you can coerce
L<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 L<DateTime> object with the year
set to 2019. This is what your users would probably expect.
=head1 METHODS
=head2 new PARAMHASH => C<Date::Extract>
=head3 arguments
=over 4
=item format
Choose what format the extracted date(s) will be. The default is "DateTime",
which will return L<DateTime> object(s). Other option include "verbatim" (return
the original text), or "epoch" (return Unix timestamp).
=item time_zone
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
L<DateTime>.
This controls both the input time zone and output time zone.
=item prefers
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:
=over 4
=item nearest
Prefer the nearest date. This is the default.
=item future
Prefer the closest future date.
=item past
Prefer the closest past date. B<NOT YET SUPPORTED>.
=back
=item returns
If the text has multiple possible dates, then this argument determines which
date will be returned. By default it's 'first'.
=over 4
=item first
Returns the first date found in the string.
=item last
Returns the final date found in the string.
=item earliest
Returns the date found in the string that chronologically precedes any other
date in the string.
=item latest
Returns the date found in the string that chronologically follows any other
date in the string.
=item all
Returns all dates found in the string, in the order they were found in the
string.
=item all_cron
Returns all dates found in the string, in chronological order.
=back
=back
=head2 extract text, ARGS => dates
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 L<DateTime>
objects (but can be customized with the C<format> argument). If called in scalar
context, only one will be returned, even if the C<returns> argument specifies
multiple possible return values.
See the documentation of C<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 C<extract>.
You do not need to have an instantiated C<Date::Extract> object to call this
method. Just C<< Date::Extract->extract($foo) >> will work.
=head1 FORMATS HANDLED
=over 4
=item * today; tomorrow; yesterday
=item * last Friday; next Monday; previous Sat
=item * Monday; Mon
=item * November 13th, 1986; Nov 13, 1986
=item * 13 November 1986; 13 Nov 1986
=item * November 13th; Nov 13
=item * 13 Nov; 13th November
=item * 1986/11/13; 1986-11-13
=item * 11-13-86; 11/13/1986
=back
=head1 CAVEATS
This module is I<intentionally> very simple. Surprises are I<not> welcome
here.
=head1 SEE ALSO
L<DateTime::Format::Natural>, L<Time::ParseDate>, L<Date::Manip>
=head1 AUTHOR
Shawn M Moore, C<< <sartak at bestpractical dot com> >>
=head1 ACKNOWLEDGEMENTS
Thanks to Steven Schubiger for writing the fine L<DateTime::Format::Natural>.
We still use it, but it doesn't quite fill all the particular needs we have.
=head1 COPYRIGHT & LICENSE
Copyright 2007-2009 Best Practical Solutions.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
|