https://github.com/mediapop/datetime_truncate
Truncate datetime objects to the specifiec level of precision, inspired by PostgreSQL's DATE_TRUNC.
https://github.com/mediapop/datetime_truncate
Last synced: 2 months ago
JSON representation
Truncate datetime objects to the specifiec level of precision, inspired by PostgreSQL's DATE_TRUNC.
- Host: GitHub
- URL: https://github.com/mediapop/datetime_truncate
- Owner: mediapop
- License: mit
- Created: 2013-04-01T09:23:31.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2021-04-20T20:39:45.000Z (about 4 years ago)
- Last Synced: 2024-04-26T04:04:24.775Z (about 1 year ago)
- Language: Python
- Size: 26.4 KB
- Stars: 14
- Watchers: 8
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
==================
datetime_truncate
==================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!
.. code-block::
pip install datetime_truncate
Usage:
------.. code-block::
>>> 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`.. _DATE_TRUNC: http://www.postgresql.org/docs/9.1/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
.. _Read the Docs: http://datetime_truncate.readthedocs.org/en/latest/