#25060: Add support for str(timedelta) representation in parse_duration
-----------------------------+--------------------
     Reporter:  vmspike      |      Owner:  nobody
         Type:  New feature  |     Status:  new
    Component:  Utilities    |    Version:  1.8
     Severity:  Normal       |   Keywords:
 Triage Stage:  Unreviewed   |  Has patch:  1
Easy pickings:  1            |      UI/UX:  0
-----------------------------+--------------------
 It's useful for conversions to/from str, so we can convert timedelta value
 to str and back without additional work.
 The following code can be used instead of original parse_duration function
 {{{
 # Support str(timedelta) format
 str_timedelta_duration_re = re.compile(
     r'^'
     r'(?:(?P<days>-?\d+) days*, )?'
     r'((?:(?P<hours>\d+):)(?=\d+:\d+))?'
     r'(?:(?P<minutes>\d+):)?'
     r'(?P<seconds>\d+)'
     r'(?:\.(?P<microseconds>\d{1,6})\d{0,6})?'
     r'$'
 )

 def parse_duration(value):
     """Parses a duration string and returns a datetime.timedelta.

     The preferred format for durations in Django is '%d %H:%M:%S.%f'.

     Also supports ISO 8601 and str(timedelta) representations.
     """
     for duration_re in (standard_duration_re, iso8601_duration_re,
 str_timedelta_duration_re):
         match = duration_re.match(value)
         if match:
             kw = match.groupdict()
             if kw.get('microseconds'):
                 kw['microseconds'] = kw['microseconds'].ljust(6, '0')
             kw = {k: float(v) for k, v in six.iteritems(kw) if v is not
 None}
             return datetime.timedelta(**kw)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25060>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.c0f49b90b8203f25754675853040c1ce%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to