README.rst 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. ==================
  2. datetime_truncate
  3. ==================
  4. This module truncates a datetime object to the level of precision that
  5. you specify, making everything higher than that zero (or one for day
  6. and month).
  7. It is based on PostgreSQL's DATE_TRUNC_.
  8. Documentation available on `Read the Docs`_.
  9. Installation:
  10. -------------
  11. You can install from pypi!
  12. .. code-block::
  13. pip install datetime_truncate
  14. Usage:
  15. ------
  16. .. code-block::
  17. >>> from datetime_truncate import truncate
  18. >>> truncate(datetime(2012, 2, 4, 12, 24, 50, 234), 'second')
  19. datetime(2012, 2, 4, 12, 24, 50)
  20. >>> truncate(datetime(2012, 2, 4, 12, 24, 50), 'minute')
  21. datetime(2012, 2, 4, 12, 24)
  22. >>> truncate(datetime(2012, 2, 4, 12, 24), 'hour')
  23. datetime(2012, 2, 4, 12)
  24. >>> truncate(datetime(2012, 2, 4, 12, 24), 'day')
  25. datetime(2012, 2, 4)
  26. >>> truncate(datetime(2012, 2, 4, 12, 24), 'week')
  27. datetime(2012, 1, 30)
  28. >>> truncate(datetime(2012, 2, 4, 12, 24), 'month')
  29. datetime(2012, 2, 1)
  30. >>> truncate(datetime(2012, 2, 4, 12, 24), 'quarter')
  31. datetime(2012, 1, 1)
  32. >>> truncate(datetime(2012, 8, 18, 12, 25), 'half_year')
  33. datetime(2012, 7, 1)
  34. >>> truncate(datetime(2012, 8, 18, 12, 25), 'year')
  35. datetime(2012, 1, 1)
  36. There are also sugar functions available on the form:
  37. * `truncate_second`
  38. * `truncate_minute`
  39. * `truncate_hour`
  40. * `truncate_day`
  41. * `truncate_week`
  42. * `truncate_month`
  43. * `truncate_quarter`
  44. * `truncate_half_year`
  45. * `truncate_year`
  46. .. _DATE_TRUNC: http://www.postgresql.org/docs/9.1/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
  47. .. _Read the Docs: http://datetime_truncate.readthedocs.org/en/latest/