Mockingbird/TimeTravel.pm

filter: Statementstmt 100% 78 / 78 Branchbran 100% 30 / 30 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 15 / 15 Podpod 100% 7 / 7 totaltotal 100% 135 / 135 CC 17 SCAR 12.4
CC
17
Cov
100%
_parse_timestamp
7.0
with_frozen_time
5.0
_unit_to_seconds
3.0
CRAP 3.5
« Mockingbird.pm ↑ Summary Mockingbird/Async.pm »
1 package Test::Mockingbird::TimeTravel;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Time::Local qw(timegm);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 our @EXPORT = qw(
11 now
12 freeze_time
13 travel_to
14 advance_time
15 rewind_time
16 restore_all
17 with_frozen_time
18 );
19  
20 # Unit-to-seconds conversion table. Singular and plural forms accepted;
21 # matching is case-insensitive (normalised in _unit_to_seconds).
22 7 my %SECONDS_PER_UNIT = (
23 second => 1,
24 seconds => 1,
25 minute => 60,
26 minutes => 60,
27 hour => 3600,
28 hours => 3600,
29 day => 86400,
30 days => 86400,
31 );
32  
33 # Internal state -- three package variables so that with_frozen_time()
34 # can save/restore them with 'local'.
35 7 our $ACTIVE = 0; # 1 when frozen, 0 when using real time
36 7 our $CURRENT_EPOCH = undef; # simulated time when frozen
37 7 our $BASE_EPOCH = undef; # epoch at the moment of freeze()
38  
39 =encoding utf-8
40  
41 =head1 NAME
42  
43 Test::Mockingbird::TimeTravel - Deterministic, controllable time for Perl tests
44  
45 =head1 VERSION
46  
47 Version 0.14
48  
49 =cut
50  
51 7 our $VERSION = '0.14';
52  
53 =head1 SYNOPSIS
54  
55 use Test::Mockingbird::TimeTravel qw(
56 now freeze_time travel_to advance_time rewind_time
57 restore_all with_frozen_time
58 );
59  
60 freeze_time('2025-01-01T00:00:00Z');
61 is now(), 1735689600, 'time is frozen';
62  
63 advance_time(2 => 'minutes');
64 is now(), 1735689720, 'time advanced deterministically';
65  
66 with_frozen_time '2025-01-02T12:00:00Z' => sub {
67 is now(), 1735819200, 'block sees overridden time';
68 };
69  
70 is now(), 1735689720, 'outer time restored after block';
71  
72 restore_all();
73 isnt now(), 1735689720, 'real time restored';
74  
75 =head1 DESCRIPTION
76  
77 C<Test::Mockingbird::TimeTravel> provides a lightweight, deterministic
78 time-control layer for Perl tests. It does B<not> monkey-patch
79 C<CORE::time()>. Instead it provides a dedicated C<now()> function and a
80 small set of declarative operations that manipulate an internal frozen clock.
81  
82 =head1 LIMITATIONS
83  
84 =over 4
85  
86 =item C<now()> is not a drop-in for C<time()>
87  
88 Production code must call C<now()> for time control to take effect. Code
89 that calls C<CORE::time()>, C<POSIX::time()>, or C<Time::HiRes::time()>
90 directly is not affected.
91  
92 =item Fractional seconds not supported
93  
94 All timestamps are integer epoch seconds. Sub-second resolution is not
95 available.
96  
97 =item Thread safety
98  
99 C<$ACTIVE>, C<$CURRENT_EPOCH>, and C<$BASE_EPOCH> are package globals.
100 Concurrent threads that manipulate time state will race.
101  
102 =back
103  
104 =head1 METHODS
105  
106 =head2 now
107  
108 Return the current time.
109  
110 Returns C<$CURRENT_EPOCH> when frozen, C<CORE::time()> otherwise.
111  
112 =head3 API SPECIFICATION
113  
114 =head4 Input (Params::Validate::Strict schema)
115  
116 none
117  
118 =head4 Output (Returns::Set schema)
119  
120 returns: Int -- epoch seconds
121  
122 =head3 FORMAL SPECIFICATION
123  
124 now ≙ ($ACTIVE = 1 ⇒ returns $CURRENT_EPOCH) ∧ ($ACTIVE = 0 ⇒ returns CORE::time())
125  
126 =cut
127  
128 101 ▶sub now () {
Subroutine now: called CC=2 SCAR=6.9
Pod documented
129 101 ▶ return $ACTIVE ? $CURRENT_EPOCH : CORE::time();
Branch: 1 branch
BranchTrueFalseTotal
$Test::Mockingbird::TimeTravel::ACTIVE ? : 96 5 101
130 }
131  
132 =head2 freeze_time
133  
134 Freeze the clock at a specific timestamp.
135  
136 my $epoch = freeze_time('2025-01-01T00:00:00Z');
137  
138 =head3 API SPECIFICATION
139  
140 =head4 Input (Params::Validate::Strict schema)
141  
142 $timestamp -- Str|Int (see _parse_timestamp for accepted formats)
143  
144 =head4 Output (Returns::Set schema)
145  
146 returns: Int -- frozen epoch
147  
148 =head3 MESSAGES
149  
150 "Invalid timestamp format for TimeTravel: ..." -- unrecognised format
151  
152 =head3 FORMAL SPECIFICATION
153  
154 freeze_time ≙
155 ∀ ts : Str|Int •
156 post $ACTIVE' = 1 ∧ $CURRENT_EPOCH' = parse(ts) ∧ $BASE_EPOCH' = parse(ts)
157  
158 =cut
159  
160 60 ▶sub freeze_time {
Subroutine freeze_time: called CC=1 SCAR=0.0
Pod documented
161 60 my ($ts) = @_;
162 60 $CURRENT_EPOCH = _parse_timestamp($ts);
163 59 $BASE_EPOCH = $CURRENT_EPOCH;
164 59 $ACTIVE = 1;
165 59 return $CURRENT_EPOCH;
166 }
167  
168 =head2 travel_to
169  
170 Move the frozen clock to a new timestamp without unfreezing.
171  
172 travel_to('2025-06-01T00:00:00Z');
173  
174 Croaks if called while TimeTravel is inactive.
175  
176 =head3 API SPECIFICATION
177  
178 =head4 Input (Params::Validate::Strict schema)
179  
180 $timestamp -- Str|Int
181  
182 =head4 Output (Returns::Set schema)
183  
184 returns: Int -- new epoch
185  
186 =head3 MESSAGES
187  
188 "travel_to() called while TimeTravel is inactive" -- freeze_time() not called first
189  
190 =head3 FORMAL SPECIFICATION
191  
192 travel_to ≙
193 pre $ACTIVE = 1
194 post $CURRENT_EPOCH' = parse(ts) ∧ $BASE_EPOCH unchanged ∧ $ACTIVE unchanged
195  
196 =cut
197  
198 8 ▶sub travel_to {
Subroutine travel_to: called CC=2 SCAR=6.9
Pod documented
199 8 ▶ croak 'travel_to() called while TimeTravel is inactive' unless $ACTIVE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Mockingbird::TimeTravel::ACTIVE 3 5 8
200 5 $CURRENT_EPOCH = _parse_timestamp($_[0]);
201 5 return $CURRENT_EPOCH;
202 }
203  
204 =head2 advance_time
205  
206 Advance the frozen clock by a duration.
207  
208 advance_time(30); # 30 seconds
209 advance_time(2 => 'minutes'); # 2 minutes
210  
211 Croaks if called while TimeTravel is inactive.
212  
213 =head3 API SPECIFICATION
214  
215 =head4 Input (Params::Validate::Strict schema)
216  
217 $amount -- Int
218 $unit -- Str, optional (second|minute|hour|day, plural forms ok)
219  
220 =head4 Output (Returns::Set schema)
221  
222 returns: Int -- new epoch
223  
224 =head3 MESSAGES
225  
226 "advance_time() called while TimeTravel is inactive" -- not frozen
227 "Unknown time unit '...'" -- unrecognised unit string
228  
229 =head3 FORMAL SPECIFICATION
230  
231 advance_time ≙
232 pre $ACTIVE = 1
233 post $CURRENT_EPOCH' = $CURRENT_EPOCH + to_seconds(amount, unit)
234  
235 =cut
236  
237 24 ▶sub advance_time {
Subroutine advance_time: called CC=2 SCAR=6.9
Pod documented
238 24 ▶ croak 'advance_time() called while TimeTravel is inactive' unless $ACTIVE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Mockingbird::TimeTravel::ACTIVE 3 21 24
239 21 my ($amount, $unit) = @_;
240 21 $CURRENT_EPOCH += _unit_to_seconds($amount, $unit);
241 19 return $CURRENT_EPOCH;
242 }
243  
244 =head2 rewind_time
245  
246 Rewind the frozen clock by a duration.
247  
248 rewind_time(30); # 30 seconds
249 rewind_time(1 => 'hour'); # 1 hour
250  
251 Croaks if called while TimeTravel is inactive.
252  
253 =head3 API SPECIFICATION
254  
255 =head4 Input (Params::Validate::Strict schema)
256  
257 $amount -- Int
258 $unit -- Str, optional
259  
260 =head4 Output (Returns::Set schema)
261  
262 returns: Int -- new epoch
263  
264 =head3 MESSAGES
265  
266 "rewind_time() called while TimeTravel is inactive" -- not frozen
267  
268 =head3 FORMAL SPECIFICATION
269  
270 rewind_time ≙
271 pre $ACTIVE = 1
272 post $CURRENT_EPOCH' = $CURRENT_EPOCH - to_seconds(amount, unit)
273  
274 =cut
275  
276 16 ▶sub rewind_time {
Subroutine rewind_time: called CC=2 SCAR=6.9
Pod documented
277 16 ▶ croak 'rewind_time() called while TimeTravel is inactive' unless $ACTIVE;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Mockingbird::TimeTravel::ACTIVE 4 12 16
278 12 my ($amount, $unit) = @_;
279 12 $CURRENT_EPOCH -= _unit_to_seconds($amount, $unit);
280 12 return $CURRENT_EPOCH;
281 }
282  
283 =head2 restore_all
284  
285 Return to real time and clear all frozen state.
286  
287 restore_all();
288  
289 Idempotent. Called automatically by L<Test::Mockingbird::DeepMock>.
290  
291 =head3 API SPECIFICATION
292  
293 =head4 Input (Params::Validate::Strict schema)
294  
295 none
296  
297 =head4 Output (Returns::Set schema)
298  
299 returns: undef
300  
301 =head3 FORMAL SPECIFICATION
302  
303 restore_all ≙
304 post $ACTIVE' = 0 ∧ $CURRENT_EPOCH' = undef ∧ $BASE_EPOCH' = undef
305  
306 =cut
307  
308 119 ▶sub restore_all {
Subroutine restore_all: called CC=1 SCAR=0.0
Pod documented
309 119 $ACTIVE = 0;
310 119 $CURRENT_EPOCH = undef;
311 119 $BASE_EPOCH = undef;
312 119 return;
313 }
314  
315 =head2 with_frozen_time
316  
317 Temporarily override time inside a code block, restoring previous state
318 afterward even if the block throws.
319  
320 with_frozen_time '2025-01-02T12:00:00Z' => sub {
321 is now(), 1735819200, 'block sees overridden time';
322 };
323  
324 Fully nestable.
325  
326 =head3 API SPECIFICATION
327  
328 =head4 Input (Params::Validate::Strict schema)
329  
330 $timestamp -- Str|Int
331 $code -- CodeRef
332  
333 =head4 Output (Returns::Set schema)
334  
335 returns: Any -- whatever $code returns
336  
337 =head3 MESSAGES
338  
339 "with_frozen_time() requires a coderef" -- second arg not a CodeRef
340 "with_frozen_time() requires a timestamp" -- first arg undefined
341  
342 =head3 FORMAL SPECIFICATION
343  
344 with_frozen_time ≙
345 ∀ ts : Str|Int; code : CodeRef •
346 pre defined(ts) ∧ ref(code) = 'CODE'
347 post (save prev_state; freeze(ts); run code; restore prev_state)
348 ∧ exceptions rethrown
349  
350 =cut
351  
352 23 ▶sub with_frozen_time {
Subroutine with_frozen_time: called CC=5 SCAR=16.1
Pod documented
353 23 my ($ts, $code) = @_;
354  
355 23 ▶ croak 'with_frozen_time() requires a coderef'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 3 20 23
356 unless ref($code) eq 'CODE';
357  
358 20 ▶ croak 'with_frozen_time() requires a timestamp'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ts 4 16 20
359 unless defined $ts;
360  
361 # Save state so nested calls restore correctly
362 16 my ($prev_active, $prev_epoch, $prev_base) =
363 ($ACTIVE, $CURRENT_EPOCH, $BASE_EPOCH);
364  
365 16 $CURRENT_EPOCH = _parse_timestamp($ts);
366 16 $BASE_EPOCH = $CURRENT_EPOCH;
367 16 $ACTIVE = 1;
368  
369 16 my (@ret, $err);
370 {
371 16 local $@;
372 16 @ret = eval { $code->() };
373 16 $err = $@;
374 }
375  
376 16 $ACTIVE = $prev_active;
377 16 $CURRENT_EPOCH = $prev_epoch;
378 16 $BASE_EPOCH = $prev_base;
379  
380 # Re-throw as croak to stay consistent with the rest of this module
381 16 ▶ croak $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 4 12 16
382  
383 12 ▶ return wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 11 12
384 }
385  
386 # _parse_timestamp -- Private
387 #
388 # Purpose: Convert a timestamp string to an integer epoch value.
389 # Supports: raw epoch integer, ISO8601 UTC (YYYY-MM-DDTHH:MM:SSZ),
390 # space-separated (YYYY-MM-DD HH:MM:SS), date-only (YYYY-MM-DD).
391 # Entry: $_[0] -- Str, non-empty timestamp
392 # Exit: Int, epoch seconds
393 # Side effects: none
394 114 ▶sub _parse_timestamp {
Subroutine _parse_timestamp: called CC=7 SCAR=19.5
395 114 my $ts = $_[0];
396  
397 114 ▶ croak 'Invalid timestamp format for TimeTravel: (undef)'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ts and length $ts 3 111 114
Condition100%3/3
defined $ts and length $ts
!ll&&!rl&&r
12111
MC/DC100%2/2
defined $ts and length $ts
defined $ts length $ts
Truth table100%3/3
A and B
A= defined $tsB= length $ts
ABresult
0X0
100
111
398 unless defined $ts && length $ts;
399  
400 111 $ts =~ s/^\s+|\s+$//g; # trim whitespace
401  
402 # Raw epoch integer -- return unchanged
403 111 ▶ return $ts if $ts =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ts =~ /^\d+$/ 4 107 111
404  
405 # ISO8601 UTC: YYYY-MM-DDTHH:MM:SSZ
406 107 ▶ if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/) 100 7 107
407 100 return timegm($6, $5, $4, $3, $2 - 1, $1);
408 }
409  
410 # Space-separated: YYYY-MM-DD HH:MM:SS
411 7 ▶ if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})$/) 2 5 7
412 2 return timegm($6, $5, $4, $3, $2 - 1, $1);
413 }
414  
415 # Date-only: YYYY-MM-DD interpreted as midnight UTC
416 5 ▶ if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts =~ /^(\d{4})-(\d{2})-(\d{2})$/) 2 3 5
417 2 return timegm(0, 0, 0, $3, $2 - 1, $1);
418 }
419  
420 3 croak "Invalid timestamp format for TimeTravel: $ts";
421 }
422  
423 # Backwards-compatible alias used in some older tests.
424 18 ▶sub _parse_datetime { _parse_timestamp(@_) }
Subroutine _parse_datetime: called CC=1 SCAR=0.0
425  
426 # _unit_to_seconds -- Private
427 #
428 # Purpose: Convert (amount, unit) into a number of seconds.
429 # Unit is optional (raw seconds assumed if absent).
430 # Entry: $amount -- Int; $unit -- Str or undef
431 # Exit: Int, seconds
432 # Side effects: none
433 44 ▶sub _unit_to_seconds {
Subroutine _unit_to_seconds: called CC=3 SCAR=11.0
434 44 my ($amount, $unit) = @_;
435  
436 44 ▶ return $amount unless defined $unit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $unit 9 35 44
437  
438 35 my $key = lc $unit;
439 croak "Unknown time unit '$unit'"
440 35 ▶ unless exists $SECONDS_PER_UNIT{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SECONDS_PER_UNIT{$key} 3 32 35
441  
442 32 return $amount * $SECONDS_PER_UNIT{$key};
443 }
444  
445 =head1 SUPPORT
446  
447 Please report bugs at L<https://github.com/nigelhorne/Test-Mockingbird/issues>.
448  
449 =head1 AUTHOR
450  
451 Nigel Horne, C<< <njh at nigelhorne.com> >>
452  
453 =head1 SEE ALSO
454  
455 =over 4
456  
457 =item * L<Test::Mockingbird>
458  
459 =item * L<Test::Mockingbird::DeepMock>
460  
461 =back
462  
463 =head1 REPOSITORY
464  
465 L<https://github.com/nigelhorne/Test-Mockingbird>
466  
467 =head1 LICENCE AND COPYRIGHT
468  
469 Copyright 2026 Nigel Horne.
470  
471 Usage is subject to GPL2 licence terms.
472  
473 =cut
474  
475 7 1;
« Mockingbird.pm ↑ Summary Mockingbird/Async.pm »