You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

duration.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import datetime
  2. def _get_duration_components(duration):
  3. days = duration.days
  4. seconds = duration.seconds
  5. microseconds = duration.microseconds
  6. minutes = seconds // 60
  7. seconds = seconds % 60
  8. hours = minutes // 60
  9. minutes = minutes % 60
  10. return days, hours, minutes, seconds, microseconds
  11. def duration_string(duration):
  12. """Version of str(timedelta) which is not English specific."""
  13. days, hours, minutes, seconds, microseconds = _get_duration_components(duration)
  14. string = '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)
  15. if days:
  16. string = '{} '.format(days) + string
  17. if microseconds:
  18. string += '.{:06d}'.format(microseconds)
  19. return string
  20. def duration_iso_string(duration):
  21. if duration < datetime.timedelta(0):
  22. sign = '-'
  23. duration *= -1
  24. else:
  25. sign = ''
  26. days, hours, minutes, seconds, microseconds = _get_duration_components(duration)
  27. ms = '.{:06d}'.format(microseconds) if microseconds else ""
  28. return '{}P{}DT{:02d}H{:02d}M{:02d}{}S'.format(sign, days, hours, minutes, seconds, ms)
  29. def duration_microseconds(delta):
  30. return (24 * 60 * 60 * delta.days + delta.seconds) * 1000000 + delta.microseconds