Test::...teCalc(3pm)

πŸ“¦
version 7 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Test::MockTime::DateCalc πŸ’– β€” fake time for Date::Calc functions

okay bestie so you know how this page is literally called Test::...teCalc(3pm) in the corner because the terminal cut it off? it's Test::MockTime::DateCalc, she's just too iconic to fit πŸ’… anyway sit down, grab a strawberry πŸ“, we're doing time travel.

✨ SYNOPSIS ✨

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

like... that's it. that's the whole vibe. use the fakery first, then everything else. no cap.

🌸 DESCRIPTION 🌸

okay so Test::MockTime::DateCalc arranges for the functions in Date::Calc to follow the Perl level time() function (go look at perlfunc, iykyk) and in particular any fake date/time set there by Test::MockTime. so like, when you lie about what time it is, Date::Calc believes you. loyal king behavior honestly πŸ‘‘

the following Date::Calc functions are the ones getting changed:

System_Clock
Today
Now
Today_and_Now
This_Year

Gmtime
Localtime
Timezone
Time_to_Date

and real talk about the second little group: Gmtime, Localtime, Timezone and Time_to_Date are made to default to the Perl-level current time(). but β€” and this is the important part bestie β€” when they're called with an explicit time argument, they're unchanged. untouched. unbothered. ✨


πŸŽ€ Module Load Order πŸŽ€

order matters SO much here, I'm not even joking 😭

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, so go read "OVERRIDING CORE FUNCTIONS" in CORE if you want the lore.

Test::MockTime::DateCalc must be loaded before Date::Calc. and here's why: 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. like, they already left the group chat, you can't retroactively change the messages πŸ™ˆ For that reason, Test::MockTime::DateCalc demands it be the one to load Date::Calc for the first time. dramatic? yes. correct? also yes.

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();

see how it's all lined up before My::Foo::Bar? chef's kiss πŸ§‘πŸ³πŸ’‹

In a test script, it's often good to have your own modules early to check they correctly load their pre-requisites. and honestly? you might want a separate test script for that so as not to accidentally rely on Test::MockTime::DateCalc loading Date::Calc. don't finesse your own test coverage by accident, bestie, it's giving sabotage πŸ’€

πŸ¦‹ Other Faking Modules πŸ¦‹

good news: Test::MockTime::DateCalc can be used with other modules which mangle the Perl-level time too. she plays well with others, unlike some people.

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;

but 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. sorry girl, you're not in the group chat πŸ’”

πŸ‘‘ SEE ALSO πŸ‘‘

  • Date::Calc
  • Test::MockTime
  • Time::Fake
  • Time::Mock
  • faketime(1)

🌈 HOME PAGE 🌈

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

Copyright 2009, 2010, 2011, 2019 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/>.


and that's the whole page bestie, you ate that 🫢 this was perl v5.36.0, dated 2022-10-16, and in the corner she's still labeled Test::...teCalc(3pm) because the terminal is a hater. go forth and fake some time. periodt. πŸŽ€βœ¨

πŸ“¦ where this page comes from