File Coverage

blib/lib/Test/MockTime/DateCalc.pm
Criterion Covered Total %
statement 28 28 100.0
branch 3 4 75.0
condition n/a
subroutine 18 18 100.0
pod n/a
total 49 50 98.0


line stmt bran cond sub pod time code
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 .
17              
18              
19             package Test::MockTime::DateCalc;
20 4     4   21686 use strict;
  4         16  
  4         120  
21              
22 4     4   21 use vars '$VERSION';
  4         7  
  4         245  
23             $VERSION = 7;
24              
25             BEGIN {
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 100   4   108 if (Date::Calc->can('Week_of_Year')) {
33 1         31 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     3   1266 use Date::Calc 4.0;
  3         22499  
  3         141  
41              
42             package Date::Calc;
43 3     3   21 use strict;
  3         6  
  3         230  
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             local $^W = 0; # no warnings
55 2 50   2   231 eval <<'HERE' or die;
  10     10   2014181  
  10     2   237  
  2     2   269  
  2     2   426  
  2         232  
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             local $^W = 0; # no warnings
78 3     3   184 eval <<'HERE' or die;
  3     3   205  
  3     3   140  
  3     3   118  
  12     12   271  
  1     1   169  
  2     2   187  
  1     1   117  
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             1;
107             __END__