| 1 | # Copyright 2009, 2010, 2011, 2019 Kevin Ryde | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # This file is part of Test-MockTime-DateCalc. | ||||||||||||||
| 4 | # | ||||||||||||||
| 5 | # Test-MockTime-DateCalc is free software; you can redistribute it and/or | ||||||||||||||
| 6 | # modify it under the terms of the GNU General Public License as published | ||||||||||||||
| 7 | # by the Free Software Foundation; either version 3, or (at your option) any | ||||||||||||||
| 8 | # later version. | ||||||||||||||
| 9 | # | ||||||||||||||
| 10 | # Test-MockTime-DateCalc is distributed in the hope that it will be useful, | ||||||||||||||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | ||||||||||||||
| 13 | # Public License for more details. | ||||||||||||||
| 14 | # | ||||||||||||||
| 15 | # You should have received a copy of the GNU General Public License along | ||||||||||||||
| 16 | # with Test-MockTime-DateCalc. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||
| 17 | |||||||||||||||
| 18 | |||||||||||||||
| 19 | package Test::MockTime::DateCalc; | ||||||||||||||
| 20 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | 4 | ▶ | use vars '$VERSION'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 3 | $VERSION = 7; | |||||||||||||
| 24 | |||||||||||||||
| 25 | 4 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||||
| 26 | # Check that Date::Calc isn't already loaded. | ||||||||||||||
| 27 | # | ||||||||||||||
| 28 | # Week_of_Year() here is a representative func, present in Date::Calc 4.0 | ||||||||||||||
| 29 | # and up, and not one that's mangled here (so as not to risk hitting that | ||||||||||||||
| 30 | # if something goes badly wrong). Maybe looking at %INC would be better. | ||||||||||||||
| 31 | # | ||||||||||||||
| 32 | 4 | ▶ | if (Date::Calc->can('Week_of_Year')) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 1 | die "Date::Calc already loaded, cannot fake after imports may have grabbed its functions"; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | # Date::Calc had a big rewrite in 4.0 of May 1998, no attempt to fake | ||||||||||||||
| 38 | # anything earlier than that | ||||||||||||||
| 39 | # | ||||||||||||||
| 40 | 3 | ▶ | use Date::Calc 4.0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | |||||||||||||||
| 42 | package Date::Calc; | ||||||||||||||
| 43 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | # Calc.xs in Date::Calc calls to the C time() func from its internal C | ||||||||||||||
| 46 | # function DateCalc_system_clock(), and also directly in its Gmtime(), | ||||||||||||||
| 47 | # Localtime(), Timezone() and Time_to_Date(). In each case that of course | ||||||||||||||
| 48 | # misses any fakery on the perl level time(). The replacements here go to | ||||||||||||||
| 49 | # perl time() for the current time, and stay with Date::Calc for conversions | ||||||||||||||
| 50 | # to d/m/y etc. | ||||||||||||||
| 51 | # | ||||||||||||||
| 52 | |||||||||||||||
| 53 | { | ||||||||||||||
| 54 | 3 | local $^W = 0; # no warnings | |||||||||||||
| 55 | 2 | ▶ | eval <<'HERE' or die; | ||||||||||||
|
Subroutine
Now: called CC=1 SCAR=0.1
Subroutine
System_Clock: called CC=2 SCAR=7.1
Subroutine
This_Year: called CC=1 SCAR=0.1
Subroutine
Today: called CC=1 SCAR=0.1
Subroutine
Today_and_Now: called CC=1 SCAR=0.1
Branch: 2 branches
| |||||||||||||||
| 56 | sub System_Clock { | ||||||||||||||
| 57 | my ($gmt) = @_; | ||||||||||||||
| 58 | return ($gmt ? Gmtime() : Localtime()); | ||||||||||||||
| 59 | } | ||||||||||||||
| 60 | sub Today { | ||||||||||||||
| 61 | return (System_Clock(@_))[0,1,2]; | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | sub Now { | ||||||||||||||
| 64 | return (System_Clock(@_))[3,4,5]; | ||||||||||||||
| 65 | } | ||||||||||||||
| 66 | sub Today_and_Now { | ||||||||||||||
| 67 | return (System_Clock(@_))[0,1,2, 3,4,5]; | ||||||||||||||
| 68 | } | ||||||||||||||
| 69 | sub This_Year { | ||||||||||||||
| 70 | return (System_Clock(@_))[0]; | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | 1 | ||||||||||||||
| 73 | HERE | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | { | ||||||||||||||
| 77 | 3 | local $^W = 0; # no warnings | |||||||||||||
| 78 | 3 | ▶ | eval <<'HERE' or die; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
Gmtime: called CC=1 SCAR=0.0
Subroutine
Localtime: called CC=1 SCAR=0.0
Subroutine
Time_to_Date: called CC=1 SCAR=0.0
Subroutine
Timezone: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||
| 79 | |||||||||||||||
| 80 | # anonymous sub to avoid adding anything to the Date::Calc namespace | ||||||||||||||
| 81 | my $default_to_time_func = sub { | ||||||||||||||
| 82 | my ($func, $time) = @_; | ||||||||||||||
| 83 | if (! defined $time) { $time = time(); } | ||||||||||||||
| 84 | return &$func($time); | ||||||||||||||
| 85 | }; | ||||||||||||||
| 86 | { my $orig; | ||||||||||||||
| 87 | BEGIN { $orig = \&Gmtime; } | ||||||||||||||
| 88 | sub Gmtime { return &$default_to_time_func ($orig, @_) } | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | { my $orig; | ||||||||||||||
| 91 | BEGIN { $orig = \&Localtime; } | ||||||||||||||
| 92 | sub Localtime { return &$default_to_time_func ($orig, @_) } | ||||||||||||||
| 93 | } | ||||||||||||||
| 94 | { my $orig; | ||||||||||||||
| 95 | BEGIN { $orig = \&Timezone; } | ||||||||||||||
| 96 | sub Timezone { return &$default_to_time_func ($orig, @_) } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | { my $orig; | ||||||||||||||
| 99 | BEGIN { $orig = \&Time_to_Date; } | ||||||||||||||
| 100 | sub Time_to_Date { return &$default_to_time_func ($orig, @_) } | ||||||||||||||
| 101 | } | ||||||||||||||
| 102 | 1 | ||||||||||||||
| 103 | HERE | ||||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 3 | 1; | |||||||||||||
| 107 | __END__ | ||||||||||||||