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


fixedtime documentation Contained in the fixedtime distribution.

Index


Code Index:

NAME

Top

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

SYNOPSIS

Top

    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

Top

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

Top

perlpragma

AUTHOR AND COPYRIGHT

Top


fixedtime documentation Contained in the fixedtime distribution.
package fixedtime;
use 5.010;    # this is a user-defined pragma and needs perl 5.10 or higher
use warnings;
use strict;

our $VERSION = 0.03;

sub import   {
    shift;
    my %args = @_;
    # we do not care about autoviv
    $^H{fixedtime} = $args{epoch_offset} // CORE::time;
}

sub unimport { $^H{fixedtime} = undef }

 
sub epoch_offset {
    my $ctrl_h = ( caller 1 )[10];
    return $ctrl_h->{fixedtime};
}

# redefine the time related functions
# this works because:
#   * pragma in effect     -> fixedtime::epoch_offset() is defined
#   * pragma not in effect -> fixedtime::epoch_offset() is not defined
#   * the // makes sure that for undef CORE::time is used
# NB: for gmtime and localtime:
#       when an epoch offset is passed, normal operation is in effect
BEGIN {
    *CORE::GLOBAL::time = sub {
        return fixedtime::epoch_offset() // CORE::time;
    };

    *CORE::GLOBAL::gmtime = sub (;$) {
        my $stamp = shift // fixedtime::epoch_offset() // CORE::time;
        CORE::gmtime( $stamp );
    };

    *CORE::GLOBAL::localtime = sub (;$) {
        my $stamp = shift // fixedtime::epoch_offset() // CORE::time;
        CORE::localtime( $stamp );
    };
}

1;

__END__