Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

monkey patching time.time() in python

I've an application where, for testing, I need to replace the time.time() call with a specific timestamp, I've done that in the past using ruby

(code available here: http://github.com/zemariamm/Back-to-Future/blob/master/back_to_future.rb )

However I do not know how to do this using Python.

Any hints ? Cheers, Ze Maria

like image 248
user361526 Avatar asked Dec 09 '22 17:12

user361526


1 Answers

You can simply set time.time to point to your new time function, like this:

import time

def my_time():
    return 0.0

old_time = time.time
time.time = my_time
like image 134
Daniel Stutzbach Avatar answered Dec 25 '22 23:12

Daniel Stutzbach