datetime_truncate_8.py 511 B

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