Skip to content

Negative xsd:duration decoded as days instead of seconds #1492

Description

@Bolt4243

Description

Duration.pythonvalue (src/zeep/xsd/types/builtins.py) decodes a negative xsd:duration with the wrong unit. isodate cannot parse a negative time component, so the code strips the minus, parses the positive duration, and negates it:

if value.startswith("PT-"):
    value = value.replace("PT-", "PT")
    result = isodate.parse_duration(value)
    return datetime.timedelta(0 - result.total_seconds())

timedelta's first positional argument is days, not seconds. result.total_seconds() (a seconds count) is passed as days, so the result is off by a factor of 86400. PT-30S decodes to -30 days instead of -30 seconds.

Steps to Reproduce

from zeep.xsd.types import builtins
d = builtins.Duration()
print(d.pythonvalue("PT-30S"))                 # -30 days, 0:00:00
print(d.pythonvalue("PT-30S").total_seconds()) # -2592000.0

Expected Behavior

PT-30S decodes to datetime.timedelta(seconds=-30) (-30.0 seconds).

Fix

Pass the value as seconds=:

return datetime.timedelta(seconds=0 - result.total_seconds())

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions