Skip to main content

Various utilities related to date and time for testing purposes.

Project description

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 = datetime.now()
>>> start_t = time.time()

Then you can:

>>> datetime.set_now(datetime(2001, 01, 01, 3, 57, 0))
>>> datetime.now()
datetime(2001, 1, 1, 3, 57)
>>> datetime.today()
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 = datetime.now()
>>> now > start
True
>>> from datetime import timedelta
>>> now - start < timedelta(0, 0, 10000) # 10 ms
True

And with the time module:

>>> now_t = time.time()
>>> now_t > start_t
True
>>> now_t - start_t < 0.01 # 10 ms again
True

Version 0.1 (2012-07-15): initial version

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anybox.testing.datetime-0.1.tar.gz (3.0 kB view details)

Uploaded Source

File details

Details for the file anybox.testing.datetime-0.1.tar.gz.

File metadata

File hashes

Hashes for anybox.testing.datetime-0.1.tar.gz
Algorithm Hash digest
SHA256 ca4f8af0fc67cb77c073b5d2c5a2996b40dfeabe212a084658a721fea93cc455
MD5 9f09bf14faa2ae6db348c207e17b334a
BLAKE2b-256 2b48b1f7a5d3a3560e3d9376b50f100db11b1fb464dde996d9a42b8eaead5446

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page