Negative duration

Issue #19 closed
KRITIKH Publishing created an issue

Hello, I selected this library to be used as part of pysaml2 - the SAML2 implementation in python.

The SAML2 dateTime datatype is based on XML Schema xs:dateTime datatype, which in turn is based on ISO 8601. One of the differences is that a duration is allowed to be negative.

An optional preceding minus sign ('-') is allowed, to indicate a negative duration. If the sign is omitted a positive duration is indicated. [0]

[...]

An optional minus sign is allowed immediately preceding, without a space, the lexical representations for duration, dateTime, date, gYearMonth, gYear. [1]

While this is not defined by ISO 8601, I see that negative durations are supported when defined within the period, for example PT-1S. Many libraries choose to support this format.

If this is not working by mistake, I would propose to also support the format that xml schema defines, which is an ISO 8601 duration format that starts with a negative sign, for example -P2Y. The python timedelta allows for such representation, and in fact

timedelta(seconds=-1) == -timedelta(seconds=1) == timedelta(seconds=1)*-1

This enables means that the implementation to be just a wrapper to the current behaviour.

period = '-PT1S'
is_negative = period[0] is '-'
sign = [1, -1][is_negative]
result = sign * parse_duration(period[is_negative:])

What do you think?

[0] https://www.w3.org/TR/xmlschema-2/#duration [1] https://www.w3.org/TR/xmlschema-2/#signallowed

Comments (2)

  1. Log in to comment