Skip to content

Instantly share code, notes, and snippets.

@shollingsworth
Created December 21, 2017 19:10
Show Gist options
  • Save shollingsworth/963609d77c61184a6bb40a16b6812952 to your computer and use it in GitHub Desktop.
Save shollingsworth/963609d77c61184a6bb40a16b6812952 to your computer and use it in GitHub Desktop.
convert duration string like: 2d3h5m30s into seconds only (python)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
test_str = "1h5m"
test_str = "5d3h2m50s"
keys = {
'd' : 60 * 60 * 24,
'h' : 60 * 60,
'm' : 60,
's' : 1,
}
alphas = [i for i in re.split('[0-9]+', test_str) if i]
nums = [i for i in re.split('[a-z]+', test_str.lower()) if i]
retval = 0
for idx, metric in enumerate(alphas):
if metric not in keys.keys():
raise Exception("Error, bad metric value: {}, need: {}".format(metric, keys.keys()))
retval += int(keys[metric]) * int(nums[idx])
print(retval)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment