File Coverage

blib/lib/DateTime/Format/Natural/Compat.pm
Criterion Covered Total %
statement 71 72 98.6
branch 17 18 94.4
condition n/a
subroutine 13 13 100.0
pod n/a
total 101 103 98.0


line stmt bran cond sub pod time code
1             package DateTime::Format::Natural::Compat;
2              
3 26     26   208 use strict;
  26         57  
  26         732  
4 26     26   146 use warnings;
  26         66  
  26         874  
5 26     26   6304 use boolean qw(true false);
  26         48698  
  26         193  
6              
7 26     26   24758 use DateTime ();
  26         13260083  
  26         3253  
8              
9             our ($VERSION, $Pure);
10              
11             $VERSION = '0.07';
12              
13             BEGIN
14             {
15 26 50   26   1757 if (eval "require Date::Calc") {
16 26         178685 Date::Calc->import(qw(
17             Add_Delta_Days
18             Day_of_Week
19             Days_in_Month
20             Decode_Day_of_Week
21             Decode_Month
22             Nth_Weekday_of_Month_Year
23             check_date
24             check_time
25             ));
26 26         244 $Pure = false;
27             }
28             else {
29 0         0 $Pure = true;
30             }
31             }
32              
33             sub _Add_Delta_Days
34             {
35 132     132   1104 my $self = shift;
36              
37 132 100       409 if ($Pure) {
38 60         538 my ($year, $day) = @_;
39 60         243 my $dt = DateTime->from_day_of_year(year => $year, day_of_year => $day);
40 60         21364 return ($dt->year, $dt->month, $dt->mday);
41             }
42             else {
43 72         675 my ($year, $day) = @_;
44 72         513 return Add_Delta_Days($year, 1, 1, $day - 1);
45             }
46             }
47              
48             sub _Day_of_Week
49             {
50 1530     1530   23767 my $self = shift;
51              
52 1530 100       3466 if ($Pure) {
53 765         6588 return $self->{datetime}->wday;
54             }
55             else {
56 765         8785 return Day_of_Week(@_);
57             }
58             }
59              
60             sub _Days_in_Month
61             {
62 105     105   622 my $self = shift;
63              
64 105 100       328 if ($Pure) {
65 45         365 my ($year, $month) = @_;
66 45         213 my $dt = DateTime->last_day_of_month(year => $year, month => $month);
67 45         15944 return $dt->day;
68             }
69             else {
70 60         700 return Days_in_Month(@_);
71             }
72             }
73              
74             sub _Decode_Day_of_Week
75             {
76 3645     3645   6302 my $self = shift;
77              
78 3645 100       7987 if ($Pure) {
79 1767         12950 my ($day) = @_;
80 1767         8645 return $self->{data}->{weekdays}->{$day};
81             }
82             else {
83 1878         21677 return Decode_Day_of_Week(@_);
84             }
85             }
86              
87             sub _Decode_Month
88             {
89 3030     3030   5431 my $self = shift;
90              
91 3030 100       6809 if ($Pure) {
92 1452         10979 my ($month) = @_;
93 1452         7084 return $self->{data}->{months}->{$month};
94             }
95             else {
96 1578         18339 return Decode_Month(@_);
97             }
98             }
99              
100             sub _Nth_Weekday_of_Month_Year
101             {
102 228     228   2384 my $self = shift;
103              
104 228 100       608 if ($Pure) {
105 114         1030 my ($year, $month, $weekday, $count) = @_;
106 114         405 my $dt = $self->{datetime}->clone;
107 114         1833 $dt->set_year($year);
108 114         59287 $dt->set_month($month);
109 114         53500 $dt->set_day(1);
110 114         53026 $dt->set_day($dt->day + 1)
111             while ($weekday ne $dt->dow);
112 114         196339 $dt->set_day($dt->day + 7 * ($count - 1));
113 114         51959 return ($dt->year, $dt->month, $dt->day);
114             }
115             else {
116 114         1360 return Nth_Weekday_of_Month_Year(@_);
117             }
118             }
119              
120             sub _check_date
121             {
122 4011     4011   9629 my $self = shift;
123              
124 4011 100       11782 if ($Pure) {
125 1932         16790 my ($year, $month, $day) = @_;
126 1932         3555 local $@;
127 1932         4382 eval {
128 1932         6275 my $dt = $self->{datetime}->clone;
129 1932         27867 $dt->set(year => $year, month => $month, day => $day);
130             };
131 1932         1020087 return !$@;
132             }
133             else {
134 2079         23253 return check_date(@_);
135             }
136             }
137              
138             sub _check_time
139             {
140 9058     9058   18588 my $self = shift;
141              
142 9058 100       29303 if ($Pure) {
143 4467         38988 my ($hour, $minute, $second) = @_;
144 4467         8200 local $@;
145 4467         9437 eval {
146 4467         14267 my $dt = $self->{datetime}->clone;
147 4467         59438 $dt->set(hour => $hour, minute => $minute, second => $second);
148             };
149 4467         2355866 return !$@;
150             }
151             else {
152 4591         52044 return check_time(@_);
153             }
154             }
155              
156             1;
157             __END__
158              
159             =head1 NAME
160              
161             DateTime::Format::Natural::Compat - Methods with more than one implementation
162              
163             =head1 SYNOPSIS
164              
165             Please see the DateTime::Format::Natural documentation.
166              
167             =head1 DESCRIPTION
168              
169             The C<DateTime::Format::Natural::Compat> class defines methods which must retain
170             more than one possible implementation due to compatibility issues on certain
171             platforms.
172              
173             =head1 SEE ALSO
174              
175             L<DateTime::Format::Natural>
176              
177             =head1 AUTHOR
178              
179             Steven Schubiger <schubiger@cpan.org>
180              
181             =head1 LICENSE
182              
183             This program is free software; you may redistribute it and/or
184             modify it under the same terms as Perl itself.
185              
186             See L<http://dev.perl.org/licenses/>
187              
188             =cut