Time-Fake-0.11/0000755000175000017500000000000010743675000011341 5ustar mjrmjrTime-Fake-0.11/MANIFEST0000644000175000017500000000021410743675000012467 0ustar mjrmjrChanges MANIFEST Makefile.PL README lib/Time/Fake.pm test.pl META.yml Module meta-data (added by MakeMaker) Time-Fake-0.11/test.pl0000644000175000017500000000115510743674573012675 0ustar mjrmjruse Time::Fake; use Test::More tests => 7; use strict; my $mon = (localtime)[4]; is( Time::Fake->offset("+2M"), 0, "original offset zero" ); ok( $mon != (localtime)[4], "changes localtime" ); is( 3600*24*60, Time::Fake->reset, "reflects new offset" ); is( (localtime)[4], $mon, "localtime reset" ); my $epoch = 1199547498; Time::Fake->offset($epoch); is( time, $epoch, "epoch as argument" ); is( scalar localtime(time), scalar localtime(), "localtime with arg gives same thing" ); is( scalar gmtime(time), scalar gmtime(), "gmtime with arg gives same thing" ); Time-Fake-0.11/Changes0000644000175000017500000000035510743674744012655 0ustar mjrmjrRevision history for Perl extension Time::Fake: 0.11 Thu Jan 17, 2008 - bugfix for {local,gm}time with argument (thanks Ash Berlin) 0.10 Fri Jan 04, 2008 - Initial release. - see http://perlmonks.org/?node_id=660449 for inspiration Time-Fake-0.11/lib/0000755000175000017500000000000010743675000012107 5ustar mjrmjrTime-Fake-0.11/lib/Time/0000755000175000017500000000000010743675000013005 5ustar mjrmjrTime-Fake-0.11/lib/Time/Fake.pm0000644000175000017500000001315410743674664014234 0ustar mjrmjrpackage Time::Fake; use Carp; use strict; use vars '$VERSION'; $VERSION = "0.11"; ##################### my $OFFSET = 0; *CORE::GLOBAL::time = sub() { CORE::time() + $OFFSET }; *CORE::GLOBAL::localtime = sub(;$) { @_ ? CORE::localtime($_[0]) : CORE::localtime(CORE::time() + $OFFSET); }; *CORE::GLOBAL::gmtime = sub(;$) { @_ ? CORE::gmtime($_[0]) : CORE::gmtime(CORE::time() + $OFFSET); }; sub import { my $pkg = shift; $pkg->offset(shift); } sub offset { my $pkg = shift; return $OFFSET if !@_; my $old_offset = $OFFSET; $OFFSET = _to_offset(shift); return $old_offset; } sub reset { shift->offset(0); } my %mult = ( s => 1, m => 60, h => 60*60, d => 60*60*24, M => 60*60*24*30, y => 60*60*24*365, ); sub _to_offset { my $t = shift || return 0; if ($t =~ m/^([+-]\d+)([smhdMy]?)$/) { $t = $1 * $mult{ $2 || "s" }; } elsif ($t !~ m/\D/) { $t = $t - CORE::time; } else { croak "Invalid time offset: `$t'"; } return $t; } 1; __END__ =head1 NAME Time::Fake - Simulate different times without changing your system clock =head1 SYNOPSIS Pretend we are running 1 day in the future: use Time::Fake '+1d'; Pretend we are running 1 year in the past: use Time::Fake '-1y'; Pretend the script started at epoch time 1234567: use Time::Fake 1234567; See what an existing script would do if run 20 years in the future: % perl -MTime::Fake="+20y" test.pl Run a section of code in a time warp: use Time::Fake; # do some setup Time::Fake->offset("+1y"); run_tests(); # thinks it's a year ahead Time::Fake->reset; # back to the present =head1 DESCRIPTION Use this module to achieve the effect of changing your system clock, but without actually changing your system clock. It overrides the Perl builtin subs C