NAME

fixedtime - lexical pragma to fix the epoch offset for time related functions

SYNOPSIS

use Test::More 'no_plan';

use constant EPOCH_OFFSET => 1204286400; # 29 Feb 2008 12:00:00 GMT

{
    use fixedtime epoch_offset => EPOCH_OFFSET;

    my $fixstamp = time;
    is $fixstamp, EPOCH_OFFSET, "Fixed point in time ($fixstamp)";
    is scalar gmtime, "Fri Feb 29 12:00:00 2008",
       "@{[ scalar gmtime ]}";

    no fixedtime;
    isnt time, EPOCH_OFFSET, "time() is back to normal";
}

isnt time, EPOCH_OFFSET, "time() is back to normal";

DESCRIPTION

This pragma demonstrates the new perl 5.10 user-defined lexical pragma capability. It uses the $^H{fixedtime} hintshash entry to store the epochoffset. Whenever $^H{fixedtime} is undefined, the praga is assumed not to be in effect.

The fixedtime pragma affects time(), gmtime() and localtime() only when called without an argument.

use fixedtime [epoch_offset => epoch_offset];

This will enable the pragma in the current lexical scope. When the epoch_offset argument is omitted, CORE::time() is taken. While the pragma is in effect the epochoffset is not changed.

Warning: If you use a variable to set the epoch offset, make sure it is initialized at compile time.

my $epoch_offset = 1204286400;
use fixedtime epoch_offset => $epoch_offset; # Will not work as expected

You will need something like:

use constant EPOCH_OFFSET => 1204286400;
use fixedtime epoch_offset => EPOCH_OFFSET;

no fixedtime;

This will disable the pragma in the current lexical scope.

SEE ALSO

perlpragma

AUTHOR AND COPYRIGHT

(c) MMVIII All rights reserved, Abe Timmerman <abeltje@cpan.org>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

This program 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.