timeparse.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. '''
  3. timeparse.py
  4. (c) Will Roberts <[email protected]> 1 February, 2014
  5. This is a vendored and modified copy of:
  6. github.com/wroberts/pytimeparse @ cc0550d
  7. It has been modified to mimic the behaviour of
  8. https://golang.org/pkg/time/#ParseDuration
  9. '''
  10. # MIT LICENSE
  11. #
  12. # Permission is hereby granted, free of charge, to any person
  13. # obtaining a copy of this software and associated documentation files
  14. # (the "Software"), to deal in the Software without restriction,
  15. # including without limitation the rights to use, copy, modify, merge,
  16. # publish, distribute, sublicense, and/or sell copies of the Software,
  17. # and to permit persons to whom the Software is furnished to do so,
  18. # subject to the following conditions:
  19. #
  20. # The above copyright notice and this permission notice shall be
  21. # included in all copies or substantial portions of the Software.
  22. #
  23. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. # SOFTWARE.
  31. import re
  32. HOURS = r'(?P<hours>[\d.]+)h'
  33. MINS = r'(?P<mins>[\d.]+)m'
  34. SECS = r'(?P<secs>[\d.]+)s'
  35. MILLI = r'(?P<milli>[\d.]+)ms'
  36. MICRO = r'(?P<micro>[\d.]+)(?:us|µs)'
  37. NANO = r'(?P<nano>[\d.]+)ns'
  38. def opt(x):
  39. return r'(?:{x})?'.format(x=x)
  40. TIMEFORMAT = r'{HOURS}{MINS}{SECS}{MILLI}{MICRO}{NANO}'.format(
  41. HOURS=opt(HOURS),
  42. MINS=opt(MINS),
  43. SECS=opt(SECS),
  44. MILLI=opt(MILLI),
  45. MICRO=opt(MICRO),
  46. NANO=opt(NANO),
  47. )
  48. MULTIPLIERS = {
  49. 'hours': 60 * 60,
  50. 'mins': 60,
  51. 'secs': 1,
  52. 'milli': 1.0 / 1000,
  53. 'micro': 1.0 / 1000.0 / 1000,
  54. 'nano': 1.0 / 1000.0 / 1000.0 / 1000.0,
  55. }
  56. def timeparse(sval):
  57. """Parse a time expression, returning it as a number of seconds. If
  58. possible, the return value will be an `int`; if this is not
  59. possible, the return will be a `float`. Returns `None` if a time
  60. expression cannot be parsed from the given string.
  61. Arguments:
  62. - `sval`: the string value to parse
  63. >>> timeparse('1m24s')
  64. 84
  65. >>> timeparse('1.2 minutes')
  66. 72
  67. >>> timeparse('1.2 seconds')
  68. 1.2
  69. """
  70. match = re.match(r'\s*' + TIMEFORMAT + r'\s*$', sval, re.I)
  71. if not match or not match.group(0).strip():
  72. return
  73. mdict = match.groupdict()
  74. return sum(
  75. MULTIPLIERS[k] * cast(v) for (k, v) in mdict.items() if v is not None)
  76. def cast(value):
  77. return int(value) if value.isdigit() else float(value)