Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Update 2: This module has been cleaned up a little bit and some small features added (not updated in the code in this node), and I've uploaded it to CPAN as Time::Fake. It will appear on CPAN after the normal processing time...

Here's a little module that I just threw together. It overrides the builtin time sub (Update: and also localtime and gmtime) to adjust the output by some offset:

package TimeOffset; sub import { my $pkg = shift; my $offset = shift || "+0"; if ($offset !~ /^[+-]/) { $offset = $offset - time; } *CORE::GLOBAL::time = sub { CORE::time() + $offset }; *CORE::GLOBAL::localtime = sub { return @_ ? CORE::localtime(@_) : CORE::localtime(CORE::time() + $offset); }; *CORE::GLOBAL::gmtime = sub { return @_ ? CORE::gmtime(@_) : CORE::gmtime(CORE::time() + $offset); }; } 1;
You can use this as follows:
use TimeOffset '+3600'; # pretend it's 1hr in the future
or
use TimeOffset '-60'; # pretend it's 1min in the past
or
use TimeOffset 12345678; # pretend the program was started # at 12345678 epoch seconds
This is just a proof of concept but you can probably extend it to however your code figures out the current time. I.e, there may be other ways to obtain the time in perl that I haven't thought of.

Perhaps I could make this a little nicer, package it up, and send it off to CPAN? That is, if something like this really isn't on CPAN already..

Update: some caveats that I can think of, off the top of my head:

  • doesn't affect -M, -A, -C filetest operators in the way you'd probably want. I belive these would still report *actual* script start time (not faked script start time) minus file access time.

blokhead


In reply to Re: Simulating the Future by blokhead
in thread Simulating the Future by dynamo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-25 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found