Alt-Date-Extract-SHARYANTO
view release on metacpan or search on metacpan
"Pod::Coverage::TrustPod" : "0",
"Test::Pod" : "1.41",
"Test::Pod::Coverage" : "1.08",
"Test::Rinci" : "0.01"
}
},
"runtime" : {
"requires" : {
"Alt::Base" : "0",
"Class::Data::Inheritable" : "0",
"DateTime::Format::Natural" : "0"
}
},
"test" : {
"requires" : {
"File::Spec" : "0",
"IO::Handle" : "0",
"IPC::Open3" : "0",
"Test::MockTime" : "0",
"Test::More" : "0"
}
dynamic_config: 0
generated_by: 'Dist::Zilla version 5.013, CPAN::Meta::Converter version 2.133380'
license: perl
meta-spec:
url: http://module-build.sourceforge.net/META-spec-v1.4.html
version: '1.4'
name: Alt-Date-Extract-SHARYANTO
requires:
Alt::Base: '0'
Class::Data::Inheritable: '0'
DateTime::Format::Natural: '0'
resources:
bugtracker: https://rt.cpan.org/Public/Dist/Display.html?Name=Alt-Date-Extract-SHARYANTO
homepage: https://metacpan.org/release/Alt-Date-Extract-SHARYANTO
repository: git://github.com/sharyanto/perl-Alt-Date-Extract-SHARYANTO.git
version: 0.05.01
x_Dist_Zilla:
perl:
version: '5.018002'
plugins:
-
Makefile.PL view on Meta::CPAN
"CONFIGURE_REQUIRES" => {
"ExtUtils::MakeMaker" => "6.30"
},
"DISTNAME" => "Alt-Date-Extract-SHARYANTO",
"EXE_FILES" => [],
"LICENSE" => "perl",
"NAME" => "Alt::Date::Extract::SHARYANTO",
"PREREQ_PM" => {
"Alt::Base" => 0,
"Class::Data::Inheritable" => 0,
"DateTime::Format::Natural" => 0
},
"TEST_REQUIRES" => {
"File::Spec" => 0,
"IO::Handle" => 0,
"IPC::Open3" => 0,
"Test::MockTime" => 0,
"Test::More" => 0
},
"VERSION" => "0.05.01",
"test" => {
"TESTS" => "t/*.t"
}
);
my %FallbackPrereqs = (
"Alt::Base" => 0,
"Class::Data::Inheritable" => 0,
"DateTime::Format::Natural" => 0,
"File::Spec" => 0,
"IO::Handle" => 0,
"IPC::Open3" => 0,
"Test::MockTime" => 0,
"Test::More" => 0
);
unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) {
delete $WriteMakefileArgs{TEST_REQUIRES};
[@Author::SHARYANTO]
:version=0.23
[Prereqs / TestRequires]
Test::MockTime=0
[Prereqs]
Alt::Base=0
Class::Data::Inheritable=0
DateTime::Format::Natural=0
lib/Date/Extract.pm view on Meta::CPAN
package Date::Extract;
use strict;
use warnings;
use DateTime::Format::Natural;
use List::Util 'reduce';
use parent 'Class::Data::Inheritable';
our $VERSION = '0.05.01'; # VERSION
our $DATE = '2014-06-09'; # DATE
__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'
&& $args{format} ne 'combined') {
_croak "Invalid `format` passed to constructor: expected `DateTime', `verbatim', `epoch', `combined'.";
}
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'.";
}
lib/Date/Extract.pm view on Meta::CPAN
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] },
});
}
lib/Date/Extract.pm view on Meta::CPAN
};
}
return (map {$_->{verbatim}} @combined) if $fmt 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);
for (@combined) {
my $dt = $parser->parse_datetime($_->{verbatim});
if ($parser->success) {
$dt->set_time_zone($args{time_zone});
$_->{DateTime} = $dt;
}
}
if ($fmt eq 'epoch') {
return map { $_->{DateTime}->epoch } @combined;
} elsif ($fmt eq 'combined') {
return @combined;
} else {
return map {$_->{DateTime}} @combined;
}
}
1;
__END__
=pod
=encoding UTF-8
lib/Date/Extract.pm view on Meta::CPAN
or die "No date found.";
return $dt->ymd;
=head1 NAME
Date::Extract - extract probable dates from strings
=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), "epoch" (return Unix timestamp), or "combined" (return
hashref containing these keys "verbatim", "DateTime", "pos" [position of date
string in the text]).
=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
lib/Date/Extract.pm view on Meta::CPAN
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
lib/Date/Extract.pm view on Meta::CPAN
=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
Steven Haryanto <stevenharyanto@gmail.com>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Steven Haryanto.
This is free software; you can redistribute it and/or modify it under
like($@, qr/01-new\.t/, "invalid `prefers` error reported from caller's perspective");
# }}}
# format {{{
$parser = eval { Date::Extract->new(format => "invalid argument") };
ok(!$parser, "did NOT get a parser out of Date::Extract->new(format => 'invalid argument')");
like($@, qr/Invalid `format` passed to constructor/, "(format => 'invalid argument') gave a sensible error message");
like($@, qr/01-new\.t/, "invalid `format` error reported from caller's perspective");
for my $opt (qw/DateTime verbatim epoch combined/) {
$parser = Date::Extract->new(format => $opt);
ok($parser, "got a parser out of Date::Extract->new(format => '$opt')");
ok($parser->isa("Date::Extract"), "new parser is a Date::Extract object");
}
# }}}
t/04-formats.t view on Meta::CPAN
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Date::Extract;
my %formats = (
'today' =>
sub { is($_->ymd, DateTime->today->ymd, "today") },
'tomorrow' =>
sub { is($_->ymd, DateTime->today->add(days => 1)->ymd, "tomorrow") },
'yesterday' =>
sub { is($_->ymd, DateTime->today->add(days => -1)->ymd, "yesterday") },
'last Friday' =>
sub {
is($_->day_name, 'Friday', "last Friday");
cmp_ok($_->epoch, '<', DateTime->today->epoch, "last Friday");
},
'next Monday' =>
sub {
is($_->day_name, 'Monday', "next Monday");
cmp_ok($_->epoch, '>', DateTime->today->epoch, "next Monday");
},
'previous Sat' => {
TODO => 'Not handled by us or DTFN yet',
test => sub {
is($_->day_name, 'Saturday', "previous Sat");
cmp_ok($_->epoch, '<', DateTime->today->epoch, "previous Sat");
},
},
'Monday' =>
sub { is($_->day_name, 'Monday', "Monday") },
'Mon' =>
sub { is($_->day_name, 'Monday', "Mon") },
'November 13th, 1986' =>
sub { is($_->ymd, '1986-11-13', "November 13th, 1986") },
'13 November 1986' =>
sub { is($_->ymd, '1986-11-13', "13 November 1986") },
'Nov 13, 1986' =>
sub { is($_->ymd, '1986-11-13', "Nov 13th, 1986") },
'November 13th' =>
sub { is($_->ymd, DateTime->today->year . '-11-13', "November 13th") },
'Nov 13' =>
sub { is($_->ymd, DateTime->today->year . '-11-13', "Nov 13") },
'13 Nov' =>
sub { is($_->ymd, DateTime->today->year . '-11-13', "13 Nov") },
'13th November' =>
sub { is($_->ymd, DateTime->today->year . '-11-13', "13th November") },
'1986/11/13' =>
sub { is($_->ymd, '1986-11-13', "1986/11/13") },
'1986-11-13' =>
sub { is($_->ymd, '1986-11-13', "1986-11-13") },
'11-13-86' => {
TODO => 'Not handled by us or DTFN yet',
test => sub { is($_->ymd, '1986-11-13', "11-13-86") },
},
'11/13/1986' => {
TODO => 'Not handled by us or DTFN yet',
t/05-format.t view on Meta::CPAN
use Test::MockTime 'set_fixed_time';
use Date::Extract;
# a Thursday. The time I wrote this line of code, in fact (in UTC)
set_fixed_time('2007-11-16T02:48:52Z');
my $in = "Today I see a boat. Tomorrow I'll see another. Yesterday I swam.";
my $parser;
$parser = Date::Extract->new(format => 'DateTime');
my $dt = $parser->extract($in);
is($dt->ymd, '2007-11-16', 'default: returns DateTime object');
$parser = Date::Extract->new(format => 'verbatim');
my $verbatim = $parser->extract($in);
is($verbatim, 'Today', 'returns verbatim text');
$parser = Date::Extract->new(format => 'epoch');
my $epoch = $parser->extract($in);
is($epoch, 1195171200, 'returns epoch');
$parser = Date::Extract->new(format => 'combined');
my $hash = $parser->extract($in);
is(ref($hash), 'HASH', 'returns hash');
is($hash->{verbatim}, 'Today', 'hash contains verbatim');
is($hash->{DateTime}->ymd, '2007-11-16', 'hash contains DateTime object');
is($hash->{pos}, 0, 'hash contains pos');
t/10-misc.t view on Meta::CPAN
#!perl -T
use strict;
use warnings;
use Test::More tests => 2;
use Date::Extract;
my $parser = Date::Extract->new;
my $dt = $parser->extract("writing a test today!");
is($dt->time_zone->name, 'floating', 'default time zone is floating');
is($dt->ymd, DateTime->today(time_zone => 'floating')->ymd, 'extracted the date as today');
( run in 0.423 second using v1.01-cache-2.11-cpan-05444aca049 )