anybox.testing.datetime /

Filename Size Date modified Message
62 B
45 B
76 B
14 B
1.4 KB
92 B
24 B
812 B

A module to allow playing with time in tests.

This README is also a doctest. To it and other doctests for this package, simply do:

nosetests --with-doctest --doctest-extension=txt

Before anything, the package must be imported in order to replace the regular datetime module with the modified one:

>>> import anybox.testing.datetime
>>> from datetime import datetime
>>> import time

Let's keep the real value of now around:

>>> start =
>>> start_t = time.time()

Then you can:

>>> datetime.set_now(datetime(2001, 01, 01, 3, 57, 0))
datetime(2001, 1, 1, 3, 57)
datetime(2001, 1, 1, 3, 57)

The time module goes along:

>>> datetime.fromtimestamp(time.time())
datetime(2001, 1, 1, 3, 57)

Note that you can expect a few microseconds difference (not displayed here because datetime.fromtimestamp ignores them).

Don't forget afterwards get back to the regular system clock, otherwise many pieces of code might get very suprised if the system clock looks as if it's frozen:

>>> datetime.real_now()

Now let's check it worked:

>>> now =
>>> now > start
>>> from datetime import timedelta
>>> now - start < timedelta(0, 0, 10000) # 10 ms

And with the time module:

>>> now_t = time.time()
>>> now_t > start_t
>>> now_t - start_t < 0.01 # 10 ms again
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.