NAME

Test::MockTime::DateCalc -- fake time for Date::Calc functions

SYNOPSIS

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

DESCRIPTION

Test::MockTime::DateCalc arranges for the functions in Date::Calc to follow the Perl level time function (see perlfunc), 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 setup to default to the Perl-level current time; but when called with an explicit time argument they're unchanged.

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

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

Remember that like all CORE::GLOBAL overrides you must set an overridden time function before loading any code which might use it, in this case before loading Test::MockTime::DateCalc (see "OVERRIDING CORE FUNCTIONS" in CORE).

Date::Calc Load Order

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 here. For that reason Test::MockTime::DateCalc demands that it be the one to load Date::Calc for the first time. Usually this simply means having Test::MockTime::DateCalc at the start of your 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 first to check they load all their pre-requisites. You might want to have a separate test script for that to see you don't accidentally rely on Test::MockTime::DateCalc loading Date::Calc for you.

SEE ALSO

Test::MockTime, Date::Calc, Time::Fake

HOME PAGE

http://www.geocities.com/user42_kevin/test-mocktime-datecalc/

COPYRIGHT

Copyright 2009 Kevin Ryde

Test-MockTime-DateCalc is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

Test-MockTime-DateCalc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Test-MockTime-DateCalc. If not, see http://www.gnu.org/licenses/.