File Coverage

blib/lib/Data/Fake/Dates.pm
Criterion Covered Total %
statement 27 27 100.0
branch n/a
condition 4 4 100.0
subroutine 13 13 100.0
pod 4 4 100.0
total 48 48 100.0


line stmt bran cond sub pod time code
1 2     2   81522 use 5.008001;
  2         16  
2 2     2   10 use strict;
  2         5  
  2         51  
3 2     2   10 use warnings;
  2         4  
  2         120  
4              
5             package Data::Fake::Dates;
6             # ABSTRACT: Fake date data generators
7              
8             our $VERSION = '0.006';
9              
10 2     2   11 use Exporter 5.57 qw/import/;
  2         28  
  2         147  
11              
12             our @EXPORT = qw(
13             fake_past_epoch
14             fake_future_epoch
15             fake_past_datetime
16             fake_future_datetime
17             );
18              
19 2     2   1153 use Time::Piece 1.27; # portability fixes
  2         23603  
  2         11  
20              
21 13     13   95 sub _past { int( rand(time) ) }
22              
23             sub _future {
24 13     13   24 my $now = time;
25 13         52 return $now + int( rand($now) );
26             }
27              
28             #pod =func fake_past_epoch
29             #pod
30             #pod $generator = fake_past_epoch();
31             #pod
32             #pod This returns a generator that gives a randomly-selected integer number of
33             #pod seconds between the Unix epoch and the current time.
34             #pod
35             #pod =cut
36              
37 6     6 1 6228 sub fake_past_epoch { \&_past }
38              
39             #pod =func fake_future_epoch
40             #pod
41             #pod $generator = fake_future_epoch();
42             #pod
43             #pod This returns a generator that gives a randomly-selected integer number of
44             #pod seconds between the the current time and a period as far into the future as
45             #pod the Unix epoch is in the past (i.e. about 45 years as of 2015).
46             #pod
47             #pod =cut
48              
49 6     6 1 5677 sub fake_future_epoch { \&_future }
50              
51             #pod =func fake_past_datetime
52             #pod
53             #pod $generator = fake_past_datetime();
54             #pod $generator = fake_past_datetime("%Y-%m-%d");
55             #pod $generator = fake_past_datetime($strftime_format);
56             #pod
57             #pod This returns a generator that selects a past datetime like
58             #pod C does but formats it as a string using FreeBSD-style
59             #pod C formats. (See L for details.)
60             #pod
61             #pod The default format is ISO8601 UTC "Zulu" time (C<%Y-%m-%dT%TZ>).
62             #pod
63             #pod =cut
64              
65             sub fake_past_datetime {
66 7     7 1 7815 my ($format) = @_;
67 7   100     35 $format ||= "%Y-%m-%dT%H:%M:%SZ";
68             return sub {
69 7     7   18 Time::Piece->strptime( _past(), "%s" )->strftime($format);
70 7         33 };
71             }
72              
73             #pod =func fake_future_datetime
74             #pod
75             #pod $generator = fake_future_datetime();
76             #pod $generator = fake_future_datetime("%Y-%m-%d");
77             #pod $generator = fake_future_datetime($strftime_format);
78             #pod
79             #pod This returns a generator that selects a future datetime like
80             #pod C does but formats it as a string using FreeBSD-style
81             #pod C formats. (See L for details.)
82             #pod
83             #pod The default format is ISO8601 UTC "Zulu" time (C<%Y-%m-%dT%TZ>).
84             #pod
85             #pod =cut
86              
87             sub fake_future_datetime {
88 7     7 1 5251 my ($format) = @_;
89 7   100     35 $format ||= "%Y-%m-%dT%H:%M:%SZ";
90             return sub {
91 7     7   15 Time::Piece->strptime( _future(), "%s" )->strftime($format);
92 7         32 };
93             }
94              
95              
96             # vim: ts=4 sts=4 sw=4 et tw=75:
97              
98             __END__