line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::MockTime; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
166560
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
91
|
|
4
|
4
|
|
|
4
|
|
18
|
use warnings; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
90
|
|
5
|
4
|
|
|
4
|
|
17
|
use Carp(); |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
51
|
|
6
|
4
|
|
|
4
|
|
15
|
use Exporter(); |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
552
|
|
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 = ( 'all' => \@EXPORT_OK, ); |
15
|
|
|
|
|
|
|
our $VERSION = '0.15'; |
16
|
|
|
|
|
|
|
our $offset = 0; |
17
|
|
|
|
|
|
|
our $fixed = undef; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
BEGIN { |
20
|
4
|
|
|
4
|
|
20
|
*CORE::GLOBAL::time = \&Test::MockTime::time; |
21
|
4
|
|
|
|
|
12
|
*CORE::GLOBAL::localtime = \&Test::MockTime::localtime; |
22
|
4
|
|
|
|
|
2190
|
*CORE::GLOBAL::gmtime = \&Test::MockTime::gmtime; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub set_relative_time { |
26
|
3
|
|
|
3
|
1
|
726
|
my ($relative) = @_; |
27
|
3
|
50
|
33
|
|
|
53
|
if ( ( $relative eq __PACKAGE__ ) |
28
|
|
|
|
|
|
|
|| ( UNIVERSAL::isa( $relative, __PACKAGE__ ) ) ) |
29
|
|
|
|
|
|
|
{ |
30
|
0
|
|
|
|
|
0
|
Carp::carp("Test::MockTime::set_relative_time called incorrectly\n"); |
31
|
|
|
|
|
|
|
} |
32
|
3
|
|
|
|
|
10
|
$offset = $_[-1]; # last argument. might have been called in a OO syntax? |
33
|
3
|
|
|
|
|
9
|
return $offset; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _time { |
37
|
15
|
|
|
15
|
|
35
|
my ( $time, $spec ) = @_; |
38
|
15
|
100
|
|
|
|
116
|
if ( $time !~ /\A -? \d+ \z/xms ) { |
39
|
8
|
|
100
|
|
|
49
|
$spec ||= '%Y-%m-%dT%H:%M:%SZ'; |
40
|
|
|
|
|
|
|
} |
41
|
15
|
100
|
|
|
|
55
|
if ($spec) { |
42
|
8
|
|
|
|
|
6158
|
require Time::Piece; |
43
|
8
|
|
|
|
|
13336
|
$time = Time::Piece->strptime( $time, $spec )->epoch(); |
44
|
|
|
|
|
|
|
} |
45
|
15
|
|
|
|
|
907
|
return $time; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub set_absolute_time { |
49
|
4
|
|
|
4
|
1
|
944
|
my ( $time, $spec ) = @_; |
50
|
4
|
50
|
33
|
|
|
62
|
if ( ( $time eq __PACKAGE__ ) || ( UNIVERSAL::isa( $time, __PACKAGE__ ) ) ) |
51
|
|
|
|
|
|
|
{ |
52
|
0
|
|
|
|
|
0
|
Carp::carp("Test::MockTime::set_absolute_time called incorrectly\n"); |
53
|
|
|
|
|
|
|
} |
54
|
4
|
|
|
|
|
18
|
$time = _time( $time, $spec ); |
55
|
4
|
|
|
|
|
13
|
$offset = $time - CORE::time; |
56
|
4
|
|
|
|
|
10
|
return $offset; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub set_fixed_time { |
60
|
11
|
|
|
11
|
1
|
14922
|
my ( $time, $spec ) = @_; |
61
|
11
|
50
|
33
|
|
|
244
|
if ( ( $time eq __PACKAGE__ ) || ( UNIVERSAL::isa( $time, __PACKAGE__ ) ) ) |
62
|
|
|
|
|
|
|
{ |
63
|
0
|
|
|
|
|
0
|
Carp::carp("Test::MockTime::set_fixed_time called incorrectly\n"); |
64
|
|
|
|
|
|
|
} |
65
|
11
|
|
|
|
|
61
|
$time = _time( $time, $spec ); |
66
|
11
|
|
|
|
|
29
|
$fixed = $time; |
67
|
11
|
|
|
|
|
35
|
return $fixed; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub time() { |
71
|
36
|
100
|
|
36
|
0
|
16003163
|
if ( defined $fixed ) { |
72
|
19
|
|
|
|
|
152
|
return $fixed; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
else { |
75
|
17
|
|
|
|
|
140
|
return ( CORE::time + $offset ); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub localtime (;$) { |
80
|
30
|
|
|
30
|
0
|
6050533
|
my ($time) = @_; |
81
|
30
|
100
|
|
|
|
151
|
if ( !defined $time ) { |
82
|
12
|
|
|
|
|
44
|
$time = Test::MockTime::time(); |
83
|
|
|
|
|
|
|
} |
84
|
30
|
|
|
|
|
1554
|
return CORE::localtime($time); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub gmtime (;$) { |
88
|
10
|
|
|
10
|
0
|
6001282
|
my ($time) = @_; |
89
|
10
|
100
|
|
|
|
64
|
if ( !defined $time ) { |
90
|
6
|
|
|
|
|
43
|
$time = Test::MockTime::time(); |
91
|
|
|
|
|
|
|
} |
92
|
10
|
|
|
|
|
268
|
return CORE::gmtime($time); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub restore { |
96
|
1
|
|
|
1
|
1
|
4
|
$offset = 0; |
97
|
1
|
|
|
|
|
2
|
$fixed = undef; |
98
|
1
|
|
|
|
|
3
|
return; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
*restore_time = \&restore; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
__END__ |