user3812142
user3812142

Reputation: 87

Parse XML, date formats (PT0H0M0S,PT2920H0M0S)

I'm trying to parse an xml-file exported from MS Project 2013 using python and it contains the following data:

<TimephasedData>
    <Type>1</Type>
    <UID>4628</UID>
    <Start>2014-09-22T08:00:00</Start>
    <Finish>2015-09-22T08:00:00</Finish>
    <Unit>8</Unit>
    <Value>PT2920H0M0S</Value>
</TimephasedData>
<TimephasedData>
    <Type>1</Type>
    <UID>4628</UID>
    <Start>2015-09-22T08:00:00</Start>
    <Finish>2015-09-23T08:00:00</Finish>
    <Unit>2</Unit>
    <Value>PT8H0M0S</Value>
</TimephasedData>

What I don't understand is what the PT8H0M0S and PT2920H0M0S represents (TimephasedData). Currently I parse the dates by replacing the character "T" and then use strptime

Upvotes: 4

Views: 2469

Answers (1)

Tomalak
Tomalak

Reputation: 338228

This is a ISO 8601 time duration value. Compare the iCal duration data type spec, which uses this notation:

Formal Definition

The value type is defined by the following notation:

dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)
dur-date   = dur-day [dur-time]   
dur-time   = "T" (dur-hour / dur-minute / dur-second)
dur-week   = 1*DIGIT "W"
dur-hour   = 1*DIGIT "H" [dur-minute]
dur-minute = 1*DIGIT "M" [dur-second]  
dur-second = 1*DIGIT "S"
dur-day    = 1*DIGIT "D"

So PT8H0M0S is time-duration (T), 8 hours (8H), 0 minutes (0M), 0 seconds (0S). (The P stands for "Period", of course).

There is at least one Python package that can handle transforming these kinds of values, for example https://pypi.python.org/pypi/isodate.

Upvotes: 5

Related Questions