package 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