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