datetime_truncate_7.py 640 B

12345678910111213141516171819
  1. def truncate_quarter(datetime):
  2. '''
  3. Truncates the datetime to the first day of the quarter for this date.
  4. :params datetime: an initialized datetime object
  5. :return: `datetime` with the month set to the first month of this quarter
  6. :rtype: :py:mod:`datetime` datetime object
  7. '''
  8. datetime = truncate(datetime, 'month')
  9. month = datetime.month
  10. if 1 <= month <= 3:
  11. return datetime.replace(month=1)
  12. elif 4 <= month <= 6:
  13. return datetime.replace(month=4)
  14. elif 7 <= month <= 9:
  15. return datetime.replace(month=7)
  16. elif 10 <= month <= 12:
  17. return datetime.replace(month=10)