Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

time change

by ag4ve (Monk)
on Feb 18, 2014 at 13:55 UTC ( #1075338=perlquestion: print w/replies, xml ) Need Help??

ag4ve has asked for the wisdom of the Perl Monks concerning the following question:

What's the best way to change the time that time, gmtime, and localtime (and TimeDate which seems to use time()) returns? I was kind of hoping that someone had written a module to do this, but I can't find anything. I want to temporarily change the time to be a week into the future to test some time dependent code (data gets deleted after it is stale and I want this tested).

Replies are listed 'Best First'.
Re: time change (ex::override core function)
by Anonymous Monk on Feb 18, 2014 at 14:02 UTC
Re: time change
by trwww (Priest) on Feb 18, 2014 at 15:01 UTC
    You could probably use Time::Mock or Test::MockTime.
Re: time change
by vinoth.ree (Monsignor) on Feb 18, 2014 at 14:31 UTC


    If you are on Linux, you can just use the date command and can change the date and time to future date and you can do the test.

    Changing the date and time:

    Changing the date and time requires two steps. First, Linux's date and time must be changed and then the new time has to be written to the hardware clock. The date command can be used for both viewing and changing the date and time.

    All is well
      Yeah, I was hoping not to need to touch any part of the system or environment (ie, changing the actual date even if it's not written to hardware - synctohc). However, overriding the functions with *CORE::GLOBAL should work (since it's done on compile time and should affect all modules that call those functions).
Re: time change
by tweetiepooh (Hermit) on Feb 18, 2014 at 15:46 UTC
    Is it possible to tag the data into the past thus making it appear expired without needing to touch the "real" time?
        Yes, Time::Mock seems to do what I want (I haven't looked to see if using Test::TimeMock would be better for my needs yet). Thanks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1075338]
Approved by hdb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2020-10-25 09:28 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (249 votes). Check out past polls.