CC: | Jesse Vincent <jesse [...] bestpractical.com> |
Subject: | [PATCH] Remove dependency on Date::Calc in favor of using DateTime native functions |
Date: | Sun, 22 Feb 2009 21:11:45 -0500 |
To: | bug-DateTime-Format-Natural [...] rt.cpan.org |
From: | Jesse Vincent <jesse [...] bestpractical.com> |
This removes two XS modules which haven't been updated in 4+ years
from this package's dependency chain, one of which won't build on
the Perl shipped with OS X 10.5.
---
Build.PL | 1 -
Changes | 3 +
Makefile.PL | 1 -
lib/DateTime/Format/Natural.pm | 17 ++++---
lib/DateTime/Format/Natural/Base.pm | 82 ++++++++++++++++------------------
5 files changed, 52 insertions(+), 52 deletions(-)
diff --git a/Build.PL b/Build.PL
index edfe983..fc27aba 100644
--- a/Build.PL
+++ b/Build.PL
@@ -11,7 +11,6 @@ my $build = Module::Build->new
dist_author => 'Steven Schubiger <schubiger@cpan.org>',
dist_version_from => 'lib/DateTime/Format/Natural.pm',
requires => {
- 'Date::Calc' => 0,
'DateTime' => 0,
'List::MoreUtils' => 0,
'Params::Validate' => 0,
diff --git a/Changes b/Changes
index 95fa491..dd9377a 100644
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
Revision history for Perl extension DateTime::Format::Natural.
+ - Remove dependency on Date::Calc
+ [Jesse Vincent <jesse@bestpractical.com]
+
0.71 Thu Jun 12 11:17:22 CEST 2008
- When processing "date abbreviations" call Date::Calc's check_date()
diff --git a/Makefile.PL b/Makefile.PL
index ad132c0..1196859 100644
--- a/Makefile.PL
+++ b/Makefile.PL
@@ -12,7 +12,6 @@ WriteMakefile
'PREREQ_PM' => {
'Test::More' => 0,
'DateTime' => 0,
- 'Date::Calc' => 0,
'Term::ReadLine' => 0,
'Params::Validate' => 0,
'Test::MockTime' => 0,
diff --git a/lib/DateTime/Format/Natural.pm b/lib/DateTime/Format/Natural.pm
index feb0abe..731e5db 100644
--- a/lib/DateTime/Format/Natural.pm
+++ b/lib/DateTime/Format/Natural.pm
@@ -6,7 +6,6 @@ use base qw(DateTime::Format::Natural::Base);
use Carp qw(croak);
use DateTime ();
-use Date::Calc qw(Day_of_Week check_date);
use List::MoreUtils qw(all any);
use Params::Validate ':all';
@@ -158,15 +157,19 @@ sub parse_datetime
if ($year > $century) { $century-- };
if (length $year == 2) { $year = "$century$year" };
- unless (check_date($year, $month, $day)) {
+ eval {
+ $self->{datetime}->set_year($year);
+ $self->{datetime}->set_month($month);
+ $self->{datetime}->set_day($day);
+ };
+ if ($@) {
$self->_set_failure;
$self->_set_error("(invalid date)");
return $self->_get_datetime_object;
+
}
- $self->{datetime}->set_year($year);
- $self->{datetime}->set_month($month);
- $self->{datetime}->set_day($day);
+
$self->_set_valid_exp;
$self->_set_modified(1);
@@ -342,7 +345,7 @@ sub _post_process_options
&& (any { $self->{tokens}->[0] =~ /$_/i } @{$self->{data}->{weekdays_all}})
&& scalar keys %modified == 1
&& (exists $self->{modified}{day} && $self->{modified}{day} == 1
- && Day_of_Week($self->{datetime}->year, $self->{datetime}->month, $self->{datetime}->day)
+ && $self->{datetime}->wday
< DateTime->now(time_zone => $self->{Time_zone})->wday)
) {
$self->{postprocess}{day} = 7;
@@ -600,7 +603,7 @@ valuable suggestions & patches:
=head1 SEE ALSO
-L<DateTime>, L<Date::Calc>, L<http://datetime.perl.org>
+L<DateTime>, L<http://datetime.perl.org>
=head1 AUTHOR
diff --git a/lib/DateTime/Format/Natural/Base.pm b/lib/DateTime/Format/Natural/Base.pm
index 15dcfea..fcee955 100644
--- a/lib/DateTime/Format/Natural/Base.pm
+++ b/lib/DateTime/Format/Natural/Base.pm
@@ -4,10 +4,6 @@ use strict;
use warnings;
use DateTime ();
-use Date::Calc qw(Add_Delta_Days
- Decode_Day_of_Week
- Nth_Weekday_of_Month_Year
- check_date check_time);
our $VERSION = '1.11';
@@ -526,7 +522,7 @@ sub _next_weekday
if (length $day == 3) {
$day = $self->{data}->{weekdays_abbrev}->{$day};
}
- my $days_diff = (7 - $self->{datetime}->wday + Decode_Day_of_Week($day));
+ my $days_diff = (7 - $self->{datetime}->wday + $self->{data}->{weekdays}->{$day});
$self->_add(day => $days_diff);
$self->_set_modified(2);
}
@@ -540,7 +536,7 @@ sub _weekday_next_week
if (length $day == 3) {
$day = $self->{data}->{weekdays_abbrev}->{$day};
}
- my $days_diff = (7 - $self->{datetime}->wday + Decode_Day_of_Week($day));
+ my $days_diff = (7 - $self->{datetime}->wday + $self->{data}->{weekdays}->{$day});
$self->_add(day => $days_diff);
$self->_set_modified(3);
}
@@ -649,7 +645,7 @@ sub _weekday_this_week
$self->_add_trace;
my ($day) = @_;
$day = ucfirst lc $day;
- my $days_diff = Decode_Day_of_Week($day) - $self->{datetime}->wday;
+ my $days_diff = $self->{data}->{weekdays}->{$day} - $self->{datetime}->wday;
$self->_add(day => $days_diff);
$self->_set_modified(3);
}
@@ -674,18 +670,31 @@ sub _count_weekday_this_month
if (length $month == 3) {
$month = $self->{data}->{months_abbrev}->{$month};
}
- my $year;
- ($year, $month, $day) =
- Nth_Weekday_of_Month_Year($self->{datetime}->year,
- $self->{data}->{months}->{$month},
- $self->{data}->{weekdays}->{$day},
- $count);
- $self->_set(year => $year);
- $self->_set(month => $month);
- $self->_set(day => $day);
+
+ $month = $self->{data}->{months}->{$month};
+ my ($new_year,$new_month,$new_day) = $self->_nth_weekday_of_month($month, $day, $count);
+
+ $self->_set(year => $new_year);
+ $self->_set(month => $new_month);
+ $self->_set(day => $new_day);
$self->_set_modified(4);
}
+sub _nth_weekday_of_month {
+ my $self = shift;
+ my $month = shift;
+ my $weekday = shift;
+ my $count = shift;
+
+ my $work = $self->{datetime}->clone;
+ $work->set_month($month);
+ $work->set_day(1);
+ $work->set_day($work->day +1 ) while ($self->{data}->{weekdays}->{$weekday} ne $work->dow);
+ $work->set_day( $work->day + 7*($count-1));
+ return ($work->year,$work->month, $work->day);
+
+}
+
sub _daytime_variant_before_yesterday
{
my $self = shift;
@@ -811,10 +820,10 @@ sub _count_yearday
$self->_add_trace;
my ($day) = @_;
my ($year, $month);
- ($year, $month, $day) = Add_Delta_Days($self->{datetime}->year, 1, 1, $day - 1);
- $self->_set(day => $day);
- $self->{datetime}->set_month($month);
- $self->{datetime}->set_year($year);
+ my $w = DateTime->from_day_of_year(year => $self->{datetime}->year, day_of_year => $day);
+ $self->_set(day => $w->mday);
+ $self->{datetime}->set_month($w->month);
+ $self->{datetime}->set_year($w->year);
$self->_set_modified(2);
}
@@ -824,10 +833,9 @@ sub _count_weekday
$self->_add_trace;
my ($count, $weekday) = @_;
$weekday = ucfirst lc $weekday;
- my ($year, $month, $day) =
- Nth_Weekday_of_Month_Year($self->{datetime}->year,
+ my ($year, $month, $day) = $self->_nth_weekday_of_month(
$self->{datetime}->month,
- $self->{data}->{weekdays}->{$weekday},
+ $weekday,
$count);
if ($self->_valid_date(day => $day, month => $month, year => $year)) {
$self->_set(day => $day);
@@ -870,35 +878,23 @@ sub _set
sub _valid_date
{
my ($self, $type, $value) = @_;
+ my $x = $self->{datetime}->clone;
- my %set = map { $_ => $self->{datetime}->$_ } qw(year month day);
- $set{$type} = $value;
-
- if (check_date($set{year}, $set{month}, $set{day})) {
- return 1;
- }
- else {
+ my $method = "set_$type";
+ eval {$x->$method($value)};
+
+ if ($@) {
$self->_set_failure;
$self->_set_error("('$value' is not a valid $type)");
return 0;
+ } else {
+ return 1;
}
}
sub _valid_time
{
- my ($self, $type, $value) = @_;
-
- my %set = map { $_ => $self->{datetime}->$_ } qw(hour min sec);
- $set{$type} = $value;
-
- if (check_time($set{hour}, $set{min}, $set{sec})) {
- return 1;
- }
- else {
- $self->_set_failure;
- $self->_set_error("('$value' is not a valid $type)");
- return 0;
- }
+ shift->_valid_date(@_);
}
1;
--
1.6.1