fixedtime - lexical pragma to fix the epoch offset for time related functions
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";
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.
$^H{fixedtime}
The fixedtime pragma affects time(), gmtime() and localtime() only when called without an argument.
fixedtime
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.
CORE::time()
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;
This will disable the pragma in the current lexical scope.
perlpragma
(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.
To install fixedtime, copy and paste the appropriate command in to your terminal.
cpanm
cpanm fixedtime
CPAN shell
perl -MCPAN -e shell install fixedtime
For more information on module installation, please visit the detailed CPAN module installation guide.