views:

122

answers:

1

As per the title, I'm trying to parse an XML file containing an xs:duration data type. I'd like to convert that into a Python timedelta object, which I can then use in further calculations.

Is there any built-in way of doing this, similar to the strptime() function? If not, what is the best way to achieve this?

A: 

Seeing as I already have a working example of what I asked in the question, I'll post it here for completeness. If any better answers come up I'll accept.

period = '-P14D'
regex  = re.compile('(?P<sign>-?)P(?:(?P<years>\d+)Y)?(?:(?P<months>\d+)M)?(?:(?P<days>\d+)D)?(?:T(?:(?P<hours>\d+)H)?(?:(?P<minutes>\d+)M)?(?:(?P<seconds>\d+)S)?)?')

# Fetch the match groups with default value of 0 (not None)
duration = regex.match(period).groupdict(0)

# Create the timedelta object from extracted groups
delta = timedelta(days=int(duration['days']) + (int(duration['months']) * 30) + (int(duration['years']) * 365),
                  hours=int(duration['hours']),
                  minutes=int(duration['minutes']),
                  seconds=int(duration['seconds']))

if duration['sign'] == "-":
    delta *= -1

This works, but won't handle the month lengths or leap years correctly. For my purposes this isn't an issue, but it is worth keeping in mind.

Rezzie