File Coverage

blib/lib/Test/MockTime.pm
Criterion Covered Total %
statement 46 49 93.8
branch 13 16 81.2
condition 5 11 45.4
subroutine 13 13 100.0
pod 4 7 57.1
total 81 96 84.3


line stmt bran cond sub pod time code
1             package Test::MockTime;
2              
3 4     4   28537 use strict;
  4         5  
  4         332  
4 4     4   21 use warnings;
  4         7  
  4         114  
5 4     4   57 use Carp();
  4         12  
  4         52  
6 4     4   19 use Exporter();
  4         4  
  4         667  
7             *import = \&Exporter::import;
8             our @EXPORT_OK = qw(
9             set_relative_time
10             set_absolute_time
11             set_fixed_time
12             restore_time
13             );
14             our %EXPORT_TAGS = (
15             'all' => \@EXPORT_OK,
16             );
17             our ($VERSION) = '0.13';
18             our ($offset) = 0;
19             our ($fixed) = undef;
20              
21             BEGIN {
22 4     4   21 *CORE::GLOBAL::time = \&Test::MockTime::time;
23 4         13 *CORE::GLOBAL::localtime = \&Test::MockTime::localtime;
24 4         2741 *CORE::GLOBAL::gmtime = \&Test::MockTime::gmtime;
25             }
26              
27             sub set_relative_time {
28 3     3 1 452 my ($relative) = @_;
29 3 50 33     49 if (($relative eq __PACKAGE__) || (UNIVERSAL::isa($relative, __PACKAGE__))) {
30 0         0 Carp::carp("Test::MockTime::set_relative_time called incorrectly\n");
31             }
32 3         9 $offset = $_[-1]; # last argument. might have been called in a OO syntax?
33             }
34              
35             sub _time {
36 15     15   41 my ($time, $spec) = @_;
37 15 100       117 unless ($time =~ /\A -? \d+ \z/xms) {
38 8   100     35 $spec ||= '%Y-%m-%dT%H:%M:%SZ';
39             }
40 15 100       48 if ($spec) {
41 8         805541 require Time::Piece;
42 8         328081 $time = Time::Piece->strptime($time, $spec)->epoch();
43             }
44 15         1044 return $time;
45             }
46              
47             sub set_absolute_time {
48 4     4 1 1209 my ($time, $spec) = @_;
49 4 50 33     68 if (($time eq __PACKAGE__) || (UNIVERSAL::isa($time, __PACKAGE__))) {
50 0         0 Carp::carp("Test::MockTime::set_absolute_time called incorrectly\n");
51             }
52 4         20 $time = _time($time, $spec);
53 4         23 $offset = $time - CORE::time;
54             }
55              
56             sub set_fixed_time {
57 11     11 1 10944 my ($time, $spec) = @_;
58 11 50 33     192 if (($time eq __PACKAGE__) || (UNIVERSAL::isa($time, __PACKAGE__))) {
59 0         0 Carp::carp("Test::MockTime::set_fixed_time called incorrectly\n");
60             }
61 11         110 $time = _time($time, $spec);
62 11         33 $fixed = $time;
63             }
64              
65             sub time() {
66 36 100   36 0 16006989 if (defined $fixed) {
67 19         106 return $fixed;
68             } else {
69 17         92 return (CORE::time + $Test::MockTime::offset);
70             }
71             }
72              
73             sub localtime (;$) {
74 30     30 0 6072056 my ($time) = @_;
75 30 100       104 unless (defined $time) {
76 12         37 $time = Test::MockTime::time();
77             }
78 30         1655 return CORE::localtime($time);
79             }
80              
81             sub gmtime (;$) {
82 10     10 0 6008201 my ($time) = @_;
83 10 100       56 unless (defined $time) {
84 6         29 $time = Test::MockTime::time();
85             }
86 10         116 return CORE::gmtime($time);;
87             }
88              
89             sub restore {
90 1     1 1 5 $offset = 0;
91 1         4 $fixed = undef;
92             }
93             *restore_time = \&restore;