Skip to main content

Truncate datetime objects to a set level of precision

Project description

This module truncates a datetime object to the level of precision that you specify, making everything higher than that zero (or one for day and month).

It is based on PostgreSQL’s DATE_TRUNC.

Documentation available on Read the Docs.

Installation:

You can install from pypi!

pip install datetime_truncate

Usage:

>>> from datetime_truncate import truncate
>>> truncate(datetime(2012, 2, 4, 12, 24, 50, 234), 'second')
datetime(2012, 2, 4, 12, 24, 50)
>>> truncate(datetime(2012, 2, 4, 12, 24, 50), 'minute')
datetime(2012, 2, 4, 12, 24)
>>> truncate(datetime(2012, 2, 4, 12, 24, 50), '5_minute')
datetime(2012, 2, 4, 12, 20)
>>> truncate(datetime(2012, 2, 4, 12, 24, 50), '19_minute')
datetime(2012, 2, 4, 12, 19)
>>> truncate(datetime(2012, 2, 4, 12, 24), 'hour')
datetime(2012, 2, 4, 12)
>>> truncate(datetime(2012, 2, 4, 12, 24), 'day')
datetime(2012, 2, 4)
>>> truncate(datetime(2012, 2, 4, 12, 24), 'week')
datetime(2012, 1, 30)
>>> truncate(datetime(2012, 2, 4, 12, 24), 'month')
datetime(2012, 2, 1)
>>> truncate(datetime(2012, 2, 4, 12, 24), 'quarter')
datetime(2012, 1, 1)
>>> truncate(datetime(2012, 8, 18, 12, 25), 'half_year')
datetime(2012, 7, 1)
>>> truncate(datetime(2012, 8, 18, 12, 25), 'year')
datetime(2012, 1, 1)

There are also sugar functions available on the form:

  • truncate_second

  • truncate_minute

  • truncate_nth_minute

  • truncate_hour

  • truncate_day

  • truncate_week

  • truncate_month

  • truncate_quarter

  • truncate_half_year

  • truncate_year

Changes

1.1.0 - 2017-10-19

  • Added truncate to nth minute of the hour, so if you want to truncate to every third minute you do: truncate(dt, ‘3_minute’) or with some more sugar on top; truncate_nth_minute(dt, 3)

  • Fixed bugs with truncate quarter and half_year, thanks @thegrymek!

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

datetime_truncate-1.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

datetime_truncate-1.1.0-py2.py3-none-any.whl (5.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file datetime_truncate-1.1.0.tar.gz.

File metadata

File hashes

Hashes for datetime_truncate-1.1.0.tar.gz
Algorithm Hash digest
SHA256 aa194f04e6829c4bbf105c80a868955505e5f9c4830b0471ddca7d2ec6bf55c6
MD5 ee870a761a5536cdd8f4831e392381f7
BLAKE2b-256 a91f8927639218b9db321785366fb4c8463ef75b5aa535f3557b64edbd7eae1c

See more details on using hashes here.

File details

Details for the file datetime_truncate-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for datetime_truncate-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ff003f791b966e59f0f9d3d80b29fa68bc47f0f4af73c4a5d08a3db54afd0aca
MD5 52686b7496e50b3d0f0e1911c34f8f26
BLAKE2b-256 12cd3d11b8039791d1e0e7624c015cf7597bc8af2e40c621da203575402432fe

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