[Bps-public-commit] Date-Extract branch, master, updated. daa9b179b092fd9381ab89977b76ef5aa5dceb86

Alex Vandiver alexmv at bestpractical.com
Mon Aug 11 14:15:13 EDT 2014


The branch, master has been updated
       via  daa9b179b092fd9381ab89977b76ef5aa5dceb86 (commit)
       via  94e82730802647941614fc215d8a36f349c2af9c (commit)
       via  0b5a03d279a33eef370e80e21b7232b6f5b6c300 (commit)
       via  8a450ac51b91fe8ded723e13ddef7dee91a16aa2 (commit)
       via  6629752caa688460a54cabc8deac621263d885cc (commit)
      from  049281057785cdf511b7e456069cd3f0fb2a8d13 (commit)

Summary of changes:
 Changes             |  5 +++++
 lib/Date/Extract.pm | 45 ++++++++++++++++++++++++++++++++++-----------
 t/01-new.t          | 14 +++++++++++++-
 t/04-formats.t      |  2 ++
 t/05-format.t       | 25 +++++++++++++++++++++++++
 5 files changed, 79 insertions(+), 12 deletions(-)
 create mode 100644 t/05-format.t

- Log -----------------------------------------------------------------
commit 6629752caa688460a54cabc8deac621263d885cc
Author: Steven Haryanto (on PC, Jakarta) <stevenharyanto at gmail.com>
Date:   Thu Jun 5 12:29:23 2014 +0700

    Handle 'dd mmm yyyy' (day month year without comma)

diff --git a/lib/Date/Extract.pm b/lib/Date/Extract.pm
index f57d1ad..16f4c7b 100644
--- a/lib/Date/Extract.pm
+++ b/lib/Date/Extract.pm
@@ -103,7 +103,7 @@ sub _build_regex {
 
     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 $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)";
diff --git a/t/04-formats.t b/t/04-formats.t
index d49237a..9bb5997 100644
--- a/t/04-formats.t
+++ b/t/04-formats.t
@@ -34,6 +34,8 @@ my %formats = (
         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' =>

commit 8a450ac51b91fe8ded723e13ddef7dee91a16aa2
Author: Steven Haryanto (on PC, Jakarta) <stevenharyanto at gmail.com>
Date:   Thu Jun 5 12:32:01 2014 +0700

    Handle 'dd mmm yyyy' (day month year without comma)

diff --git a/lib/Date/Extract.pm b/lib/Date/Extract.pm
index f57d1ad..67b983c 100644
--- a/lib/Date/Extract.pm
+++ b/lib/Date/Extract.pm
@@ -103,7 +103,7 @@ sub _build_regex {
 
     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 $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)";
@@ -341,6 +341,8 @@ method. Just C<< Date::Extract->extract($foo) >> will work.
 
 =item * November 13th, 1986; Nov 13, 1986
 
+=item * 13 November 1986; 13 Nov 1986
+
 =item * November 13th; Nov 13
 
 =item * 13 Nov; 13th November
diff --git a/t/04-formats.t b/t/04-formats.t
index d49237a..9bb5997 100644
--- a/t/04-formats.t
+++ b/t/04-formats.t
@@ -34,6 +34,8 @@ my %formats = (
         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' =>

commit 0b5a03d279a33eef370e80e21b7232b6f5b6c300
Merge: 8a450ac 6629752
Author: Steven Haryanto (on PC, Jakarta) <stevenharyanto at gmail.com>
Date:   Thu Jun 5 12:32:25 2014 +0700

    Merge branch 'master' of github.com:sharyanto/operl-Date-Extract


commit 94e82730802647941614fc215d8a36f349c2af9c
Author: Steven Haryanto (on PC, Jakarta) <stevenharyanto at gmail.com>
Date:   Thu Jun 5 12:51:11 2014 +0700

    Support format argument [RT#95998]

diff --git a/lib/Date/Extract.pm b/lib/Date/Extract.pm
index 67b983c..62c42de 100644
--- a/lib/Date/Extract.pm
+++ b/lib/Date/Extract.pm
@@ -17,12 +17,19 @@ sub _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'
@@ -52,6 +59,7 @@ sub _combine_args {
     my $from = shift;
     my $to = shift;
 
+    $to->{format}    ||= $from->{format};
     $to->{prefers}   ||= $from->{prefers};
     $to->{returns}   ||= $from->{returns};
     $to->{time_zone} ||= $from->{time_zone};
@@ -189,6 +197,8 @@ sub _extract {
     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';
@@ -202,6 +212,9 @@ sub _extract {
             if $parser->success;
     }
 
+    if ($self->{format} eq 'epoch') {
+        return map { $_->epoch } @ret;
+    }
     return @ret;
 }
 
@@ -227,13 +240,12 @@ 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 build a L<DateTime> object
-out of 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.
+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
 
@@ -243,8 +255,16 @@ would probably expect.
 
 =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).
@@ -314,10 +334,11 @@ Returns all dates found in the string, in chronological order.
 
 =back
 
-=head2 extract text, ARGS => C<DateTime>s
+=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 C<DateTime> objects. If called in scalar
+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.
 
diff --git a/t/01-new.t b/t/01-new.t
index 9c6cf08..4e29bf2 100644
--- a/t/01-new.t
+++ b/t/01-new.t
@@ -1,7 +1,7 @@
 #!perl -T
 use strict;
 use warnings;
-use Test::More tests => 26;
+use Test::More tests => 35;
 use Date::Extract;
 
 my $parser = Date::Extract->new();
@@ -35,3 +35,15 @@ like($@, qr/01-new\.t/, "invalid `prefers` error reported from caller's perspect
 
 # }}}
 
+# 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/) {
+    $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");
+}
+# }}}
diff --git a/t/05-format.t b/t/05-format.t
new file mode 100644
index 0000000..0b43f8b
--- /dev/null
+++ b/t/05-format.t
@@ -0,0 +1,25 @@
+#!perl -T
+use strict;
+use warnings;
+use Test::More tests => 3;
+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');

commit daa9b179b092fd9381ab89977b76ef5aa5dceb86
Author: Steven Haryanto (on PC, Jakarta) <stevenharyanto at gmail.com>
Date:   Thu Jun 5 12:55:36 2014 +0700

    Changes and version bump to 0.05

diff --git a/Changes b/Changes
index cf08478..f58db64 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,10 @@
 Revision history for Date-Extract
 
+0.05    Thu Jun  5 12:52:12 WIB 2014
+        This release is done by SHARYANTO
+        Add 'format' argument (RT#95998)
+        Support '13 Nov 1986' format (RT#95999)
+
 0.04    Wed Nov 18 19:49:23 2009
         Improve Date::Extract->extract default handling
         Documentation improvements
diff --git a/lib/Date/Extract.pm b/lib/Date/Extract.pm
index 62c42de..6c4d73b 100644
--- a/lib/Date/Extract.pm
+++ b/lib/Date/Extract.pm
@@ -5,7 +5,7 @@ use DateTime::Format::Natural;
 use List::Util 'reduce';
 use parent 'Class::Data::Inheritable';
 
-our $VERSION = '0.04';
+our $VERSION = '0.05';
 
 __PACKAGE__->mk_classdata($_) for qw/scalar_downgrade handlers regex/;
 

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list