Parses a duration string of the form 1h2h3m4s5.6ms4.5us7.8ns into milliseconds.
(duration)
| 90 | |
| 91 | |
| 92 | def parse_duration_string_ms(duration): |
| 93 | """Parses a duration string of the form 1h2h3m4s5.6ms4.5us7.8ns into milliseconds.""" |
| 94 | pattern = r'(?P<value>[0-9]+\.?[0-9]*?)(?P<units>\D+)' |
| 95 | matches = list(re.finditer(pattern, duration)) |
| 96 | assert matches, 'Failed to parse duration string %s' % duration |
| 97 | |
| 98 | times = {'h': 0, 'm': 0, 's': 0, 'ms': 0} |
| 99 | for match in matches: |
| 100 | parsed = match.groupdict() |
| 101 | times[parsed['units']] = float(parsed['value']) |
| 102 | |
| 103 | return (times['h'] * 60 * 60 + times['m'] * 60 + times['s']) * 1000 + times['ms'] |
| 104 | |
| 105 | |
| 106 | def parse_duration_string_ns(duration): |
no outgoing calls