Test::MockTime::DateCalc(3pm)

Test::MockTime::DateCalc3pm

Usage

 use Test::MockTime;
 use Test::MockTime::DateCalc; # before Date::Calc loads
 # ...
 use My::Module::Using::Date::Calc;

Overview

"Test::MockTime::DateCalc" arranges for the functions in "Date::Calc" to follow the Perl level "time()" function (see perlfunc) and in particular any fake date/time set there by "Test::MockTime". The following "Date::Calc" functions are changed

    System_Clock
    Today
    Now
    Today_and_Now
    This_Year
    Gmtime
    Localtime
    Timezone
    Time_to_Date

"Gmtime", "Localtime", "Timezone" and "Time_to_Date" are made to default to the Perl-level current "time()". When called with an explicit time argument, they're unchanged.

Module Load Order

"Test::MockTime" or similar fakery must be loaded first, before anything with a "time()" call, which includes "Test::MockTime::DateCalc". This is the same as for any "CORE::GLOBAL" override, see "OVERRIDING CORE FUNCTIONS" in CORE.

"Test::MockTime::DateCalc" must be loaded before "Date::Calc". If "Date::Calc" is already loaded then its functions might have been imported into other modules and such imports are not affected by the redefinitions made. For that reason, "Test::MockTime::DateCalc" demands it be the one to load "Date::Calc" for the first time. Usually this simply means having "Test::MockTime::DateCalc" at the start of a test script, before the things you're going to test.

    use strict;
    use warnings;
    use Test::MockTime ':all';
    use Test::MockTime::DateCalc;
    use My::Foo::Bar;
    set_fixed_time('1981-01-01T00:00:00Z');
    is (My::Foo::Bar::something(), 1981);
    restore_time();

In a test script, it's often good to have your own modules early to check they correctly load their pre-requisites. You might want a separate test script for that so as not to accidentally rely on "Test::MockTime::DateCalc" loading "Date::Calc".

Other Faking Modules

"Test::MockTime::DateCalc" can be used with other modules which mangle the Perl-level "time" too. For example "Time::Fake",

    use Time::Fake;                # fakery first
    use Test::MockTime::DateCalc;

Or "Time::Mock",

    use Time::Mock;                # fakery first
    use Test::MockTime::DateCalc;

"Time::Warp" (as of version 0.5) only exports a new "time", it's not a core override and so can't be used with "Test::MockTime::DateCalc".

Home page

http://user42.tuxfamily.org/test-mocktime-datecalc/index.html

Releases

7 releases since Debian 7 (Wheezy); the text changed 3 times.

  1. 7–9
  2. 10
  3. 11
  4. 12–13

From libtest-mocktime-datecalc-perl 7+ds-2 in Debian 13 (Trixie). Parsed by carta from the page's man source.