diff options
| author | Will Hardy <Will.Hardy@thermondo.de> | 2016-05-26 14:48:36 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-07-14 13:34:15 -0400 |
| commit | 8ef78b81654ebcf19a1fc241e2b1ede35100096b (patch) | |
| tree | 67baaa077b5ed4530330a2202b5d3906708690b3 /django/utils/duration.py | |
| parent | a7b5dfd1703a8fbed70b7aca5e6dda092af51154 (diff) | |
Fixed #26656 -- Added duration (timedelta) support to DjangoJSONEncoder.
Diffstat (limited to 'django/utils/duration.py')
| -rw-r--r-- | django/utils/duration.py | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/django/utils/duration.py b/django/utils/duration.py index c37c885b91..53322b51d2 100644 --- a/django/utils/duration.py +++ b/django/utils/duration.py @@ -1,7 +1,7 @@ -"""Version of str(timedelta) which is not English specific.""" +import datetime -def duration_string(duration): +def _get_duration_components(duration): days = duration.days seconds = duration.seconds microseconds = duration.microseconds @@ -12,6 +12,13 @@ def duration_string(duration): hours = minutes // 60 minutes = minutes % 60 + return days, hours, minutes, seconds, microseconds + + +def duration_string(duration): + """Version of str(timedelta) which is not English specific.""" + days, hours, minutes, seconds, microseconds = _get_duration_components(duration) + string = '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds) if days: string = '{} '.format(days) + string @@ -19,3 +26,15 @@ def duration_string(duration): string += '.{:06d}'.format(microseconds) return string + + +def duration_iso_string(duration): + if duration < datetime.timedelta(0): + sign = '-' + duration *= -1 + else: + sign = '' + + days, hours, minutes, seconds, microseconds = _get_duration_components(duration) + ms = '.{:06d}'.format(microseconds) if microseconds else "" + return '{}P{}DT{:02d}H{:02d}M{:02d}{}S'.format(sign, days, hours, minutes, seconds, ms) |
