Skip to main content

zc.time

zc.time provides a single point of creating datetime objects with the current time. It is easily swappable with a test method without having to monkeypatch the standard datetime classes.

>>> import time
>>> import zc.time
>>> now = zc.time.now()
>>> type(now)
<type 'datetime.datetime'>

It also defaults to UTC, which the vanilla datetime does not.

>>> now.tzinfo
<UTC>

There’s also a utcnow() function, which returns the naive UTC time corresponding to the now() function’s return value. This provides a utcnow() implementation that’s similarly affected by replacing the now() function:

>>> now = zc.time.utcnow()
>>> type(now)
<type 'datetime.datetime'>
>>> now.tzinfo

This relationship holds even if now() is replaced (not recommended):

>>> import datetime
>>> import pytz
>>> t = datetime.datetime(2010, 4, 1, 10, 50, 30, 2345, pytz.UTC)
>>> old_now = zc.time.now
>>> zc.time.now = lambda: t
>>> zc.time.now()
datetime.datetime(2010, 4, 1, 10, 50, 30, 2345, tzinfo=<UTC>)
>>> zc.time.utcnow()
datetime.datetime(2010, 4, 1, 10, 50, 30, 2345)

The reset() function provided cleans up modifications made to control the time:

>>> zc.time.reset()
>>> zc.time.now is old_now
True

A set_now() function is provided that takes a datetime, and causes now() and utcnow() to pretend that’s the real time. The time passed in can be in any time zone; naive times are converted to UTC using pytz.UTC.localize:

>>> zc.time.set_now(t)
>>> zc.time.now()
datetime.datetime(2010, 4, 1, 10, 50, 30, 2345, tzinfo=<UTC>)
>>> zc.time.utcnow()
datetime.datetime(2010, 4, 1, 10, 50, 30, 2345)
>>> time.time()
1270137030.002345
>>> naive = datetime.datetime(2010, 4, 1, 12, 27, 3, 5432)
>>> zc.time.set_now(naive)
>>> zc.time.now()
datetime.datetime(2010, 4, 1, 12, 27, 3, 5432, tzinfo=<UTC>)
>>> zc.time.utcnow()
datetime.datetime(2010, 4, 1, 12, 27, 3, 5432)
>>> time.time()
1270142823.005432
>>> t = datetime.datetime(2010, 4, 1, 11, 17, 3, 5432,
...                       pytz.timezone("US/Eastern"))
>>> zc.time.set_now(t)
>>> zc.time.now()
datetime.datetime(2010, 4, 1, 16, 17, 3, 5432, tzinfo=<UTC>)
>>> zc.time.utcnow()
datetime.datetime(2010, 4, 1, 16, 17, 3, 5432)
>>> time.time()
1270156623.005432

To move forward in time, simply use set_now() again:

>>> zc.time.set_now(t + datetime.timedelta(hours=1))
>>> zc.time.now()
datetime.datetime(2010, 4, 1, 17, 17, 3, 5432, tzinfo=<UTC>)
>>> zc.time.utcnow()
datetime.datetime(2010, 4, 1, 17, 17, 3, 5432)
>>> time.time()
1270160223.005432

If an application sleeps using time.sleep, that’ll be reflected in the times reported:

>>> import time
>>> time.sleep(0.25)
>>> zc.time.now()
datetime.datetime(2010, 4, 1, 17, 17, 3, 255432, tzinfo=<UTC>)
>>> zc.time.utcnow()
datetime.datetime(2010, 4, 1, 17, 17, 3, 255432)
>>> time.time()
1270160223.255432

The reported time will be updated by the exact delay requested of the time.sleep call, rather than by the actual delay.

The reset() function is used to clean up after this as well:

>>> zc.time.reset()

The reset() is registered as a general cleanup handler if zope.testing is available. This is generally not sufficient for functional tests, which will need to call reset themselves.

Changes

1.0.2 (2015-04-20)

  • Fix packaging bug.

1.0.0 (2015-04-20)

  • Include time.time in what’s controlled by zc.time.set_now.

0.3 (2010-07-23)

  • Added time.sleep() support.

0.2 (2010-04-01)

  • Added utcnow().

  • Added set_now(), reset().

0.1

Initial release.

Metadata

Release files for zc.time 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zc.time 1.0.2
File Size Uploaded Provenance Actions
zc.time-1.0.2.tar.gz 3.7 kB Without provenance Details for zc.time-1.0.2.tar.gz

Release files / zc.time-1.0.2.tar.gz

Download URL zc.time-1.0.2.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c3bbfd5b878c9b8d478ba97ecd7af9efcc87886bacc030c6213c3adb71b08f3a
BLAKE2b-256 checksum
How to use checksums
d22f1882e604d12621d0acf929d11723735a0c8f25280d5c59db70648a54545a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for zc.time 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.2 This release

1 release file No provenance

1.0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page