File Coverage

blib/lib/Random/Day/InTheFuture.pm
Criterion Covered Total %
statement 19 21 90.4
branch 2 4 50.0
condition 1 3 33.3
subroutine 5 5 100.0
pod 1 1 100.0
total 28 34 82.3


line stmt bran cond sub pod time code
1             package Random::Day::InTheFuture;
2              
3 11     11   953909 use base qw(Random::Day);
  11         22  
  11         7318  
4 11     11   89 use strict;
  11         20  
  11         230  
5 11     11   42 use warnings;
  11         101  
  11         642  
6              
7 11     11   61 use DateTime;
  11         21  
  11         1843  
8              
9             our $VERSION = 0.17;
10              
11             sub new {
12 34     34 1 3946377 my ($class, @params) = @_;
13              
14             # Set minimal date.
15 34         123 my $dt_from_exists = 0;
16 34         172 foreach (my $i = 0; $i < @params; $i++) {
17 7 50 33     24 if ($i % 1 == 1 && $params[$i] eq 'dt_from') {
18 0         0 $params[$i+1] = DateTime->now;
19 0         0 $dt_from_exists = 1;
20             }
21             }
22 34 50       107 if (! $dt_from_exists) {
23 34         245 push @params, 'dt_from', DateTime->now;
24             }
25              
26             # Object.
27 34         20539 return bless $class->SUPER::new(@params), $class;
28             }
29              
30             1;
31              
32             __END__
33              
34             =pod
35              
36             =encoding utf8
37              
38             =head1 NAME
39              
40             Random::Day::InTheFuture - Class for random day generation in the future.
41              
42             =head1 SYNOPSIS
43              
44             use Random::Day::InTheFuture;
45              
46             my $obj = Random::Day::InTheFuture->new(%params);
47             my $dt = $obj->get;
48             my $dt = $obj->random;
49             my $dt = $obj->random_day($day);
50             my $dt = $obj->random_day_month($day, $month);
51             my $dt = $obj->random_day_month_year($day, $month, $year);
52             my $dt = $obj->random_month($month);
53             my $dt = $obj->random_month_year($month, $year);
54             my $dt = $obj->random_year($year);
55              
56             =head1 METHODS
57              
58             =head2 C<new>
59              
60             my $obj = Random::Day::InTheFuture->new(%params);
61              
62             Constructor.
63              
64             =over 8
65              
66             =item * C<day>
67              
68             Day.
69              
70             Default value is undef.
71              
72             =item * C<dt_to>
73              
74             DateTime object to.
75              
76             Default value is DateTime object for 2050 year.
77              
78             =item * C<month>
79              
80             Month.
81              
82             Default value is undef.
83              
84             =item * C<year>
85              
86             Year.
87              
88             Default value is undef.
89              
90             =back
91              
92             =head2 C<get>
93              
94             my $dt = $obj->get;
95              
96             Get random date defined by constructor parameters.
97              
98             Returns DateTime object for date.
99              
100             =head2 C<random>
101              
102             my $dt = $obj->random;
103              
104             Get random date.
105              
106             Returns DateTime object for date.
107              
108             =head2 C<random_day>
109              
110             my $dt = $obj->random_day($day);
111              
112             Get random date defined by day.
113              
114             Returns DateTime object for date.
115              
116             =head2 C<random_day_month>
117              
118             my $dt = $obj->random_day_month($day, $month);
119              
120             Get random date defined by day and month.
121              
122             Returns DateTime object for date.
123              
124             =head2 C<random_day_month_year>
125              
126             my $dt = $obj->random_day_month_year($day, $month, $year);
127              
128             Get date defined by day, month and year.
129              
130             Returns DateTime object for date.
131              
132             =head2 C<random_month>
133              
134             my $dt = $obj->random_month($month);
135              
136             Get random date defined by month.
137              
138             Returns DateTime object for date.
139              
140             =head2 C<random_month_year>
141              
142             my $dt = $obj->random_month_year($month, $year);
143              
144             Get random date defined by month and year.
145              
146             Returns DateTime object for date.
147              
148             =head2 C<random_year>
149              
150             my $dt = $obj->random_year($year);
151              
152             Get random date defined by year.
153              
154             Returns DateTime object for date.
155              
156             =head1 ERRORS
157              
158             new():
159             From Class::Utils::set_params():
160             Unknown parameter '%s'.
161             From Mo::utils::check_isa():
162             Parameter 'dt_to' must be a 'DateTime' object.
163             Value: %s
164             Reference: %s
165             From Random::Day::new():
166             Parameter 'dt_from' must have older or same date than 'dt_to'.
167             Date from: %s
168             Date to: %s
169              
170             random_day():
171             From Random::Day::random_day():
172             Day cannot be a zero.
173             Day isn't number.
174              
175             random_day_month():
176             From Random::Day::random_day_month():
177             Cannot create DateTime object.
178             Day cannot be a zero.
179             Day isn't number.
180              
181             random_day_month_year():
182             From Random::Day::random_day_year():
183             Cannot create DateTime object.
184             Error: %s
185             Day cannot be a zero.
186             Day isn't number.
187              
188             random_month():
189             From Random::Day::random_momth():
190             Cannot create DateTime object.
191             Error: %s
192              
193             random_month_year():
194             From Random::Day::random_month_year():
195             Begin of expected month is lesser than minimal date.
196             Expected year: %s
197             Expected month: %s
198             Minimal year: %s
199             Minimal month: %s
200             Cannot create DateTime object.
201             Error: %s
202             End of expected month is greater than maximal date.
203             Expected year: %s
204             Expected month: %s
205             Maximal year: %s
206             Maximal month: %s
207              
208             random_year():
209             From Random::Day::random_year():
210             Year is greater than maximal year.
211             Expected year: %s
212             Maximal year: %s
213             Year is lesser than minimal year.
214             Expected year: %s
215             Minimal year: %s
216              
217             =head1 EXAMPLE
218              
219             =for comment filename=get_random_day_in_the_future.pl
220              
221             use strict;
222             use warnings;
223              
224             use Random::Day::InTheFuture;
225              
226             # Object.
227             my $obj = Random::Day::InTheFuture->new;
228              
229             # Get date.
230             my $dt = $obj->get;
231              
232             # Print out.
233             print $dt->ymd."\n";
234              
235             # Output like:
236             # \d\d\d\d-\d\d-\d\d
237              
238             =head1 DEPENDENCIES
239              
240             L<Random::Day>,
241             L<DateTime>.
242              
243             =head1 SEE ALSO
244              
245             =over
246              
247             =item L<Data::Random>
248              
249             Perl module to generate random data
250              
251             =item L<Random::Day>
252              
253             Class for random day generation.
254              
255             =item L<Random::Day::InThePast>
256              
257             Class for random day generation in the past.
258              
259             =back
260              
261             =head1 REPOSITORY
262              
263             L<https://github.com/michal-josef-spacek/Random-Day>
264              
265             =head1 AUTHOR
266              
267             Michal Josef Špaček L<mailto:skim@cpan.org>
268              
269             L<http://skim.cz>
270              
271             =head1 LICENSE AND COPYRIGHT
272              
273             © 2013-2025 Michal Josef Špaček
274              
275             BSD 2-Clause License
276              
277             =head1 VERSION
278              
279             0.17
280              
281             =cut