MCPcopy Create free account
hub / github.com/apache/impala / parse_duration_string_ns

Function parse_duration_string_ns

tests/util/parse_util.py:106–120  ·  view source on GitHub ↗

Parses a duration string of the form 1h2h3m4s5.6ms4.5us7.8ns into nanoseconds.

(duration)

Source from the content-addressed store, hash-verified

104
105
106def parse_duration_string_ns(duration):
107 """Parses a duration string of the form 1h2h3m4s5.6ms4.5us7.8ns into nanoseconds."""
108 pattern = r'(?P<value>[0-9]+\.?[0-9]*?)(?P<units>\D+)'
109 matches = list(re.finditer(pattern, duration))
110 assert matches, 'Failed to parse duration string %s' % duration
111
112 times = {'h': 0, 'm': 0, 's': 0, 'ms': 0, 'us': 0, 'ns': 0}
113 for match in matches:
114 parsed = match.groupdict()
115 times[parsed['units']] = float(parsed['value'])
116
117 value_ns = (times['h'] * 60 * 60 + times['m'] * 60 + times['s']) * 1000000000
118 value_ns += times['ms'] * 1000000 + times['us'] * 1000 + times['ns']
119
120 return value_ns
121
122
123def get_duration_us_from_str(duration_str):

Callers 2

Calls

no outgoing calls

Tested by 1