def parse_duration(value):
 match = standard_duration_re.match(value)
 if (not match):
  match = iso8601_duration_re.match(value)
 if match:
  kw = match.groupdict()
  sign = ((-1) if (kw.pop('sign', '+') == '-') else 1)
  if kw.get('microseconds'):
   kw['microseconds'] = kw['microseconds'].ljust(6, '0')
  if (kw.get('seconds') and kw.get('microseconds') and kw['seconds'].startswith('-')):
   kw['microseconds'] = ('-' + kw['microseconds'])
  kw = {k: float(v) for (k, v) in kw.items() if (v is not None)}
  return (sign * datetime.timedelta(**kw))

def parse_duration ( value ) : match = standard_duration_re . match ( value ) if ( not match ) : match = iso8601_duration_re . match ( value ) if match : kw = match . groupdict ( ) sign = ( ( - 1 ) if ( kw . pop ( 'sign' , '+' ) == '-' ) else 1 ) if kw . get ( 'microseconds' ) : kw [ 'microseconds' ] = kw [ 'microseconds' ] . ljust ( 6 , '0' ) if ( kw . get ( 'seconds' ) and kw . get ( 'microseconds' ) and kw [ 'seconds' ] . startswith ( '-' ) ) : kw [ 'microseconds' ] = ( '-' + kw [ 'microseconds' ] ) kw = { k : float ( v ) for ( k , v ) in kw . items ( ) if ( v is not None ) } return ( sign * datetime . timedelta ( ** kw ) )
parses a duration string and returns a datetime .