| 1 | package Test::MockTime; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Carp(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Exporter(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | *import = \&Exporter::import; | |||||||||||||||||||||||||||
| 8 | 4 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 9 | set_relative_time | ||||||||||||||||||||||||||||
| 10 | set_absolute_time | ||||||||||||||||||||||||||||
| 11 | set_fixed_time | ||||||||||||||||||||||||||||
| 12 | restore_time | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | 4 | our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, ); | |||||||||||||||||||||||||||
| 15 | 4 | our $VERSION = '0.17'; | |||||||||||||||||||||||||||
| 16 | 4 | our $offset = 0; | |||||||||||||||||||||||||||
| 17 | 4 | our $fixed = undef; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | *CORE::GLOBAL::time = \&Test::MockTime::time; | |||||||||||||||||||||||||||
| 21 | 4 | *CORE::GLOBAL::localtime = \&Test::MockTime::localtime; | |||||||||||||||||||||||||||
| 22 | 4 | *CORE::GLOBAL::gmtime = \&Test::MockTime::gmtime; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | sub set_relative_time { | ||||||||||||||||||||||||||
|
Subroutine
set_relative_time: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 3 | my ($relative) = @_; | |||||||||||||||||||||||||||
| 27 | 3 | ▶ | if ( ( $relative eq __PACKAGE__ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$relative eq __PACKAGE__ or UNIVERSAL::isa($relative, __PACKAGE__)
MC/DC0%0/2
$relative eq __PACKAGE__ or UNIVERSAL::isa($relative, __PACKAGE__)
$relative eq __PACKAGE__
UNIVERSAL::isa($relative, __PACKAGE__)
Truth table33%1/3
A or B
A= $relative eq __PACKAGE__B= UNIVERSAL::isa($relative, __PACKAGE__)
| |||||||||||||||||||||||||||||
| 28 | || ( UNIVERSAL::isa( $relative, __PACKAGE__ ) ) ) | ||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||
| 30 | 0 | Carp::carp("Test::MockTime::set_relative_time called incorrectly\n"); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 3 | $offset = $_[-1]; # last argument. might have been called in a OO syntax? | |||||||||||||||||||||||||||
| 33 | 3 | return $offset; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 15 | ▶ | sub _time { | ||||||||||||||||||||||||||
|
Subroutine
_time: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 37 | 15 | my ( $time, $spec ) = @_; | |||||||||||||||||||||||||||
| 38 | 15 | ▶ | if ( $time !~ /\A -? \d+ \z/xms ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 8 | ▶ | $spec ||= '%Y-%m-%dT%H:%M:%SZ'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$spec ||= "%Y-%m-%dT%H:%M:%SZ"
MC/DC100%1/1
$spec ||= "%Y-%m-%dT%H:%M:%SZ"
$spec
Truth table100%2/2
A
A= $spec
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 15 | ▶ | if ($spec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 8 | require Time::Piece; | |||||||||||||||||||||||||||
| 43 | 8 | $time = Time::Piece->strptime( $time, $spec )->epoch(); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 15 | return $time; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | ▶ | sub set_absolute_time { | ||||||||||||||||||||||||||
|
Subroutine
set_absolute_time: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 4 | my ( $time, $spec ) = @_; | |||||||||||||||||||||||||||
| 50 | 4 | ▶ | if ( ( $time eq __PACKAGE__ ) || ( UNIVERSAL::isa( $time, __PACKAGE__ ) ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)
MC/DC0%0/2
$time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)
$time eq __PACKAGE__
UNIVERSAL::isa($time, __PACKAGE__)
Truth table33%1/3
A or B
A= $time eq __PACKAGE__B= UNIVERSAL::isa($time, __PACKAGE__)
| |||||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||
| 52 | 0 | Carp::carp("Test::MockTime::set_absolute_time called incorrectly\n"); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | 4 | $time = _time( $time, $spec ); | |||||||||||||||||||||||||||
| 55 | 4 | $offset = $time - CORE::time; | |||||||||||||||||||||||||||
| 56 | 4 | return $offset; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 11 | ▶ | sub set_fixed_time { | ||||||||||||||||||||||||||
|
Subroutine
set_fixed_time: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 11 | my ( $time, $spec ) = @_; | |||||||||||||||||||||||||||
| 61 | 11 | ▶ | if ( ( $time eq __PACKAGE__ ) || ( UNIVERSAL::isa( $time, __PACKAGE__ ) ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)
MC/DC0%0/2
$time eq __PACKAGE__ or UNIVERSAL::isa($time, __PACKAGE__)
$time eq __PACKAGE__
UNIVERSAL::isa($time, __PACKAGE__)
Truth table33%1/3
A or B
A= $time eq __PACKAGE__B= UNIVERSAL::isa($time, __PACKAGE__)
| |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | Carp::carp("Test::MockTime::set_fixed_time called incorrectly\n"); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 11 | $time = _time( $time, $spec ); | |||||||||||||||||||||||||||
| 66 | 11 | $fixed = $time; | |||||||||||||||||||||||||||
| 67 | 11 | return $fixed; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 118 | ▶ | sub time() { | ||||||||||||||||||||||||||
|
Subroutine
time: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 118 | ▶ | if ( defined $fixed ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 58 | return $fixed; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | else { | ||||||||||||||||||||||||||||
| 75 | 60 | return ( CORE::time + $offset ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 30 | ▶ | sub localtime (;$) { | ||||||||||||||||||||||||||
|
Subroutine
localtime: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 80 | 30 | my ($time) = @_; | |||||||||||||||||||||||||||
| 81 | 30 | ▶ | if ( !defined $time ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 12 | $time = Test::MockTime::time(); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 30 | return CORE::localtime($time); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 10 | ▶ | sub gmtime (;$) { | ||||||||||||||||||||||||||
|
Subroutine
gmtime: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 10 | my ($time) = @_; | |||||||||||||||||||||||||||
| 89 | 10 | ▶ | if ( !defined $time ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 6 | $time = Test::MockTime::time(); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 10 | return CORE::gmtime($time); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub restore { | ||||||||||||||||||||||||||
|
Subroutine
restore: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 1 | $offset = 0; | |||||||||||||||||||||||||||
| 97 | 1 | $fixed = undef; | |||||||||||||||||||||||||||
| 98 | 1 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 4 | *restore_time = \&restore; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | 1; | |||||||||||||||||||||||||||
| 103 | __END__ | ||||||||||||||||||||||||||||