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   216 use strict;
  26         49  
  26         1004  
4 26     26   173 use warnings;
  26         54  
  26         1475  
5 26     26   6360 use boolean qw(true false);
  26         66261  
  26         163  
6              
7 26     26   42365 use DateTime ();
  26         15440166  
  26         4202  
8              
9             our ($VERSION, $Pure);
10              
11             $VERSION = '0.07';
12              
13             BEGIN
14             {
15 26 50   26   2111 if (eval "require Date::Calc") {
16 26         256086 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         183 $Pure = false;
27             }
28             else {
29 0         0 $Pure = true;
30             }
31             }
32              
33             sub _Add_Delta_Days
34             {
35 132     132   1893 my $self = shift;
36              
37 132 100       599 if ($Pure) {
38 60         624 my ($year, $day) = @_;
39 60         417 my $dt = DateTime->from_day_of_year(year => $year, day_of_year => $day);
40 60         31720 return ($dt->year, $dt->month, $dt->mday);
41             }
42             else {
43 72         794 my ($year, $day) = @_;
44 72         878 return Add_Delta_Days($year, 1, 1, $day - 1);
45             }
46             }
47              
48             sub _Day_of_Week
49             {
50 1530     1530   29304 my $self = shift;
51              
52 1530 100       5313 if ($Pure) {
53 765         10230 return $self->{datetime}->wday;
54             }
55             else {
56 765         11427 return Day_of_Week(@_);
57             }
58             }
59              
60             sub _Days_in_Month
61             {
62 123     123   1185 my $self = shift;
63              
64 123 100       491 if ($Pure) {
65 54         620 my ($year, $month) = @_;
66 54         369 my $dt = DateTime->last_day_of_month(year => $year, month => $month);
67 54         30385 return $dt->day;
68             }
69             else {
70 69         881 return Days_in_Month(@_);
71             }
72             }
73              
74             sub _Decode_Day_of_Week
75             {
76 3645     3645   7734 my $self = shift;
77              
78 3645 100       11344 if ($Pure) {
79 1767         17996 my ($day) = @_;
80 1767         14577 return $self->{data}->{weekdays}->{$day};
81             }
82             else {
83 1878         31072 return Decode_Day_of_Week(@_);
84             }
85             }
86              
87             sub _Decode_Month
88             {
89 3078     3078   6817 my $self = shift;
90              
91 3078 100       9570 if ($Pure) {
92 1476         14501 my ($month) = @_;
93 1476         11730 return $self->{data}->{months}->{$month};
94             }
95             else {
96 1602         27303 return Decode_Month(@_);
97             }
98             }
99              
100             sub _Nth_Weekday_of_Month_Year
101             {
102 228     228   3368 my $self = shift;
103              
104 228 100       895 if ($Pure) {
105 114         1310 my ($year, $month, $weekday, $count) = @_;
106 114         557 my $dt = $self->{datetime}->clone;
107 114         2602 $dt->set_year($year);
108 114         86335 $dt->set_month($month);
109 114         68362 $dt->set_day(1);
110 114         68783 $dt->set_day($dt->day + 1)
111             while ($weekday ne $dt->dow);
112 114         266273 $dt->set_day($dt->day + 7 * ($count - 1));
113 114         65912 return ($dt->year, $dt->month, $dt->day);
114             }
115             else {
116 114         1729 return Nth_Weekday_of_Month_Year(@_);
117             }
118             }
119              
120             sub _check_date
121             {
122 4088     4088   11942 my $self = shift;
123              
124 4088 100       17777 if ($Pure) {
125 1962         22551 my ($year, $month, $day) = @_;
126 1962         5025 local $@;
127 1962         5543 eval {
128 1962         9612 my $dt = $self->{datetime}->clone;
129 1962         39755 $dt->set(year => $year, month => $month, day => $day);
130             };
131 1962         1378721 return !$@;
132             }
133             else {
134 2126         34008 return check_date(@_);
135             }
136             }
137              
138             sub _check_time
139             {
140 9252     9252   21535 my $self = shift;
141              
142 9252 100       41304 if ($Pure) {
143 4554         51990 my ($hour, $minute, $second) = @_;
144 4554         11343 local $@;
145 4554         13859 eval {
146 4554         25826 my $dt = $self->{datetime}->clone;
147 4554         90500 $dt->set(hour => $hour, minute => $minute, second => $second);
148             };
149 4554         3205655 return !$@;
150             }
151             else {
152 4698         74985 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