django
django

Reputation: 2909

Python convert time duration in words to seconds

I am using external program which outputs time in this format.

15mn
1h 15mn 3sc
34 sc

How can I convert all such strings in words to seconds i.e (15mn = 900 seconds)?

Upvotes: 0

Views: 911

Answers (4)

Kent Martin
Kent Martin

Reputation: 11

Came across this looking for something else. Here's a much cleaner simpler solution using Python Pints

import pint
ureg = pint.UnitRegistry()

ureg.define("mn = minutes") # Define non-standard units
ureg.define("sc = seconds")
ureg.define("h = hours")

def parse_odd_time_format(s):
    duration = ureg("0 seconds") # Just something to initialize
    for x in s.split(" "):
        duration += ureg(x) # Parse all the bits and add them together 
    return duration.magnitude # Split off the seconds

parse_odd_time_format("1h 15mn 3sc")

Upvotes: 0

Jon Clements
Jon Clements

Reputation: 142226

Use an re a dict to get a multiplier, eg:

import re

text = '1h 15mn 3sc'
in_seconds = {'h': 60 * 60, 'mn': 60, 'sc': 1}
seconds = sum(int(num) * in_seconds[weight] for num, weight in re.findall(r'(\d+)\s?(mn|sc|h)', text))
# 4503

It's important to note that this allows constructs such as "1h 3mn 5h 3sc 12mn 2h 5sc" so may not be desirable...

Upvotes: 1

ssm
ssm

Reputation: 5383

Another one:

If you just specify a function which extracts out numbers with specific tags after them ...

def fs(x, p):
     p = re.sub('\s+', '', p) # get rid of spaces ...
     if re.search('[0-9]+'+x, p): # exp = (n digits) + (tag 'x') 
         return int( re.search('[0-9]+'+x, p).group()[:-len(x)] )
     else: return 0

Then you can subsequently just use the numbers for your computations ...

def toSec(p): return fs('h',p)*3600 + fs('mn',p)*60 + fs('sc',p)

Upvotes: 1

halex
halex

Reputation: 16403

I use a (rather complex :)) regex to parse your string.

>>> def s_to_secs(s):
        import re
        mat = re.match(r"((?P<hours>\d+)\s?h)?\s?((?P<minutes>\d+)\s?mn)?\s?((?P<seconds>\d+)\s?sc)?", s)
        secs = 0
        secs += int(mat.group("hours"))*3600 if mat.group("hours") else 0
        secs += int(mat.group("minutes"))*60 if mat.group("minutes") else 0
        secs += int(mat.group("seconds")) if mat.group("seconds") else 0
        return secs
>>> for s in ("15mn", "1h 15mn 3sc", "34 sc"):
        print(s_to_secs(s))
900
4503
34

Upvotes: 0

Related Questions