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), '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_hour

  • truncate_day

  • truncate_week

  • truncate_month

  • truncate_quarter

  • truncate_half_year

  • truncate_year

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.0.1.tar.gz (2.5 kB view details)

Uploaded Source

File details

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

File metadata

File hashes

Hashes for datetime_truncate-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5d8e6a73e871c781a9a20c9cdfb5125f9db836f40fdfee071d163bcbc0af412b
MD5 5f09c9243d0a2fbbd71944bff18d2995
BLAKE2b-256 4c368dceb9cc88d90ccba473b6f4c8a3655c31c5f96e7ff20c6aa70f521f1a1c

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