Skip to main content

Utilities for testing with pendulum timestamps

Project description

Plummet

Methods for testing with pendulum timestamps.

The most useful method for testing is the frozen_time() method which allows you to fix a moment in time so that all calls to pendulum.now() return the provided timestamp.

It is also possible to freeze timestamps given by datetime.now() by installing the time-machine extra dependency.

Methods

Here is a breakdown of the methods provided, what they do, and examples of how to use them

momentize()

This method is used to turn a variety of different timestamps into pendulum.DateTime instances in the UTC timezone.

Accepted types

  • String timestamps (anything that pendulum can parse)
  • datetime.datetime instances
  • pendulum.DateTime instances (that might be in other timezones)
  • None -- returns the current moment in UTC

Examples

Get the current time in UTC:

>>> momentize()
DateTime(2021, 11, 17, 21, 15, 0, 20728, tzinfo=Timezone('UTC'))

Convert a string timestamp:

>>> momentize("2021-11-17 21:29:00")
DateTime(2021, 11, 17, 21, 29, 0, tzinfo=Timezone('UTC'))

See pendulum's documentation for more info.

Convert a datetime.datetime:

>>> momentize(datetime.datetime(2021, 11, 17, 21, 29, 0))
DateTime(2021, 11, 17, 21, 29, 0, tzinfo=Timezone('UTC'))

If momentize cannot convert the provided object, it will raise an exception.

moments_match()

This method is used to compare two possibly different forms of timestamps to make sure they are exactly equal. Under the hood, it is using momentize() to convert the arguments to pendulum.DateTime instances and then compares the two.

Accepted types

  • String timestamps (anything that pendulum can parse)
  • datetime.datetime instances
  • pendulum.DateTime instances (that might be in other timezones)
  • None -- compares the current moment in UTC

Examples

Compare a string to a datetime.datetime:

>>> moments_match("2021-11-17 21:41:00", datetime.datetime(2021, 11, 17, 21, 41, 0))
True

Compare a pendulum.DateTime to a datetime.datetime in different timezones:

>>> moments_match(
...     pendulum.datetime(
...         2021, 11, 17, 13, 44, 0,
...         tz="America/Los_Angeles",
...         ),
...     datetime.datetime(
...         2021, 11, 17, 16, 44, 0,
...         tzinfo=datetime.timezone(datetime.timedelta(hours=-4)),
...     ),
... )
...
True

frozen_time()

The frozen_time method is the main functionality of this package. It allows you to freeze the time returned by pendulum.now() (and it's relatives) to a given moment.

Accepted types

  • String timestamps (anything that pendulum can parse)
  • datetime.datetime instances
  • pendulum.DateTime instances (that might be in other timezones)
  • None -- freeze at the current moment in UTC

Examples

Freeze time at a specific moment:

>>> with frozen_time("2021-11-17 22:03:00"):
...     now = pendulum.now("UTC")
...     print(now)
...
2021-11-17T22:03:00+00:00

Freezing datetime.now()

By default, the frozen_time method only works for pendulum.now(). However, if you install with the extra "time-machine", it is possible to make frozen_time work with datetime.now() as well.

Installing the extra "time-machine" dependency

To install the extra dependency with pip:

$ pip install plummet[time-machine]

To install the extra dependency with poetry:

$ poetry add plummet[time-machine]

Now, plummet will freeze datetime.now() as well:

>>> with frozen_time("2021-11-17 22:03:00"):
...     pendulum_now = pendulum.now("UTC")
...     datetime_now = datetime.now(tz=timezone.utc)
...     print(pendulum_now)
...     print(datetime_now)
...
2021-11-17T22:03:00+00:00
2021-11-17 22:03:00+00:00

Testing

To run the testing suite:

$ make test

To run the full set of quality checks:

$ make qa

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

plummet-1.0.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

plummet-1.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file plummet-1.0.0.tar.gz.

File metadata

  • Download URL: plummet-1.0.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.4.72-microsoft-standard-WSL2

File hashes

Hashes for plummet-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6046725a171c62987390933f656277f64bd26b16aa2593f9fcfeb1d1e0dac5e0
MD5 8f9b4f79f733c87732946b5828b445de
BLAKE2b-256 e689bd4f6da711260d911a098cc8d394e62132f8b8eefdda7d5dbcb0118f9ced

See more details on using hashes here.

File details

Details for the file plummet-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: plummet-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.8.10 Linux/5.4.72-microsoft-standard-WSL2

File hashes

Hashes for plummet-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c79fccdabf09496a908094da515dbb89e4a9d42f9c4d249bb4a99533edcb2562
MD5 f8ecc02a8fadb7e48f9c097f8003f69a
BLAKE2b-256 c75eb4373b9be1c672c9353dbd8ed20a6eb49f4b477e0955d795ddd9ba803fb8

See more details on using hashes here.

Supported by

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